Class: NagiosAnalyzer::Status

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

Constant Summary collapse

STATE_OK =
0
STATES =
{
  0 => "OK",
  1 => "WARNING",
  2 => "CRITICAL",
  3 => "UNKNOWN",
  4 => "DEPENDENT"
}
STATES_ORDER =
{
  2 => 0, #critical => first etc.
  3 => 1,
  1 => 2,
  4 => 3,
  0 => 4
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statusfile, options = {}) ⇒ Status

Returns a new instance of Status.



21
22
23
24
25
26
27
28
29
# File 'lib/nagios_analyzer/status.rb', line 21

def initialize(statusfile, options = {})
  @file = statusfile
  sections #loads section at this point so we raise immediatly if file has a item
  @last_updated = Time.at(File.mtime(statusfile))
  #scope is an array of lambda procs : it evaluates to true if service has to be displayed
  @scopes = []
  @scopes << lambda { |section| !section.include?("current_state=#{STATE_OK}") } unless options[:include_ok]
  @scopes << options[:scope] if options[:scope].is_a?(Proc)
end

Instance Attribute Details

#last_updatedObject

Returns the value of attribute last_updated.



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

def last_updated
  @last_updated
end

#scopesObject

Returns the value of attribute scopes.



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

def scopes
  @scopes
end

Instance Method Details

#contactstatus_itemsObject



49
50
51
# File 'lib/nagios_analyzer/status.rb', line 49

def contactstatus_items
  section_items(:contactstatus)
end

#host_itemsObject



37
38
39
# File 'lib/nagios_analyzer/status.rb', line 37

def host_items
  section_items(:hoststatus)
end

#host_problemsObject



85
86
87
# File 'lib/nagios_analyzer/status.rb', line 85

def host_problems
  section_items(:hoststatus, true)
end

#hostcomment_itemsObject



53
54
55
# File 'lib/nagios_analyzer/status.rb', line 53

def hostcomment_items
  section_items(:hostcomment)
end

#hostdowntime_itemsObject



57
58
59
# File 'lib/nagios_analyzer/status.rb', line 57

def hostdowntime_items
  section_items(:hostdowntime)
end

#hoststatus_itemsObject



61
62
63
# File 'lib/nagios_analyzer/status.rb', line 61

def hoststatus_items
  section_items(:hoststatus)
end

#in_scope?(section) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/nagios_analyzer/status.rb', line 93

def in_scope?(section)
  @scopes.inject(true) do |memo,condition|
    memo && condition.call(section)
  end
end

#info_itemsObject



65
66
67
# File 'lib/nagios_analyzer/status.rb', line 65

def info_items
  section_items(:info)
end

#itemsObject



45
46
47
# File 'lib/nagios_analyzer/status.rb', line 45

def items
  @items ||= (host_items + service_items)
end

#problem?(section) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/nagios_analyzer/status.rb', line 99

def problem?(section)
  section.match(/current_state=(\d+)/) && $1.to_i != STATE_OK
end

#programstatus_itemsObject



69
70
71
# File 'lib/nagios_analyzer/status.rb', line 69

def programstatus_items
  section_items(:programstatus)
end

#reset_cache!Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nagios_analyzer/status.rb', line 103

def reset_cache!
  @items = nil
  [
    :contactstatus,
    :hostcomment,
    :hostdowntime,
    :hoststatus,
    :info,
    :programstatus,
    :servicecomment,
    :servicedowntime,
    :servicestatus,
  ].each do |name|
    self.instance_variable_set(section_var_sym(name, true), nil)
    self.instance_variable_set(section_var_sym(name, false), nil)
  end
end

#sectionsObject



31
32
33
34
35
# File 'lib/nagios_analyzer/status.rb', line 31

def sections
  # don't try to instanciate each section ! on my conf (85hosts/700services),
  # it makes the script more 10 times slower (0.25s => >3s)
  @sections ||= File.read(@file).split(/\}\s*\n/).map(&:strip)
end

#service_itemsObject



41
42
43
# File 'lib/nagios_analyzer/status.rb', line 41

def service_items
  section_items(:servicestatus)
end

#service_problemsObject



89
90
91
# File 'lib/nagios_analyzer/status.rb', line 89

def service_problems
  section_items(:servicestatus, true)
end

#servicecomment_itemsObject



73
74
75
# File 'lib/nagios_analyzer/status.rb', line 73

def servicecomment_items
  section_items(:servicecomment)
end

#servicedowntime_itemsObject



77
78
79
# File 'lib/nagios_analyzer/status.rb', line 77

def servicedowntime_items
  section_items(:servicedowntime)
end

#servicestatus_itemsObject



81
82
83
# File 'lib/nagios_analyzer/status.rb', line 81

def servicestatus_items
  section_items(:servicestatus)
end