How to Execute Nagios Checks Manually via Command Line for Service Testing


1 views

When developing new Nagios service checks, repeatedly restarting the Nagios daemon and waiting for scheduled checks is inefficient. The proper approach involves directly invoking the check plugins from command line.

First identify where your Nagios plugins are installed. Common locations include:

/usr/lib/nagios/plugins/
/usr/local/nagios/libexec/
/opt/nagios/libexec/

Basic command structure:

./check_plugin_name -argument1 value1 -argument2 value2

For example, to test an HTTP service:

./check_http -H example.com -p 443 -S -u /health

Nagios plugins follow standardized exit codes:

0 = OK
1 = WARNING
2 = CRITICAL
3 = UNKNOWN

Match the exact parameters from your service definition. For example, if your service is defined as:

define service {
    service_description    Disk Space
    check_command          check_nrpe!check_disk!-a '-w 20% -c 10% -p /dev/sda1'
}

The manual test would be:

./check_nrpe -H hostname -c check_disk -a '-w 20% -c 10% -p /dev/sda1'

For systems where active checks aren't possible:

echo "hostname|service_name|return_code|plugin_output" | send_nsca -H nagios_server -c /path/to/send_nsca.cfg

Create a test script to validate multiple checks:

#!/bin/bash
CHECKS=(
    "./check_http -H web1.example.com -w 2 -c 5"
    "./check_ssh -H db1.example.com -p 2222"
)

for check in "${CHECKS[@]}"; do
    echo "Running: $check"
    $check
    echo "Exit code: $?"
    echo
done
  • Use -v for verbose output where supported
  • Check plugin permissions with ls -l
  • Test with sudo -u nagios to match runtime context
  • Capture output with 2>&1 | tee test.log

When developing or troubleshooting Nagios configurations, manually triggering service checks can significantly accelerate your workflow. Instead of waiting for scheduled checks or restarting the Nagios daemon, you can leverage Nagios' built-in command line utilities.

The most direct approach is using the nagios command with the -v (verify) and -C (check) flags:

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg -C /usr/local/nagios/var/objects.cache

Each Nagios check is essentially a standalone executable. You can run plugins directly with their full path:

/usr/lib/nagios/plugins/check_http -H example.com -p 443 -S

For distributed monitoring, configure passive checks and submit results manually:

echo "hostname;service description;0;OK" | /usr/local/nagios/bin/send_nsca -H nagios-server -c /usr/local/nagios/etc/send_nsca.cfg

When manual checks fail but scheduled ones work:

  • Verify plugin permissions: chmod +x /path/to/plugin
  • Check environment variables: env -i /path/to/plugin
  • Test as nagios user: sudo -u nagios /path/to/plugin

When your SSH service check isn't working as expected:

/usr/lib/nagios/plugins/check_ssh -H remotehost -p 2222 -t 30 -v

This provides detailed output showing connection attempts, timeouts, and authentication failures.

Remember that manual checks:

  • Don't update the web interface status
  • May show different results than scheduled checks due to timing differences
  • Don't trigger notifications unless you manually process them