Class: Nagios::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios/status.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statusfile = nil) ⇒ Status

Returns a new instance of Status.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nagios/status.rb', line 6

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

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/nagios/status.rb', line 3

def path
  @path
end

#statusObject (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



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
82
# File 'lib/nagios/status.rb', line 52

def find_hosts(options = {})
  forhost = options.fetch(:forhost, [])
  notifications = options.fetch(:notifyenabled, nil)
  action = options.fetch(:action, nil)
  withservice = options.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



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
# File 'lib/nagios/status.rb', line 85

def find_services(options = {})
  forhost = options.fetch(:forhost, [])
  notifications = options.fetch(:notifyenabled, nil)
  action = options.fetch(:action, nil)
  withservice = options.fetch(:withservice, [])
  acknowledged = options.fetch(:acknowledged, nil)
  passive = options.fetch(:passive, nil)
  current_state = options.fetch(:current_state, nil)

  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

  services.uniq.sort
end

#parsestatus(path = nil) ⇒ Object Also known as: parse

Parses a nagios status file returning a data structure for all the data

Raises:

  • (ArgumentError)


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
47
# File 'lib/nagios/status.rb', line 19

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