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
10
11
12
13
14
# File 'lib/nagios_analyzer/section.rb', line 7

def method_missing(method, *args)
  begin
    hash.send(method, *args)
  rescue NoMethodError => e
    raise e if args.size > 0
    hash[method]
  end
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
# File 'lib/nagios_analyzer/section.rb', line 50

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

#hashObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nagios_analyzer/section.rb', line 25

def hash
  return @hash if @hash
  @hash = {}
  @section.each_line do |line|
    line.strip!
    if line.match(/^\s*([a-zA-Z0-9]*)\s*\{/)
      @hash[:type] = $1
    elsif line.match(/(\S+)=(.*)/) #more efficient than include?+split+join..
      property, value = ["#{$1}", "#{$2}"]
      @hash[property.to_sym] =
        case
        when value.strip =~ /^[0-9]+$/ then value.to_i
        when value.strip =~ /^[0-9.]+$/ then value.to_f
        else value
        end
    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



54
55
56
57
58
59
# File 'lib/nagios_analyzer/section.rb', line 54

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

#typeObject



20
21
22
# File 'lib/nagios_analyzer/section.rb', line 20

def type
  hash[:type]
end