How to create/write Nrpe plugins using BASH/Python

NRPE is a Open source Project from Nagios , which allows you to remotely execute Nagios plugins on other Linux/Unix machines. This is useful to monitor remote machine metrics like CPU, Load, Memory , Disk , IO and many more application metrics. Usually the NRPE service listen on port 5666 and responds to probes from Nagios Server after executing the plugins locally. You can write the nrpe plugins in your favorite language, like bash, python or perl.  Just make sure that the plugins are executable for NRPE service . In this article we will explain how to write Nrpe plugins using BASH/Python. 

We will write a simple plugin to monitor the number of open files on your machine.

Nagios uses following exit codes from the remote executor to trigger the alert

Exit Code Status
0 OK
1 WARNING
2 CRITICAL
3 UNKNOWN

 

Basically,  the plugin exit status decides the state of the service. So we need to keep these exit codes as reference while writing the nrpe plugis

Please see the below script which checks the number of open files in the machine and and throw an alert if it is over the threshold

Please copy this script and keep it under libexec folder, inside Nrpe root folder. Make sure you make the script executable

You can test the script by running it

nrpe-plugin-openfiles

Now add a new check inside the nrpe configuration file . Please add the following lines in the nrpe configuration file

Make sure you restart the nrpe service. Once done, you can invoke this script from your nagios server using following command

Same script can be written in Python also

 

Save this file as ” /usr/local/nagios/libexec/openfiles.py ” and follow the same procedure as of bash script.

As always, feel free to drop us a note if you have any queries or feedbacks using our comment form below. Always happy to help you 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *