Introduction

Python is a popular command processor available on Linux by default.

This time, we will expand on this idea and create Nagios plugins using Python.

These plugins will be running on client VPS, and be executed via NRPE.

Step 1 - Install NRPE on client VPS

nagios illustration for: Step 1 - Install NRPE on client VPS
				
					apt-get install -y python nagios-nrpe-server

useradd nrpe && update-rc.d nagios-nrpe-server defaults
				
			

Step 2 - Create your Python Script

It would be a good idea to keep your plugins in same directory as other Nagios plugins (/usr/lib/nagios/plugins/ for example).

For our example, we will create a script that checks current disk usage by calling “df” from shell, and throw an alert if it is over 85% used:

				
					#!/usr/bin/python

import os, sys

used_space=os.popen("df -h / | grep -v Filesystem | awk '{print $5}'").readline().strip()

if used_space < "85%":

        print "OK - %s of disk space used." % used_space

        sys.exit(0)

elif used_space == "85%":

        print "WARNING - %s of disk space used." % used_space

        sys.exit(1)

elif used_space > "85%":

        print "CRITICAL - %s of disk space used." % used_space

        sys.exit(2)

else:

        print "UKNOWN - %s of disk space used." % used_space

        sys.exit(3)
				
			

<img src="images/how-to-create-nagios-plugins-with-python-on-ubuntu-12-10-section-1.png; width="680">

We will save this script in /usr/lib/nagios/plugins/usedspace.py and make it executable:

				
					chmod +x /usr/lib/nagios/plugins/usedspace.py
				
			

The entire Nagios NRPE plugin boils down to using exit codes to trigger alerts.

You introduce your level of logic to the script, and if you want to trigger an alert (whether it is OK, WARNING, CRITICAL, or UNKNOWN) – you specify an exit code.

Refer to the following Nagios Exit Codes:

Nagios Exit Codes

<table border=0 cellpadding=0>

<thead>

<tr>

<td width=120 align=center><B>Exit Code</B></td>

<td align=center><B>Status</B></td>

</tr>

<tr align=center>

<td width=120>0</td>

<td align=center>OK</td>

</tr>

<tr align=center>

<td width=120>1</td>

<td align=center>WARNING</td>

</tr>

<tr align=center>

<td width=120>2</td>

<td align=center>CRITICAL</td>

</tr>

<tr align=center>

<td width=120>3</td>

<td align=center>UNKNOWN</td>

</tr>

</thead>

</table>

Step 3 - Add Your Script to NRPE configuration on client host

Delete original /etc/nagios/nrpe.cfg and add the following lines to it:

				
					log_facility=daemon

pid_file=/var/run/nagios/nrpe.pid

server_port=5666

nrpe_user=nrpe

nrpe_group=nrpe

allowed_hosts=198.211.117.251

dont_blame_nrpe=1

debug=0

command_timeout=60

connection_timeout=300

include_dir=/etc/nagios/nrpe.d/

command[usedspace_python]=/usr/lib/nagios/plugins/usedspace.py
				
			

Where 198.211.117.251 is our monitoring server from previous articles. Change these to your own values.

Make sure to restart Nagios NRPE service:

				
					service nagios-nrpe-server restart
				
			

Step 4 - Add Your New Command to Nagios Checks on Nagios Monitoring Server

Define new command in /etc/nagios/objects/commands.cfg

				
					define command{

        command_name    usedspace_python

        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c usedspace_python

        }
				
			

As you can see, it uses NRPE to make TCP connections to port 5666 and run command ‘usedspace_python’, which we defined in /etc/nagios/nrpe.cfg on that remote host.

Add this check to your Nagios configuration file for client VPS.

For our example, we will monitor a server called UbuntuDroplet and edit /etc/nagios/servers/UbuntuDroplet.cfg

				
					define service {

        use                             generic-service

        host_name                       UbuntuDroplet

        service_description             Custom Disk Checker In Python

        check_command                   usedspace_python

        }
				
			

<img src="images/how-to-create-nagios-plugins-with-python-on-ubuntu-12-10-section-1.png; width="680">

Restart Nagios:

				
					service nagios restart
				
			

Verify that the new check is working:

<img src="images/how-to-create-nagios-plugins-with-python-on-ubuntu-12-10-section-1.png; width="680">

And you are all done!