6

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

4 Answers 4

6

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
2
  • anonymous, thanks for the tip that allows us to remove the dependency on Perl. Oct 4, 2013 at 14:59
  • Works with icinga. Just make sure to change the grep verification string, and of course the URL. Mar 19, 2015 at 16:38
3

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"
4
  • Thanks Anders...let me try that and let you know about the outcome. Aug 4, 2011 at 7:17
  • Andres .where have you find the data-urlencode option? I haven't found it on curl manual.. Aug 31, 2011 at 10:49
  • 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... Aug 31, 2011 at 12:19
  • 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-script Aug 31, 2011 at 12:55
1

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
0

For this to work on my Nagios, I had to add an extra line under "data host=$HOST"

--data "com_author=Automatic+Downtime" \

Without that, my Nagios wouldn't accept the downtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.