Nagios – downtime on host/service from command line with curl

Sometimes deployment process or other havy task may cause some Nagios checks to rise below normal levels and bother admin. If this is expected and you want to add downtime on host/service during this task you may use this script:

#!/bin/bash

function die {
  echo $1;
  exit 1;
}

if [[ $# -eq 0 ]] ; then
    die "Give hostname and time in minutes as parameter!"
fi

if [[ $# -eq 1 ]] ; then
    MINUTES=15
else
    MINUTES=$2
fi

HOST=$1
NAGURL=http://nagios.example.com/nagios/cgi-bin/cmd.cgi
USER=nagiosuser
PASS=nagiospassword
SERVICENAME=someservice
COMMENT="Deploying new code"

export MINUTES

echo "Scheduling downtime on $HOST for $MINUTES minutes..."

# The following is urlencoded already
#STARTDATE=`date "+%Y-%m-%d %H:%M:%S"`
STARTDATE=`date "+%d-%m-%Y %H:%M:%S"`
# This gives us the date/time X minutes from now
#ENDDATE=`date "+%Y-%m-%d %H:%M:%S" -d "$MINUTES min"`
ENDDATE=`date "+%d-%m-%Y %H:%M:%S" -d "$MINUTES min"`
curl --silent --show-error \
    --data cmd_typ=56 \
    --data cmd_mod=2 \
    --data host=$HOST \
    --data-urlencode "service=$SERVICENAME" \
    --data-urlencode "com_data=$COMMENT" \
    --data trigger=0 \
    --data-urlencode "start_time=$STARTDATE" \
    --data-urlencode "end_time=$ENDDATE" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data btnSubmit=Commit \
    --insecure \
    $NAGURL -u "$USER:$PASS"| grep -q "Your command request was successfully submitted to Nagios for processing." || die "Failed to con
tact nagios";

echo Scheduled downtime on nagios from $STARTDATE to $ENDDATE

Threat this script as template with some tips:

  • I you want to add downtime on service, then provide SERVICENAME and --data cmd_typ=56 \.
  • If you want downtime on whole host, just remove this line: --data-urlencode "service=$SERVICENAME" \ and --data cmd_typ=86 \
  • Another thing that in my example nagios page use basic auth for security, if your don’t use it, you may remove -u "$USER:$PASS" from parameters.
  • If you get Start or end time not valid, then you have to adapt dates to your formats of dates accepted by Nagios (probably this depends on Nagios version or timezone configuration).

Source:
http://stackoverflow.com/questions/6842683/how-to-set-downtime-for-any-specific-nagios-host-for-certain-time-from-commandli

Leave a Reply

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