Page 1 of 1

EventHandler update MySQL query

Posted: Tue May 23, 2017 7:01 am
by Anton @ CPB
Good day

Running a check to see if a port is open on a link;

Code: Select all

#!/bin/bash
# portcheck tool

SERVER=$1
PORT=$2

usage()
{
    echo -e "\n USAGE: ./${0##*/} [host|ip] \n"
    exit
}

check_port()
{
    local host="${SERVER}"
    local port="${PORT}"
    if nc -w 5 -z ${host-ip} ${port} 2>/dev/null
    then
    echo -e "OK - ${host}:${port} is open"
    exit 0
    else
    echo -e "CRITICAL - ${host}:${port} is closed"
    exit 2
    fi
}

[[ $# -ne 2 ]] && usage

check_port ${1} ${2}
Then if it an event happens the eventshandler runs a script to update a mysqldb to set it from "On" to "Off".

Events Handler script;

Code: Select all

#!/bin/bash
ON=$(mysql -hxxx.xxx.xxx.xxx -e"update xxxx.xxxx set Value='On' where Category='ExternalServices' and Param='xxxx';")
OFF=$(mysql -hxxx.xxx.xxx.xxx -e"update xxxx.xxxx set Value='Off' where Category='ExternalServices' and Param='xxxx';")
case "$1" in
OK) $ON
	;;
WARNING) # Don't do anything
	;;
UNKNOWN) # Don't do anything
	;;
CRITICAL)
	case "$2" in
		SOFT)
                        case "$3" in
                                1) # Don't do anything
                                ;;
                                2) # Don't do anything
                                ;;
                                3) # Don't do anything
                                ;;
                        esac
                ;;
                HARD)
                        case "$3" in
                                1) #Only from state 3
                                ;;
                                2) #Only from state 3
                                ;;
                                3) $OFF
                                ;;
                        esac
                        ;;
	esac
	;;
esac
exit 0
But the update query does not run.

Code: Select all

define command{
command_name    xxxx_xxxx
command_line    /usr/local/nagios/libexec/eventhandlers/xxxxxx $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}
Service

Code: Select all

define service{
	    use                             xxxxx-service
        host_name                       apache2
        service_description             xxxxxxxxxxxxx
        check_command                   check_xxxxx!hostname!PortNumber
        event_handler                   xxxx_xxxx
        register                        1
}
Anything I'm missing

nagios has rights to run the mysqlclient query and the mysql command works straight from bash running as nagios.

Re: EventHandler update MySQL query

Posted: Tue May 23, 2017 4:04 pm
by tgriep
You should enable the eventhandler for that service by adding the following to your service check.

Code: Select all

event_handler_enabled	1
This directive is used to determine whether or not the event handler for this service is enabled. Values: 0 = disable service event handler, 1 = enable service event handler.
I think the default setting is disabled so you will have to enable it.

Re: EventHandler update MySQL query

Posted: Tue May 23, 2017 11:48 pm
by Anton @ CPB
Enabled, but query still not running.

Service

Code: Select all

define service{
        use                             xxxxx-service
        host_name                       apache2
        service_description             xxxxxxxxxxxxx
        check_command                   check_xxxxx!hostname!PortNumber
        event_handler                   xxxx_xxxx
        event_handler_enabled           1
        register                        1
}
Log

Code: Select all

May 24 06:38:35 nagios nagios: SERVICE NOTIFICATION: antonsms;apache2;{SERVICE DESCRIPTION};CRITICAL;notify-by-tgn-service;CRITICAL - {HOST}:{PORT} is closed
May 24 06:38:35 nagios nagios: SERVICE EVENT HANDLER: apache2;{SERVICE DESCRIPTION};CRITICAL;HARD;3;{EVENTHANDLER COMMAND NAME}
I can also confirm that it runs after the query I've added

Code: Select all

&& echo "Test from script" >> /tmp/nagiostest.txt
And it writes the text file to /tmp dir.

Re: EventHandler update MySQL query

Posted: Wed May 24, 2017 9:01 am
by tgriep
When event handlers and check commands are executed by Nagios, they are run as the nagios user account.
One test you can do is to change to the nagios user account and run your script. To do that, run the following in a shell. Fill in the macros so the combination needed to get the Value set to On or Off in the MYSQL update command.

Code: Select all

su nagios
/usr/local/nagios/libexec/eventhandlers/xxxxxx $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$

Re: EventHandler update MySQL query

Posted: Wed May 24, 2017 2:11 pm
by Anton @ CPB
tgriep wrote:When event handlers and check commands are executed by Nagios, they are run as the nagios user account.
One test you can do is to change to the nagios user account and run your script. To do that, run the following in a shell. Fill in the macros so the combination needed to get the Value set to On or Off in the MYSQL update command.

Code: Select all

su nagios
/usr/local/nagios/libexec/eventhandlers/xxxxxx $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
Can confirm that it works like that running the script from bash as nagios user.

Re: EventHandler update MySQL query

Posted: Wed May 24, 2017 3:03 pm
by tgriep
Can you run the following commands and post the the output? I need to see the info to continue on.

Code: Select all

ps -ef --cols=300
su nagios
bash -x /usr/local/nagios/libexec/eventhandlers/xxxxxx $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$

Re: EventHandler update MySQL query

Posted: Wed May 24, 2017 11:40 pm
by Anton @ CPB

Code: Select all

nagios@nagios:/root$ bash -x /usr/local/nagios/libexec/eventhandlers/xxxxxx $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
++ mysql -hxxx.xxx.xxx.xxx '-eupdate DATABASE.TABLE set Value='\''On'\'' where Category='\''ExternalServices'\'' and Param='\''{FIELDNAME}'\'';'
+ ON=
++ mysql -hxxx.xxx.xxx.xxx '-eupdate DATABASE.TABLE set Value='\''Off'\'' where Category='\''ExternalServices'\'' and Param='\''{FIELDNAME}'\'';'
+ OFF=
+ case "$1" in
+ exit 0
nagios@nagios:/root$ 

Re: EventHandler update MySQL query

Posted: Thu May 25, 2017 11:08 am
by tgriep
Try changing the event handler script to the following

Code: Select all

#!/bin/bash

echo $1 >>/tmp/event
echo $2 >>/tmp/event
echo $3 >>/tmp/event

case "$1" in
  OK)
     mysql -hxxx.xxx.xxx.xxx -e"update xxxx.xxxx set Value='On' where Category='ExternalServices' and Param='xxxx';"
     ;;
  WARNING) # Don't do anything
     ;;
  UNKNOWN) # Don't do anything
     ;;
  CRITICAL)
    case "$2" in
          SOFT)
                case "$3" in
                  1) # Don't do anything
                    ;;
                  2) # Don't do anything
                    ;;
                  3) # Don't do anything
                    ;;
                  esac
                    ;;
          HARD)
                case "$3" in
                  1) #Only from state 3
                    ;;
                  2) #Only from state 3
                    ;;
                  3)
                    mysql -hxxx.xxx.xxx.xxx -e"update xxxx.xxxx set Value='Off' where Category='ExternalServices' and Param='xxxx';"
                    ;;
                  esac
                    ;;
    esac
    ;;
esac
exit 0
Then run it and see what is passed to the script by looking at the /tmp/event file.
If the scripts starts to function, you can remove these 3 lines from the script.

Code: Select all

echo $1 >>/tmp/event
echo $2 >>/tmp/event
echo $3 >>/tmp/event