Writing your own Nagios plugin in PHP

In second place in the 2013/2014 Teach something, Win something contest we find Maarten Kossen, administrator of LowendBox.com. His guide on creating a Nagios plugin in PHP is a follow up on a series of Nagios tutorials Maarten wrote on LowendBox. Hope you will enjoy it and that you can use it as a starting point towards your own creations .

Lets get going!



One of the things you are eventually going to run in to when running Nagios, is that you may want to check something that isn’t covered by a default or existing Nagios plugin. For example, checking the status of one of your own PHP scripts, the size or a certain folder, or whether a cron job was executed.

In order to tackle this problem, you will need to write your own plugin. While this is often thought of as hard, it is actually quite simple. I am going to show you how to write your own Nagios plugin in PHP. For this tutorial you need to have a working Nagios installation and knowledge on how to add a Nagios check. This guide will limit to writing the actual check and returning a proper value. Setting this up in Nagios is not covered. See http://lowendbox.com/blog/remote-server-monitoring-with-nagios/ for more information on how to do that.

You need the Debian/Ubuntu package ‘php5-cli’ installed for this tutorial to work, or the equivalent of that package on CentOS or any other Linux distribution. Knowdledge of PHP may also help, but it’s not that hard in this case. Other than that, there are no requirements.

In any examples I use, I assume a recent Ubuntu (12.04 or higher) or Debian (7 or higher) version.

The script
I’m going to show you to create a test to see if a file is actually present. While this may sound as a bit of a lame check, if could very well be useful. You could expand it to check the timestamp of a file, to see if it has changed, or to see if a certain ‘lock file’ is present. Besides that, it makes the script easy and comprehendable in my examples.

Create a file check_file_exists in the folder /usr/lib/nagios/plugins/. Next, set the right permissions on it:

This ensure the file is executable. Now, open the file, and start with this:

What I’ve done here, is first let bash know that this is a PHP script and the current environment’s default php-binary should be used to execute it (which defaults to the system-wide php-binary).

Next, I’ve defined four constants. Those “keywords” can be used later in the script. I’ve created four because there is one for each Nagios status/exit code:

We will use be using OK and CRITICAL in this example.

Now, let’s add the following lines to the script:

I’m going to go over this line by line. The first line,checks whether the file exists. If it does, it enters that code block:

Inside the code block, I first echo the “OK” status and a new line after that. After that, I _exit_ the script with a constant (STATE_OK in this case). This output and the constant (which is linked to an exit code) will be picked up by Nagios or passed on to it by NRPE. It is important the status code is the exit code of the script (and not echoed by it), otherwise Nagios won’t pick it up. So, basically if the file is present, the plugin outputs “OK” and a new line, after which is exists with code 0. Nagios will appreciate that.

But what is the file is not there? Well, in my specific example, you would be in more trouble, as the default shell is gone. But let’s not dive into that, let’s focus on returning that data to Nagios:

In this case an actual error string is output, describing what is wrong. Again, with a new line following it. Nagios somehow seems to require this. After that, I exit with code 2 (STATE_CRITICAL) to tell Nagios something is wrong. Nagios will not appreciate this and start to toss errors at you.

So, that’s it! This is how simple it is to create a Nagios check! You can do this in any language you master, of course. As long as it outputs a string, followed by a new line, and exits with an appropriate exit code.

Happy coding!

3 comments for “Writing your own Nagios plugin in PHP

Leave a Reply

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

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