Writing a Nagios plugin with PHP

For those of you who don’t know, Nagios is a simple and powerful monitioring tool. Nagios can call external commands at set intervals to check for errors and then escalate or alert on the errors. Here is a quick example of writing a PHP plugin for Nagios.

Create your PHP file named “check_php_test.php” with the following contents

\<?php
	fwrite(STDOUT, 'This check passed');
	exit(0);

This will output the “This check passed” string to Nagio with a status code of zero. Of course you will want to customize this to determine your OK and Error states. The available status codes are

0  = OK	or UP
1 = WARNING UP or DOWN/UNREACHABLE
2 = CRITICAL or DOWN/UNREACHABLE
3 = UNKNOWN or DOWN/UNREACHABLE

Now edit your commands.cfg and add a new command for the PHP plugin we will create

define command{
    command_name    check-php-test
    command_line    php /your/path/check_php_test.php
}

Add a service to execute this command in your host config

define service{
    use                             local-service ; Name of service template to use
    host_name                       localhost
    service_description             PHP_ERROR
    check_command                   check-php-test
    check_interval                  5 ; check every 5 minutes (if you havent changed your time units)
    register                        1
    active_checks_enabled           1
    retry_interval                  1 ; retry every minute when in an error state
    max_check_attempts              4 ; test 4 times before deciding the hard state
    contact_groups                  admins ; contact the admin with errors
}

Restart Nagios and monitor your shit.

Let's work together.

If you're interested in working together email me at ben@benedmunds.com with details and I'll be happy to connect.


Find me on ...