Class: NagiosAnalyzer::Section

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

Instance Method Summary collapse

Constructor Details

#initialize(section) ⇒ Section

Returns a new instance of Section.



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

def initialize(section)
  @section = section.strip
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



7
8
9
# File 'lib/nagios_analyzer/section.rb', line 7

def method_missing(method, *args)
  hash.send(method, *args)
end

Instance Method Details

#<=>(other) ⇒ Object



30
31
32
# File 'lib/nagios_analyzer/section.rb', line 30

def <=>(other)
  self.sort_array <=> other.sort_array
end

#hashObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nagios_analyzer/section.rb', line 11

def hash
  return @hash if @hash
  @hash = {}
  @section.each_line do |line|
    line.strip!
    if line.match(/(\S+) \{/)
      @hash[:type] = $1
    elsif line.match(/(\S+)=(.*)/) #more efficient than include?+split+join..
      @hash[$1.to_sym] = ($2 == "#{$2.to_i}" ? $2.to_i : $2)
    end
  end
  if @hash[:type] == "servicestatus"
    @hash[:status] = Status::STATES[@hash[:current_state]]
  else
    @hash[:status] = (@hash[:current_state] == Status::STATE_OK ? "OK" : "CRITICAL")
  end
  @hash
end

#sort_arrayObject



34
35
36
37
38
39
# File 'lib/nagios_analyzer/section.rb', line 34

def sort_array
  [ (self[:type] == "servicestatus" ? 1 : 0),
    Status::STATES_ORDER[self[:current_state]].to_i,
    self[:host_name],
    self[:service_description].to_s ]
end