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

为君一笑 提交于 2019-12-04 08:41:06

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

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"

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!