问题
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
回答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
回答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"
回答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
andUSER
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
回答4:
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.
来源:https://stackoverflow.com/questions/6842683/how-to-set-downtime-for-any-specific-nagios-host-for-certain-time-from-commandli