class Nagios_Reporter

Attributes

state[R]

Public Class Methods

new() click to toggle source
# File lib/nagios_reporter.rb, line 4
def initialize
  # 2d array of reports implemented with a hash
  @reports = Hash.new
  @type = :all
  @silent_exit = false
  @state = -1
end

Public Instance Methods

critical(message) click to toggle source
# File lib/nagios_reporter.rb, line 37
def critical(message)
  report(2,message)
end
critical!(message) click to toggle source
# File lib/nagios_reporter.rb, line 40
def critical!(message)
  report!(2,message)
end
final_report() click to toggle source
# File lib/nagios_reporter.rb, line 53
def final_report
  message = ""
  if @type == :all
    state_spacer = ""
    @reports.keys.sort.reverse.each do |state|
      message << state_spacer
      message << state_header(state)
      message << @reports[state].join(", ")
      state_spacer = ". "
    end
  else
    message << state_header(state)
    message << @reports[@state].join(", ")
  end

  @state = 3 if @state == -1
  message = "Check gathered no information" if message == ""

  exit @state if @silent_exit
  report!(@state,message,true)
end
ok(message) click to toggle source
# File lib/nagios_reporter.rb, line 25
def ok(message)
  report(0,message)
end
ok!(message) click to toggle source
# File lib/nagios_reporter.rb, line 28
def ok!(message)
  report!(0,message)
end
report(state,message) click to toggle source
# File lib/nagios_reporter.rb, line 80
def report(state,message)
  # update global state if new state is higher
  @state = @state > state ? @state : state
  # add to list of reports of given state
  @reports[state] ||= []
  @reports[state].push(message)
end
report!(state,message,no_state_header=false) click to toggle source
# File lib/nagios_reporter.rb, line 74
def report!(state,message,no_state_header=false)
  # report with passed state and message immediately then exit
  print state_header(state) unless no_state_header
  puts message
  exit state
end
set_type(type=:all) click to toggle source
# File lib/nagios_reporter.rb, line 11
def set_type(type=:all)
  if type == :all or type == :worst_only
    @type = type
  else
    unknown!("Invalid reporter type, #{type}, use :all or :worst_only")
  end
end
silence() click to toggle source
# File lib/nagios_reporter.rb, line 18
def silence
  @silent_exit = true
end
silence!() click to toggle source
# File lib/nagios_reporter.rb, line 21
def silence!
  @silent_exit = true
  final_report
end
state_header(state) click to toggle source
# File lib/nagios_reporter.rb, line 49
def state_header(state)
  message_header = {0 => "OK: ", 1 => "WARNING: ", 2 => "CRITICAL: ", 3 => "UNKNOWN: "}
  message_header[state]
end
unknown(message) click to toggle source
# File lib/nagios_reporter.rb, line 43
def unknown(message)
  report(3,message)
end
unknown!(message) click to toggle source
# File lib/nagios_reporter.rb, line 46
def unknown!(message)
  report!(3,message)
end
warning(message) click to toggle source
# File lib/nagios_reporter.rb, line 31
def warning(message)
  report(1,message)
end
warning!(message) click to toggle source
# File lib/nagios_reporter.rb, line 34
def warning!(message)
  report!(1,message)
end