How to set downtime for any specific nagios host for certain time from commandline through curl?

17,295

Solution 1

I enhanced Anders answer to provide a complete script and to not require the use of a newer curl that supports --data-urlencode. This also automatically calculates the end time to send and checks that the request was successfully submitted to Nagios. Also, this schedules downtime for the host and all the services on the host.

#!/bin/bash

function die {
  echo $1;
  exit 1;
}

echo Scheduling downtime on nagios

HOST=monitoredhost
NAGURL=https://nagios.example.com/cgi-bin/nagios3/cmd.cgi
USER=nagiosuser
PASS=nagiospassword
MINUTES=10

export MINUTES

# The following is urlencoded already
STARTDATE=`date "+%Y-%m-%d+%H%%3A%M%%3A%S"`
# This gives us the date/time X minutes from now
ENDDATE=`date "+%Y-%m-%d+%H%%3A%M%%3A%S" -d "$MINUTES min"`
curl --silent --show-error \
    --data cmd_typ=86 \
    --data cmd_mod=2 \
    --data host=$HOST \
    --data "com_data=Updating+application" \
    --data trigger=0 \
    --data "start_time=$STARTDATE" \
    --data "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 contact nagios";

echo Scheduled downtime on nagios

Solution 2

You can send multiple form field values with curl simply by adding more --data(-d) arguments. This should schedule service downtime on a Nagios system:

curl \
    --data cmd_typ=56 \
    --data cmd_mod=2 \
    --data host=$HOSTNAME \
    --data-urlencode "service=${SERVICENAME}" \
    --data-urlencode "com_data=${COMMENT}" \
    --data trigger=0 \
    --data-urlencode "start_time=2011-07-31 00:00:00" \
    --data-urlencode "end_time=2011-07-31 01:00:00" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data btnSubmit=Commit \
    $NAGIOS-URL "username:password"

Solution 3

I further enhanced Sarels answer.

  • made it work with our Nagios 3.5.1 (changed cmd_typ, added childoptions, changed date format).
  • made HOST and USER a command line arg
  • using $USER (current user) as default
  • added password prompt (no hardcoded password)
  • added author to nagios message

My version:

#!/bin/bash

# Bash script to schedule downtime for Host
# source: http://stackoverflow.com/a/9198181
# Author: Sarel Botha http://stackoverflow.com/users/35264/

function die {
  echo $1;
  exit 1;
}

if [ $# -lt 1 ]; then
  echo "$0 <host> [<user>]"
  exit 0;
elif [ $# -ge 2 ]; then
  USER=$2
fi

HOST=$1
NAGURL=https://nagios.example.com/nagios3/cgi-bin/cmd.cgi
MINUTES=30

echo Scheduling downtime on nagios for $HOST

export MINUTES

# read password
read -s  -p "Password for $USER:" PASS
echo ""  # newline after prompt

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

echo Scheduled downtime on nagios
Share:
17,295
unixbhaskar
Author by

unixbhaskar

GNU/Linux Consultant and Administrator

Updated on June 17, 2022

Comments

  • unixbhaskar
    unixbhaskar almost 2 years

    I need to set a schedule downtime for specific nagios host from the commandline by curl..how do I do that?

    here is something I am already using for service/host notification enable/disable from commandline.

    curl -d "some input here" url "user:pass" 
    

    Like way I need to do the thing for schedule downtime.Now the problem is that downtime option takes more options i.e starttime,endtime,comment etc.

    So how do I get it done by curl from the commandline?

    curl -d " some key value pair(hostname,servicename" url "username:passowrd"
    

    which will do the service/host notification on and off from the commandline. So I want use curl in this fashion to provide downtime for specific nagios server.

    Above script is not working for it because downtime option of nagios taked more parameter and I tried to infuse those in the script..but it didn't work out that way.We need provide starttime,endtime and comment value too.

    Plus I have tried curl's option called --form and --form-string with that script..not able to get through.

    The besic idea is instead of going to the Nagios web interface, we want to done this thing from the command line(we have succeded for service/host service and notification).

    Hope I am absolutely clear now.

    TIA

    Bhaskar

  • unixbhaskar
    unixbhaskar over 12 years
    Thanks Anders...let me try that and let you know about the outcome.
  • unixbhaskar
    unixbhaskar over 12 years
    Andres .where have you find the data-urlencode option? I haven't found it on curl manual..
  • unixbhaskar
    unixbhaskar over 12 years
    Can you suggest me how can I go about it without url-encode? light me. I am specifically running curl 7.15 on RHEL box...and not really want to upgrade at this moment...
  • Anders Lindahl
    Anders Lindahl over 12 years
    You can get around that by url-encoding the values using some other means. There are plenty of ways to do this, check out the answers to this question: stackoverflow.com/questions/296536/urlencode-from-a-bash-scr‌​ipt
  • Sarel Botha
    Sarel Botha over 10 years
    anonymous, thanks for the tip that allows us to remove the dependency on Perl.
  • Dan Garthwaite
    Dan Garthwaite about 9 years
    Works with icinga. Just make sure to change the grep verification string, and of course the URL.