Class: Reporter
- Inherits:
-
Object
- Object
- Reporter
- Defined in:
- lib/nagios_reporter.rb
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #critical(message) ⇒ Object
- #critical!(message) ⇒ Object
- #final_report ⇒ Object
-
#initialize ⇒ Reporter
constructor
A new instance of Reporter.
- #ok(message) ⇒ Object
- #ok!(message) ⇒ Object
- #report(state, message) ⇒ Object
- #report!(state, message, no_state_header = false) ⇒ Object
- #set_type(type = :all) ⇒ Object
- #silence ⇒ Object
- #silence! ⇒ Object
- #state_header(state) ⇒ Object
- #unknown(message) ⇒ Object
- #unknown!(message) ⇒ Object
- #warning(message) ⇒ Object
- #warning!(message) ⇒ Object
Constructor Details
#initialize ⇒ Reporter
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
#state ⇒ Object (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() report(2,) end |
#critical!(message) ⇒ Object
40 41 42 |
# File 'lib/nagios_reporter.rb', line 40 def critical!() report!(2,) end |
#final_report ⇒ Object
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 = "" if @type == :all state_spacer = "" @reports.keys.sort.reverse.each do |state| << state_spacer << state_header(state) << @reports[state].join(", ") state_spacer = ". " end else << state_header(state) << @reports[@state].join(", ") end @state = 3 if @state == -1 = "Check gathered no information" if == "" exit @state if @silent_exit report!(@state,,true) end |
#ok(message) ⇒ Object
25 26 27 |
# File 'lib/nagios_reporter.rb', line 25 def ok() report(0,) end |
#ok!(message) ⇒ Object
28 29 30 |
# File 'lib/nagios_reporter.rb', line 28 def ok!() report!(0,) end |
#report(state, message) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/nagios_reporter.rb', line 80 def report(state,) # 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() 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,,no_state_header=false) # report with passed state and message immediately then exit print state_header(state) unless no_state_header puts 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 |
#silence ⇒ Object
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) = {0 => "OK: ", 1 => "WARNING: ", 2 => "CRITICAL: ", 3 => "UNKNOWN: "} [state] end |
#unknown(message) ⇒ Object
43 44 45 |
# File 'lib/nagios_reporter.rb', line 43 def unknown() report(3,) end |
#unknown!(message) ⇒ Object
46 47 48 |
# File 'lib/nagios_reporter.rb', line 46 def unknown!() report!(3,) end |
#warning(message) ⇒ Object
31 32 33 |
# File 'lib/nagios_reporter.rb', line 31 def warning() report(1,) end |
#warning!(message) ⇒ Object
34 35 36 |
# File 'lib/nagios_reporter.rb', line 34 def warning!() report!(1,) end |