Writing a Nagios plugin with Javascript using Node.js

This is pretty much an exact copy of my Writing a Nagios plugin with PHP post modified for Javascript. So, here is a quick example of writing a Javascript plugin for Nagios using Node.js.

Create your JS file named “check_js_test.js” with the following contents

process.stdout.write('This check passed');
process.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-js-test
    command_line    node /your/path/check_js_test.js
}

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-js-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.

Get In Touch.

If you'd like to get in touch, you can reach me at ben@benedmunds.com.


Find me on ...