Talking to the API

In many environments I find myself “teaching” employees how to work with a monitoring system: why nagios is sending more then one alert , why they were unable to acknowledge an issue , why they did not get a notification about an event they saw on the dashboard , what the benefit of the scheduled downtime is , and many other things that come with working with nagios.

Almost always the question comes up “is there an automated way we can have to do all those things ?” , well, the answer to this is always “Yes , and No” .

Yes – there is the External commands that are available to communicate with the nagios core process and allow you to submit actions without using the Web Ui .

No – to each command there are somewhat different parameters and you will need to know which ones need to be added for which command , otherwise the request will fail .

So I have been working on trying to allow the user to “simplify” the access to the cgi and wrote a script :

#!/bin/bash

usage (){
echo “usage :sumbit_cgi_option.sh <Host> <Service> <Comment>  <username> <password> <cmd_mod> ”
echo -e -n “The script requires several parameters in order to work :\n”
echo -e -n “\tHost = The short hostname [i.e lmmapp2801].\n”
echo -e -n “\tService = The descriptive service name  [i.e Jave Core Dump], a name containing spaces should be encapsulated in single quotes ‘ ‘ .\n”
echo -e -n “\tComment = A descriptive comment pretainig to the issue. \n”
echo -e -n “\tUsername  = Your Nagios interface login name. \n”
echo -e -n “\tPassword = Your Nagios Interface password.\n”
echo -e -n “\tcmd_mod = should the command run on a host level or a service level :\n”
echo -e -n ” \t\t2 = Service Level (Prefered Method)\n”
echo -e -n ” \t\t1 = Host Level \n”
}

type_host () {
echo -e -n ” CMD_ADD_HOST_COMMENT\t                            1\n”
echo -e -n ” CMD_SCHEDULE_HOST_DOWNTIME        \t              55\n”
}
type_svc(){
echo -n -e ” CMD_ADD_SVC_COMMENT\t                              3\n”
echo -n -e ” CMD_SCHEDULE_SVC_DOWNTIME          \t             56\n”
echo -n -e ” CMD_SCHEDULE_HOST_SVC_DOWNTIME \t                 86\n”
}

if [ $# -ne  6 ] || [ $# -eq 0 ] ;then
usage
exit 1
fi

HOST=$1
SERVICE=$2
COMMENT=$3
NAGURL=http://10.173.4.82/nagios/cgi-bin/cmd.cgi
USER=$4
PASS=$5
CMD_MOD=$6

echo Scheduling downtime on nagios

if [ $6 -eq “1” ] ; then
echo “Enter the Host command type:”
type_host
read CMD_TYPE
if  [ $CMD_TYPE -eq  “55” ]  ; then
echo “Enter the Downtime Length (in minutes):”
read LENGTH
STARTDATE=`date +%d-%m-%Y\ %H:%M:%S`
ENDDATE=`date –date=”+$LENGTH minutes” +%d-%m-%Y\ %H:%M:%S`
curl $NAGURL -u $USER:$PASS –silent –show-error \
–data cmd_typ=$CMD_TYPE \
–data cmd_mod=$CMD_MOD \
–data host=$HOST \
–data “service=$SERVICE”  \
–data “com_data=$COMMENT” \
–data trigger=0 \
–data “start_time=$STARTDATE” \
–data “end_time=$ENDDATE” \
–data fixed=1 \
–data hours=2 \
–data minutes=0 \
–data btnSubmit=Commit | grep -q “Your command request was successfully submitted to Nagios for processing.” ;
else
STARTDATE=`date +%d-%m-%Y\ %H:%M:%S`
curl $NAGURL -u $USER:$PASS –silent –show-error \
–data cmd_typ=$CMD_TYPE \
–data cmd_mod=$CMD_MOD \
–data host=$HOST \
–data “service=$SERVICE”  \
–data “com_data=$COMMENT” \
–data trigger=0 \
–data “start_time=$STARTDATE” \
–data btnSubmit=Commit | grep -q “Your command request was successfully submitted to Nagios for processing.” ;
fi
echo Scheduled downtime for $HOST
exit
fi

if [ $6 -eq “2 ” ] ; then
echo “Enter the Service command type:”
type_svc
read CMD_TYPE
if  [ $CMD_TYPE -eq  “56” ]  || [ $CMD_TYPE -eq  “86” ]; then
echo “Enter the Downtime Length  (in minutes):”
read LENGTH
STARTDATE=`date +%d-%m-%Y\ %H:%M:%S`
ENDDATE=`date –date=”+$LENGTH minutes” +%d-%m-%Y\ %H:%M:%S`
curl $NAGURL -u $USER:$PASS –silent –show-error \
–data cmd_typ=$CMD_TYPE \
–data cmd_mod=$CMD_MOD \
–data host=$HOST \
–data “service=$SERVICE”  \
–data “com_data=$COMMENT” \
–data trigger=0 \
–data “start_time=$STARTDATE” \
–data “end_time=$ENDDATE” \
–data fixed=1 \
–data hours=2 \
–data minutes=0 \
–data btnSubmit=Commit | grep -q “Your command request was successfully submitted to Nagios for processing.” ;
else
STARTDATE=`date +%d-%m-%Y\ %H:%M:%S`
curl $NAGURL -u $USER:$PASS –silent –show-error \
–data cmd_typ=$CMD_TYPE \
–data cmd_mod=$CMD_MOD \
–data host=$HOST \
–data “service=$SERVICE”  \
–data “com_data=$COMMENT” \
–data trigger=0 \
–data “start_time=$STARTDATE” \
–data btnSubmit=Commit | grep -q “Your command request was successfully submitted to Nagios for processing.” ;
fi
echo Scheduled downtime for $SERVICE in host $HOST
exit
fi

I have tested it and it works well , but it still is limited in the functionality it provides so I am thinking of converting this to a PHP page/url that will allow the same functionality , but that will have to wait till i can find the time .