Class: Nagios::Status

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nagios/status.rb', line 35

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/nagios/status.rb', line 68

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

    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 << {"notifications_enabled" => notifications.to_s} if notifications

    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(statusfile) ⇒ Object

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nagios/status.rb', line 6

def parsestatus(statusfile)
        @status = {}
        @status["hosts"] = {}

        handler = ""
        blocklines = []

        File.readlines(statusfile).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)
                eval("handle_#{handler}(blocklines)")
                handler = ""
            end
        end
end