Class: HQ::Tools::CheckScript

Inherits:
BaseScript show all
Defined in:
lib/hq/tools/check-script.rb

Overview

This is an abstract base class for creating icinga/nagios plugins.

Create a derived class and override the #process_args, #prepare and #perform_checks methods where you will do your work.

During the #perform_checks method, you should make calls to the methods #message, #warning, #critical and #unknown, with short messages to be added to the final output.

The exit status and full output will be put together for you.

Instance Attribute Summary

Attributes inherited from BaseScript

#args, #status, #stderr, #stdout

Instance Method Summary collapse

Constructor Details

#initializeCheckScript

Creates a new check script.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hq/tools/check-script.rb', line 21

def initialize
  @name = "Unnamed"
  @messages = []
  @critical = false
  @warning = false
  @unknown = false
  @performances = []
  @postscript = []
  @stdout = $stdout
  @stderr = $stderr
end

Instance Method Details

#mainObject

Handle a single invocation of the script. This calls #process_args, #prepare, #perform_checks and #perform_output. It also gracefully handles any exceptions thrown during #prepare and #perform_checks.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hq/tools/check-script.rb', line 37

def main

  process_args

  begin
    prepare
    perform_checks
  rescue => exception

    message = "%s: %s" % [
      exception.class,
      exception.message,
    ]

    critical message

    @postscript << message
    @postscript << exception.backtrace

  end

  perform_output

end