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

#host_itemsObject



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

def host_items
  @host_items ||= sections.map do |s|
    Section.new(s) if s =~ /^hoststatus/ && in_scope?(s)
  end.compact
end

#host_problemsObject



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

def host_problems
  @host_problems ||= sections.map do |s|
    Section.new(s) if s =~ /^hoststatus/ && in_scope?(s) && problem?(s)
  end.compact
end

#in_scope?(section) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#itemsObject



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

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

#problem?(section) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/nagios_analyzer/status.rb', line 71

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

#reset_cache!Object



75
76
77
# File 'lib/nagios_analyzer/status.rb', line 75

def reset_cache!
  @items = @service_items = @host_items = nil
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("\n\n")
end

#service_itemsObject



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

def service_items
  @service_items ||= sections.map do |s|
    Section.new(s) if s =~ /^servicestatus/ && in_scope?(s)
  end.compact
end

#service_problemsObject



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

def service_problems
  @service_problems ||= sections.map do |s|
    Section.new(s) if s =~ /^servicestatus/ && in_scope?(s) && problem?(s)
  end.compact
end