How to Programmatically Cancel Scheduled Downtime in Nagios 3.5.1 Using Command Line Interface


2 views

When managing Nagios 3.5.1 in production environments, we often schedule maintenance windows for system updates. The default 2-hour downtime window becomes problematic when maintenance completes earlier - leaving alerting disabled unnecessarily. Here's how to proactively cancel scheduled downtime through Nagios' external command interface.

First, check currently active downtimes using the downtime file or web interface:

# View current downtimes
cat /usr/local/nagios/var/retention.dat | grep "downtime {" -A 8

Or through the web UI: Status → Scheduled Downtime (shown in your screenshots). Note the downtime_id for the entry you want to cancel.

Nagios processes external commands through the command file. Here's the exact syntax to cancel downtime:

# Format for cancelling host downtime
echo "[TIMESTAMP] DEL_HOST_DOWNTIME;DOWNTIME_ID" > /usr/local/nagios/var/rw/nagios.cmd

# Example for cancelling downtime ID 12345
echo "[$(date +%s)] DEL_HOST_DOWNTIME;12345" > /usr/local/nagios/var/rw/nagios.cmd

For WordPress/Drupal maintenance scenarios, create a cancellation script (cancel_downtime.sh):

#!/bin/bash
NAGIOS_CMD_FILE="/usr/local/nagios/var/rw/nagios.cmd"
DOWNTIME_ID=$1

if [ -z "$DOWNTIME_ID" ]; then
  echo "Usage: $0 <downtime_id>"
  exit 1
fi

TIMESTAMP=$(date +%s)
echo "[$TIMESTAMP] DEL_HOST_DOWNTIME;$DOWNTIME_ID" > $NAGIOS_CMD_FILE
logger "Cancelled Nagios downtime ID $DOWNTIME_ID"

Check the nagios.log for confirmation:

tail -f /usr/local/nagios/var/nagios.log | grep -i "downtime"

You should see entries like:
[1620000000] EXTERNAL COMMAND: DEL_HOST_DOWNTIME;12345
[1620000001] Caught SIGCHLD: A command was executed (PID=1234)

For newer Nagios installations with API access:

curl -XPOST "http://nagios.example.com/nagios/cgi-bin/cmd.cgi" \
  -d "cmd_typ=79" \
  -d "cmd_mod=2" \
  -d "down_id=12345" \
  -u "nagiosadmin:password"

• Command file location may vary (/var/lib/nagios3/rw/nagios.cmd on Debian)
• The nagios.cmd pipe requires proper permissions (usually nagios:nagios)
• For service-specific downtime, use DEL_SVC_DOWNTIME instead
• Changes take effect after the next Nagios event cycle (typically 30 seconds)


When managing maintenance windows in Nagios 3.5.1, scheduled downtime is typically set using either:

  • The web interface (as shown in your screenshots)
  • Command-line tools via external commands

From the Nagios web console:


1. Navigate to "Scheduled Downtime" under the "System" menu
2. Locate your active downtime entry (filter by host/service if needed)
3. Click the "Delete" icon (trash can) next to the downtime entry
4. Confirm the deletion in the pop-up dialog

For scriptable solutions or remote administration, use the external command file:


# Syntax for canceling host downtime
echo "[TIMESTAMP] DEL_HOST_DOWNTIME;DOWNTIME_ID" >> /usr/local/nagios/var/rw/nagios.cmd

# Example for canceling downtime ID 1234
echo "date +%s DEL_HOST_DOWNTIME;1234" >> /var/lib/nagios3/rw/nagios.cmd

To programmatically retrieve active downtime IDs:


# Query current downtimes (JSON output format)
curl -s "http://nagios-server/nagios/cgi-bin/statusjson.cgi?query=downtimelist" | jq '.data.downtimelist[] | select(.is_in_effect=="1")'

For Drupal/WordPress maintenance scenarios:


<?php
function cancel_nagios_downtime($hostname) {
    $downtime_id = shell_exec("curl -s 'http://nagios/nagios/cgi-bin/statusjson.cgi?query=downtimelist&hostname=$hostname' | jq -r '.data.downtimelist[] | select(.is_in_effect==\"1\") | .id'");
    
    if (!empty($downtime_id)) {
        $cmd = "echo \"date +%s DEL_HOST_DOWNTIME;$downtime_id\" >> /var/lib/nagios3/rw/nagios.cmd";
        shell_exec($cmd);
        return "Cancelled downtime ID: $downtime_id";
    }
    return "No active downtime found";
}
?>

After executing either method, verify success by:

  • Checking the web interface's "Downtime" view
  • Reviewing nagios.log for the cancellation event
  • Running service nagios status to confirm no errors