Hello world plugin for Nagios in Python

Nagios looks at 1) return codes and 2) output to stdout. This is the hello world of Nagios plugins, written in Python:

check_helloworld.py:

#!/usr/bin/env python
 
# optparse is Python 2.3 - 2.6, deprecated in 2.7
# For 2.7+ use http://docs.python.org/library/argparse.html#module-argparse
from optparse import OptionParser
 
# CONSTANTS FOR RETURN CODES UNDERSTOOD BY NAGIOS
# Exit statuses recognized by Nagios
UNKNOWN = -1
OK = 0
WARNING = 1
CRITICAL = 2
 
# TEMPLATE FOR READING PARAMETERS FROM COMMANDLINE
parser = OptionParser()
parser.add_option("-m", "--message", dest="message", 
   default='Hello world', help="A message to print after OK - ")
(options, args) = parser.parse_args()
 
# RETURN OUTPUT TO NAGIOS
# USING THE EXAMPLE -m PARAMETER PARSED FROM COMMANDLINE
print 'OK - %s' % (options.message)
raise SystemExit, OK

Define a command that uses this plugin:

define command {
    command_name    check_hello_world
    command_line    $USER1$/check_hello_world.py -m 'some message'
}

Check a host or service using the command:

define service {
    use         generic-service
    host_name           some-host
    service_description Check using the hello world plugin (always returns OK)
    check_command       check_hello_world
}

2 Replies to “Hello world plugin for Nagios in Python”

Leave a Reply