Class: Nagios::Status
- Inherits:
-
Object
- Object
- Nagios::Status
- Defined in:
- lib/nagios/status.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#find_hosts(options = {}) ⇒ Object
Returns a list of all hosts matching the options in options.
-
#find_services(options = {}) ⇒ Object
Returns a list of all services matching the options in options.
-
#initialize(statusfile = nil) ⇒ Status
constructor
A new instance of Status.
-
#parsestatus(path = nil) ⇒ Object
(also: #parse)
Parses a nagios status file returning a data structure for all the data.
Constructor Details
#initialize(statusfile = nil) ⇒ Status
Returns a new instance of Status.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/nagios/status.rb', line 5 def initialize(statusfile=nil) if statusfile raise ArgumentError, "Statusfile file name must be provided" unless statusfile raise RuntimeError, "Statusfile #{statusfile} does not exist" unless File.exist? statusfile raise RuntimeError, "Statusfile #{statusfile} is not readable" unless File.readable? statusfile @path = statusfile end @status = {'hosts' => { }} self end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/nagios/status.rb', line 3 def path @path end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
3 4 5 |
# File 'lib/nagios/status.rb', line 3 def status @status end |
Instance Method Details
#find_hosts(options = {}) ⇒ Object
Returns a list of all hosts matching the options in options
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nagios/status.rb', line 51 def find_hosts( = {}) forhost = .fetch(:forhost, []) notifications = .fetch(:notifyenabled, nil) action = .fetch(:action, nil) withservice = .fetch(:withservice, []) hosts = [] searchquery = [] # Build up a search query for find_with_properties each # array member is a hash of property and a match forhost.each do |host| searchquery << search_term("host_name", host) end withservice.each do |s| searchquery << search_term("service_description", s) end searchquery << {"notifications_enabled" => notifications.to_s} if notifications hsts = find_with_properties(searchquery) hsts.each do |host| host_name = host["host_name"] hosts << parse_command_template(action, host_name, "", host_name) end hosts.uniq.sort end |
#find_services(options = {}) ⇒ Object
Returns a list of all services matching the options in options
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/nagios/status.rb', line 84 def find_services( = {}) forhost = .fetch(:forhost, []) notifications = .fetch(:notifyenabled, nil) action = .fetch(:action, nil) withservice = .fetch(:withservice, []) acknowledged = .fetch(:acknowledged, nil) passive = .fetch(:passive, nil) current_state = .fetch(:current_state, nil) json = .fetch(:json, false) details = .fetch(:details, false) services = [] searchquery = [] # Build up a search query for find_with_properties each # array member is a hash of property and a match forhost.each do |host| searchquery << search_term("host_name", host) end withservice.each do |s| searchquery << search_term("service_description", s) end searchquery << {"current_state" => current_state } if current_state searchquery << {"notifications_enabled" => notifications.to_s} if notifications searchquery << {"problem_has_been_acknowledged" => acknowledged.to_s} if acknowledged if passive searchquery << {"active_checks_enabled" => 0} searchquery << {"passive_checks_enabled" => 1} end svcs = find_with_properties(searchquery) svcs.each do |service| service_description = service["service_description"] host_name = service["host_name"] # when printing services with notifications en/dis it makes # most sense to print them in host:service format, abuse the # action option to get this result action = "${host}:${service}" if (notifications != nil && action == nil) services << parse_command_template(action, host_name, service_description, service_description) end if json [ "[", svcs.join(", \n").gsub("=>", ":"), "]" ] elsif details space = ' '*100 delim = ' ' state = ['OK', 'Warning', 'Critical', 'Unknown'] details = Array.new svcs.each do |s| details << (s['host_name'] + space)[0, 25] + delim \ + (s['service_description'] + space)[0,35] + delim \ + (state[s['current_state'].to_i].to_s + space)[0,8] + delim \ + (s['plugin_output'] + space)[0,120] end details else services.uniq.sort end end |
#parsestatus(path = nil) ⇒ Object Also known as: parse
Parses a nagios status file returning a data structure for all the data
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/nagios/status.rb', line 18 def parsestatus path=nil path ||= @path raise ArgumentError, "Statusfile file name must be provided either in constructor or as argument to parsestatus method" unless path @status, handler, blocklines = {'hosts' => {}}, '', [] File.readlines(path, :encoding => 'iso-8859-1').each do |line| # start of new sections if line =~ /(\w+) \{/ blocklines = [] handler = $1 end # gather all the lines for the block into an array # we'll pass them to a handler for this kind of block if line =~ /\s+(\w+)=(.+)/ && handler != "" blocklines << line end # end of a section if line =~ /\}/ && handler != "" && self.respond_to?("handle_#{handler}", include_private = true) self.send "handle_#{handler}".to_sym, blocklines handler = "" end end self end |