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
}

Comments

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

  1. Luthiano Trarbach Avatar
    Luthiano Trarbach

    Thanks Konstantin , i looks for some simple, to understand the engine of NRPE, you got this in your post.

    Very thanks

    Luthiano Trarbach

  2. […] I’m trying to get a response from Nagios by using the following Python code and instructions: http://skipperkongen.dk/2011/12/06/hello-world-plugin-for-nagios-in-python/ […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.