How to Create Nagios Plugins With Python On Ubuntu 12.10

Introduction

 

Nagios is an open source monitoring software application that helps organizations to prevent IT infrastructure problems. It can be used with Linux as well as other Unix variants.

In this tutorial, you will learn how to create Nagios plugins with Phython. Here, I will be using Ubuntu 12.10.
 

Initial Set Up

 

Before getting into the creation of Nagios plugins, you have to first install NRPE on client VPS. NRPE stands for Nagios Remote Plugin Executor.

NRPE can be installed using the following command:

apt-get install -y python nagios-nrpe-server

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

 

Creation of Phython Script

 

Phython is a common command processor that is available with Linux by default. Here, we will be creating a script which will check disk usage and throw alert if it is over 85%.

 

The script will look like this:

#!/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)

 

This script has to be saved in the same directory as other Nagios plugins, /usr/lib/nagios/plugins/ checkdiskspace.py

Make it executable:

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

You can customize the script to apply different logic to trigger alerts and exit codes or return codes.

Here is the list of Nagios Return Codes:

Exit Codes / Return Codes Status
0 OK
1 WARNING
2 CRITICAL
3 UNKNOWN

 

Configure NRPE

 

Now that we are done creating the script, we have to add the script to nrpe configuration /etc/nagios/nrpe.cfg on client host. For that, we will first delete the original config file (/etc/nagios/nrpe.cfg) and update it with the following lines of code:

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[checkdiskspace_python]=/usr/lib/nagios/plugins/ checkdiskspace.py

You have to specify the correct values for allowed_hosts, which should be the IP of monitoring server.

Restart the Nagios nrpe service:

service nagios-nrpe-server restart

 

Set Up on Nagios Monitoring Server

 

 

In this step, we will add a new command to check Nagios on monitoring server. For that, we have to  update the commands file /etc/nagios/objects/commands.cfg.

 

define command{

command_name    checkdiskspace_python

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

}

This will use nrpe to make TCP connections to port 5666 and will run the command ‘checkdiskspace_python’ which we already defined in /etc/nagios/nrpe.cfg on remote host.

Now we will add this check to Nagios configuration file for client VPS. In this tutorial, we will be using ‘UbuntuDroplet’ as server for monitoring and will update /etc/nagios/servers/UbuntuDroplet.cfg with following lines of code:

define service {

use                             generic-service

host_name                       UbuntuDroplet

service_description             Custom Disk Checker In Python

check_command                   checkdiskspace_python

}

Once the changes are done, you should restart the Nagios:

service nagios restart

Verify whether the new check is working or not:

 

nagios-ubuntu-python

That’s it!

support2 has written 111 articles

Leave a Reply