Skip to content

Instantly share code, notes, and snippets.

@gardart
Created August 26, 2016 13:50
Show Gist options
  • Save gardart/e5ce447b87c7b97f1eb2c0e73d7a8b10 to your computer and use it in GitHub Desktop.
Save gardart/e5ce447b87c7b97f1eb2c0e73d7a8b10 to your computer and use it in GitHub Desktop.
Nagios - Schedule Downtime from the command line (from any host)
#!/bin/bash
# Bash script to schedule downtime for Host
function die {
echo $1;
exit 1;
}
if [ $# -lt 1 ]; then
echo "$0 <host> [<user>]"
exit 0;
elif [ $# -ge 2 ]; then
USER=$2
PASS=$3
fi
HOST=$1
NAGURL=https://nagiosserver/nagios/cgi-bin/cmd.cgi
MINUTES=30
echo Scheduling downtime on nagios for $HOST
export MINUTES
# read password
if [[ ! $PASS ]]; then
read -s -p "Password for $USER:" PASS
fi
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=scheduled+reboot" \
--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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment