Class: Reporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



4
5
6
7
8
9
10
# 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

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



2
3
4
# File 'lib/nagios_reporter.rb', line 2

def state
  @state
end

Instance Method Details

#critical(message) ⇒ Object



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

def critical(message)
  report(2,message)
end

#critical!(message) ⇒ Object



40
41
42
# File 'lib/nagios_reporter.rb', line 40

def critical!(message)
  report!(2,message)
end

#final_reportObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



25
26
27
# File 'lib/nagios_reporter.rb', line 25

def ok(message)
  report(0,message)
end

#ok!(message) ⇒ Object



28
29
30
# File 'lib/nagios_reporter.rb', line 28

def ok!(message)
  report!(0,message)
end

#report(state, message) ⇒ Object



80
81
82
83
84
85
86
# 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) ⇒ Object



74
75
76
77
78
79
# 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) ⇒ Object



11
12
13
14
15
16
17
# 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

#silenceObject



18
19
20
# File 'lib/nagios_reporter.rb', line 18

def silence
  @silent_exit = true
end

#silence!Object



21
22
23
24
# File 'lib/nagios_reporter.rb', line 21

def silence!
  @silent_exit = true
  final_report
end

#state_header(state) ⇒ Object



49
50
51
52
# 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) ⇒ Object



43
44
45
# File 'lib/nagios_reporter.rb', line 43

def unknown(message)
  report(3,message)
end

#unknown!(message) ⇒ Object



46
47
48
# File 'lib/nagios_reporter.rb', line 46

def unknown!(message)
  report!(3,message)
end

#warning(message) ⇒ Object



31
32
33
# File 'lib/nagios_reporter.rb', line 31

def warning(message)
  report(1,message)
end

#warning!(message) ⇒ Object



34
35
36
# File 'lib/nagios_reporter.rb', line 34

def warning!(message)
  report!(1,message)
end