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:


function die {
  echo $1;
  exit 1;

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

if [[ $# -eq 1 ]] ; then

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


Nagios – run checks as root with NRPE

I’ve few Nagios checks that require root privileges but running nrpe as root user is not acceptable. I prefer to use sudo for only these few commands.

Run visudo and coment out this line:

#Defaults    requiretty

This change is crucial to get scripts working.

Then add at the end of file:

%nrpe ALL=(ALL) NOPASSWD: /usr/lib64/nagios/plugins/

I’ve used nrpe group, but you have to add exactly group that your nrpe process uses.

Now you should be able to run checks as root – edit /etc/nagios/nrpe.cfg and add check like this:

command[check_as_root]=/usr/bin/sudo /usr/lib64/nagios/plugins/check_with_root_privileges