Class: Aspera::Nagios

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/nagios.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNagios

Returns a new instance of Nagios.



54
55
56
# File 'lib/aspera/nagios.rb', line 54

def initialize
  @data = []
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



52
53
54
# File 'lib/aspera/nagios.rb', line 52

def data
  @data
end

Class Method Details

.process(data) ⇒ Object

process results of a analysis and display status and exit with code



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

def process(data)
  Aspera.assert_type(data, Array)
  Aspera.assert(!data.empty?){'data is empty'}
  %w[status component message].each do |c|
    Aspera.assert(data.first.key?(c)){"result must have #{c}"}
  end
  res_errors = data.reject{|s|s['status'].eql?('ok')}
  # keep only errors in case of problem, other ok are assumed so
  data = res_errors unless res_errors.empty?
  # first is most critical
  data.sort!{|a, b|LEVELS.index(a['status'].to_sym) <=> LEVELS.index(b['status'].to_sym)}
  # build message: if multiple components: concatenate
  # message = data.map{|i|"#{i['component']}:#{i['message']}"}.join(', ').gsub("\n",' ')
  message = data
    .map{|i|i['component']}
    .uniq
    .map{|comp|comp + ':' + data.select{|d|d['component'].eql?(comp)}.map{|d|d['message']}.join(',')}
    .join(', ')
    .tr("\n", ' ')
  status = data.first['status'].upcase
  # display status for nagios
  puts("#{status} - [#{message}]\n")
  # provide exit code to nagios
  Process.exit(LEVELS.index(data.first['status'].to_sym))
end

Instance Method Details

#check_product_version(component, _product, version) ⇒ Object



75
76
77
78
# File 'lib/aspera/nagios.rb', line 75

def check_product_version(component, _product, version)
  add_ok(component, "version #{version}")
  # TODO: check on database if latest version
end

#check_time_offset(remote_date, component) ⇒ Object

compare remote time with local time



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aspera/nagios.rb', line 59

def check_time_offset(remote_date, component)
  # check date if specified : 2015-10-13T07:32:01Z
  remote_time = DateTime.strptime(remote_date)
  diff_time = (remote_time - DateTime.now).abs
  diff_rounded = diff_time.round(-2)
  Log.log.debug{"DATE: #{remote_date} #{remote_time} diff=#{diff_rounded}"}
  msg = "offset #{diff_rounded} sec"
  if diff_time >= DATE_CRIT_OFFSET
    add_critical(component, msg)
  elsif diff_time >= DATE_WARN_OFFSET
    add_warning(component, msg)
  else
    add_ok(component, msg)
  end
end

#resultObject

translate for display



81
82
83
84
# File 'lib/aspera/nagios.rb', line 81

def result
  raise 'missing result' if @data.empty?
  {type: :object_list, data: @data.map{|i|{'status' => LEVELS[i[:code]].to_s, 'component' => i[:comp], 'message' => i[:msg]}}}
end