Class: Carbios::PerfData

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/carbios/perfdata.rb

Constant Summary collapse

SERVICEKEY =
'SERVICEPERFDATA'
HOSTKEY =
'HOSTPERFDATA'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#normalize_key

Constructor Details

#initialize(checkstr, options = {}) ⇒ PerfData

Returns a new instance of PerfData.



11
12
13
14
15
16
17
18
19
20
# File 'lib/carbios/perfdata.rb', line 11

def initialize ( checkstr, options = {} )
  @prefix    = options[:prefix]
  parse_checkdata(checkstr)
  @time      = Time.at(@checkdata['TIMET'].to_i)
  @hostname  = Hostname.new( @checkdata['HOSTNAME'], 
                             :base_hostname    => options[:base_hostname] )
  @desc      = normalize_key(@checkdata['SERVICEDESC']) if @checkdata['SERVICEDESC']
  @reverse_hostname = options[:reverse_hostname]
  parse_perfdata
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



6
7
8
# File 'lib/carbios/perfdata.rb', line 6

def hostname
  @hostname
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/carbios/perfdata.rb', line 6

def time
  @time
end

Instance Method Details

#has_perfdata?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/carbios/perfdata.rb', line 41

def has_perfdata?
  !@perfdata.empty?
end

#normalize_perfdataObject



37
38
39
# File 'lib/carbios/perfdata.rb', line 37

def normalize_perfdata
  Hash[@perfdata.map { |k,v| [normalize_key(k), v] }]
end

#parse_checkdata(checkstr) ⇒ Object



22
23
24
# File 'lib/carbios/perfdata.rb', line 22

def parse_checkdata( checkstr )
  @checkdata = Hash[checkstr.scan(/(.*?)::(?:(.*?)(?:\t+|$))/)]
end

#parse_perfdataObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/carbios/perfdata.rb', line 26

def parse_perfdata
  if @checkdata.has_key? SERVICEKEY
    @key = SERVICEKEY
  elsif @checkdata.has_key? HOSTKEY
    @key = HOSTKEY
  else
    raise "Cannot find #{SERVICEKEY} or #{HOSTKEY} keys"
  end
  @perfdata = Hash[@checkdata[@key].scan(/(\S+)=(-?\d+(?:\.\d+)?)\S*\s?/)]
end

#to_hObject



45
46
47
48
49
# File 'lib/carbios/perfdata.rb', line 45

def to_h
  hostname = @reverse_hostname ? @hostname.reverse : @hostname
  prefix = [@prefix, hostname, @desc].compact.join('.')
  normalize_perfdata.prefix_keys("#{prefix}.")
end