Page 1 of 1

Service Handler doesnt seem to get right fields from macros

Posted: Thu Aug 16, 2018 2:20 pm
by jkelly1959
We have successfully implement a Host event handler and are no looking to implement a service event handler to issue a command to send the event to a different event manager. When I try to use the macro $SERVICEDESC$ it seems to break the actual description up across $SERVICEDESC$ $SERVICEOUTPUT$ and $SERVICEGROUPNAMES$. I echo each parm to a file during execution.

Here is the raw definition of the service:

Code: Select all

define service {
    host_name                sabudtrhqttc001.cinf.net
    service_description      Drive F: Disk Usage
    use                      xiwizard_windowsserver_nsclient_service
    check_command            check_xi_service_nsclient!B0az!USEDDISKSPACE!-l F -w 80 -c 95!!!!!
    max_check_attempts       5
    check_interval           5
    retry_interval           1


    check_period             xi_timeperiod_24x7
    event_handler            ServiceIssue_Event_Handler
    event_handler_enabled    1
    notification_interval    60
    notification_period      xi_timeperiod_24x7
    contacts                 nagiosadmin
    _xiwizard                windowsserver
    register                 1
The command we created the ServiceIssue_Event_Handler


Code: Select all

$USER1$/event_handler_SERVICEISSUE.sh $HOSTNAME$ $HOSTADDRESS$ $SERVICESTATE$ $SERVICEPROBLEMID$ $LASTSERVICEPROBLEMID$ $SERVICEDESC$ $LONGSERVICEOUTPUT$ $SERVICEGROUP$ $SERVICEGROUPNAMES$
Slightly modified the XI_service_event_handler
to include extra macros:

Code: Select all

[/usr/bin/php /usr/local/nagiosxi/scripts/handle_nagioscore_event.php --handler-type=service --host="$HOSTNAME$" --service="$SERVICEDESC$" --hostaddress="$HOSTADDRESS$" --hoststate=$HOSTSTATE$ --hoststateid=$HOSTSTATEID$ --hosteventid=$HOSTEVENTID$ --hostproblemid=$HOSTPROBLEMID$ --servicestate=$SERVICESTATE$ --servicestateid=$SERVICESTATEID$ --lastservicestate=$LASTSERVICESTATE$ --lastservicestateid=$LASTSERVICESTATEID$ --servicestatetype=$SERVICESTATETYPE$ --currentattempt=$SERVICEATTEMPT$ --maxattempts=$MAXSERVICEATTEMPTS$ --serviceeventid=$SERVICEEVENTID$ --serviceproblemid=$SERVICEPROBLEMID$ --serviceoutput="$SERVICEOUTPUT$" --longserviceoutput="$LONGSERVICEOUTPUT$" --servicedowntime=$SERVICEDOWNTIME$ --lastserviceproblemid=$LASTSERVICEPROBLEMID$ --servicegroupname=$SERVICEGROUPNAME$ --servicegroupnames=$SERVICEGROUPNAMES$	Yes/code]


Here is the script:

[code]#!/bin/bash
#$USER1$/event_handler_SERVICEISSUE.sh  $HOSTNAME$ $HOSTADDRESS$ $SERVICESTATE$ $SERVICEPROBLEMID$ $LASTSERVICEPROBLEMID$ $SERVICEDESC$ $LONGSERVICEOUTPUT$ $SERVICEGROUP$ $SERVICEGROUPNAMES$
set -x
DATE=$(date)
CALLMSEND=/home/nagios/msend/bin/msend
#echo "The host has changed state at $DATE" > /tmp/hostinfo.txt
echo "The Service has changed state at $DATE" > /tmp/Svchandlertest.txt
HOSTNAME=$1
HOSTADDR=$2
ServiceState=$3
ServiceProbId=$4
ServiceLastProbId=$5
ServiceDesc=$6
ServiceOutput=$7
ServiceGroup=$8
ServiceGroupNames=$9
#
# Variable population for testing
#
#HOSTNAME="Johntest.cinfin.com"
#HOSTADDR="1.2.3.4"
#ServiceProbId="789123"
#ServiceLastProbId="789456"
#ServiceState="OK"
#ServiceDesc="URL check"
#ServiceOutput='This is a test from Nagios Serivce Handler'
#SERVICESTATETYPE="Set"
# SERVICEPROBLEMID and HOSTPROBLEMID are unique and are by far the best way to
#track issues detected by Nagios
#SERVICEPROBLEMID=12345
SysContact="Dave.....what are you doing Dave....."

#
#
echo $HOSTNAME >> /tmp/Svchandlertest.txt
echo "HostAddress" >> /tmp/Svchandlertest.txt
echo $HOSTADDR >> /tmp/Svchandlertest.txt 
echo "ServiceDescription" >> /tmp/Svchandlertest.txt
echo $ServiceDesc >> /tmp/Svchandlertest.txt 
echo "ServiceOutput" >> /tmp/Svchandlertest.txt
echo $ServiceOutput >> /tmp/Svchandlertest.txt 
echo "ServiceProlemID" >> /tmp/Svchandlertest.txt
echo $ServiceProbId >> /tmp/Svchandlertest.txt
echo "LastServiceProlemID" >> /tmp/Svchandlertest.txt
echo $ServiceLastProbId >> /tmp/Svchandlertest.txt
echo "ServiceGROUPNAMES" >> /tmp/Svchandlertest.txt
echo $ServiceGROUPNAMES >> /tmp/Svchandlertest.txt 
echo "ServiceGroupNames" >> /tmp/Svchandlertest.txt
echo $ServiceGroupNames >> /tmp/Svchandlertest.txt 
echo "SysContact" >> /tmp/Svchandlertest.txt
echo $SysContact >> /tmp/Svchandlertest.txt
#
#
# The message below is a mock up. You will need to edit the format to one
#acceptable to your ticketing system
#if [[ "$HOSTSTATETYPE" == "Set" && "$SERVICESTATE" == "DOWN" ]]; then
#msend -n @corpnmshqpcf010.cinfin.com:1832#mc -a  PATROL_EV -b "msg=$SERVICEDESC ; severity=CRITICAL; mc_host=$HOSTNAME; mc_host_address='1.2.3.4';" 
#exit 0
#else
#exit 0
#fi
case "$ServiceState" in
OK)
#msend -n @corpnmshqpcf010.cinfin.com:1832#mc -a  PATROL_EV -b "msg=$SERVICEDESC ; severity=OK; mc_host=$HOSTNAME; mc_host_address='1.2.3.4';"
$CALLMSEND -n @corpnmshqpcf010.cinfin.com:1832#mc -a  Nagios -b "Nagios_service=$ServiceDesc; Nagios_msg=$ServiceOutput; severity=OK; Nagios_host=$HOSTNAME; Nagios_address=$HOSTADDR;Nagios_SvcProbId=$ServiceProbId;Nagios_SvcLastProbId=$ServiceLastProbId;Nagios_Clearable=CLEAR;Nagios_SysContact=$SysContact;"
echo "OK sent to BEM" >> /tmp/Svchandlertest.txt
exit 0
;;
WARNING)
#msend -n @corpnmshqpcf010.cinfin.com:1832#mc -a  PATROL_EV -b "msg=$SERVICEDESC ; severity=WARNING; mc_host=$HOSTNAME; mc_host_address='1.2.3.4';"
$CALLMSEND -n @corpnmshqpcf010.cinfin.com:1832#mc -a  Nagios -b "Nagios_service=$ServiceDesc; Nagios_msg=$ServiceOutput; severity=WARNING; Nagios_host=$HOSTNAME; Nagios_address=$HOSTADDR;Nagios_Nagios_SvcProbId=$ServiceProbId;Nagios_SvcTLastProbId=$ServiceLastProbId;Nagios_Clearable=SET;Nagios_SysContact=$SysContact;"
exit 0
;;
CRITICAL)
#msend -n @corpnmshqpcf010.cinfin.com:1832#mc -a  PATROL_EV -b "msg=$SERVICEDESC ; severity=CRITICAL; mc_host=$HOSTNAME; mc_host_address='1.2.3.4';"
$CALLMSEND -n @corpnmshqpcf010.cinfin.com:1832#mc -a  Nagios -b "Nagios_service=$ServiceDesc; Nagios_msg=$ServiceOutput; severity=CRITICAL; Nagios_host=$HOSTNAME; Nagios_address=$HOSTADDR;Nagios_SvcProbId=$ServiceProbId;Nagios_SvcLastProbId=$ServiceLastProbId;Nagios_Clearable=SET;Nagios_SysContact=$SysContact;"
echo "Critical Alert send to BEM" >> /tmp/Svchandlertest.txt
exit 0
;;
UNKNOWN)
exit 2
;;
esac
#print "Sent to BEM...\n";
It kicks off as it should be the data from the echo's appears off. Here is a sample output.

Code: Select all

The Service has changed state at Thu Aug 16 13:00:55 EDT 2018
sabudtrhqttc001.cinf.net
HostAddress
sabudtrhqttc001.cinf.net
ServiceDescription
Drive
ServiceOutput

ServiceProlemID
1145
LastServiceProlemID
1144
ServiceGROUPNAMES

ServiceGroupNames
Usage
SysContact
Dave.....what are you doing Dave.....
Critical Alert send to BEM

I did expect data in the group names macros but it looks like in broke up the description as I mentioned.
Can anyone see what I might have done wrong here? Maybe I have been looking at this too long.

Thanks
JK

Re: Service Handler doesnt seem to get right fields from mac

Posted: Thu Aug 16, 2018 2:40 pm
by scottwilkerson
Anything that could contain spaces or special chars should be wrapped in quotes, such as

Code: Select all

$USER1$/event_handler_SERVICEISSUE.sh "$HOSTNAME$" "$HOSTADDRESS$" $SERVICESTATE$ $SERVICEPROBLEMID$ $LASTSERVICEPROBLEMID$ "$SERVICEDESC$" "$LONGSERVICEOUTPUT$" "$SERVICEGROUP$" "$SERVICEGROUPNAMES$"

Re: Service Handler doesnt seem to get right fields from mac

Posted: Mon Aug 20, 2018 7:16 am
by jkelly1959
That did the trick! Thank you!

Re: Service Handler doesnt seem to get right fields from mac

Posted: Mon Aug 20, 2018 9:38 am
by scottwilkerson
jkelly1959 wrote:That did the trick! Thank you!
great

Locking