Page 1 of 1

calling script from eventhandler

Posted: Fri Mar 21, 2014 1:05 pm
by vvz
Hello!
Nagios3 is running on CentOS6.5. in /usr/lib64/nagios/plugins/eventhandlers folder I have a script with a name - asteriks-host-phone-call .
As you can see from the script if host is DOWN, SOFT, 3 (for now) email should be sent (later on we are going initiate asteriks phone call instead)
If I call this script from CLI like: /usr/lib64/nagios/plugins/asteriks-host-phone-call DOWN SOFT 3 I'm getting email, that means script itself and mail programme work properly (I'm receiving standard notificatios for the host also)

Code: Select all

#!/bin/bash

# This script  is initiated by host-event-handler

#what state of host?

case "$1" in

OK)
        # The host just came back up, don't do anything
        ;;
UNREACHABLE)
        # we don't really know what might be causing an unknown error, don't do anything
        ;;
DOWN)
        # the host has real problem - we need to do something depends of state

        # is this a HARD or SOFT state?

        case "$2" in

        SOFT)
                #what check attempt? we don't whant to make a call immediately, so we skip SOFT 1 and 2

                case "$3" in

                        3)

                        # 3 -means after 3-d check make a call, of course we can change this number
                        touch ./callismade.txt

                        sudo echo  "THIS MESSAGE FROM EVENTHANDLER" | /bin/mail -s "EVENTHANDLER MESSAGE" [email protected]

                        #instead of echo command we gonna use linphone command here

                                ;;
                                esac
                ;;


        HARD)
                echo -n "We can place another command here or make another call"

                ;;
        esac
        ;;
esac

exit 0

this is test.cfg

Code: Select all

define host {
        host_name               test-condor-site
        alias                   test-condor-site
        address                 172.16.37.44
        hostgroups              condor-site
        check_command           check-host-alive
        max_check_attempts      4
        check_interval          1
        retry_interval          1
        active_checks_enabled   1
        check_period            24x7
        contact_groups          admins
        contacts                vassiliy;mike,lukasz,frank
        notification_interval   2
        notification_period     24x7
        notification_options    d
        notifications_enabled   1
        event_handler           asteriks-host-phone-call
        event_handler_enabled   1
        }
define service {
        host_name               test-condor-site
        service_description     check-host-alive-or-not
        check_command           check-host-alive
        max_check_attempts      4
        check_interval          1
        retry_interval          1
        active_checks_enabled   1
        check_period            24x7
        contact_groups          admins
        contacts                vassiliy;mike,lukasz,frank
        notification_interval   60
        notification_period     24x7
        notification_options    w,u,r,c
        notifications_enabled   1
        #event_handler           asteriks-service-phone-call
        #event_handler_enabled   1
        }

I don't need service event handler for the moment, just host

this is a part of my commands.cfg
define command {
command_name asteriks-service-phone-call
command_line /usr/lib64/nagios/plugins/eventhandlers/asteriks-service-phone-call $SERVICESTATE $SERVICESTATETYPE $SERVICEATTEMPT
}

define command {
command_name asteriks-host-phone-call
command_line /usr/lib64/nagios/plugins/eventhandlers/asteriks-host-phone-call $HOSTSTATE $HOSTSTATETYPE $HOSTATTEMPT
}
I simulate that host is unpingable , using address for host 172.16.37.244 (no host with IP)

in my /var/log/messages I have
Mar 21 13:23:50 callme-crt-billing nagios: INITIAL SERVICE STATE: test-condor-site;check-host-alive-or-not;OK;HARD;1;PING OK - Packet loss = 0%, RTA = 4.71 ms
Mar 21 13:24:40 callme-crt-billing nagios: SERVICE ALERT: test-condor-site;check-host-alive-or-not;CRITICAL;SOFT;1;PING CRITICAL - Packet loss = 100%
Mar 21 13:24:50 callme-crt-billing nagios: HOST ALERT: test-condor-site;DOWN;SOFT;1;PING CRITICAL - Packet loss = 100%
Mar 21 13:24:50 callme-crt-billing nagios: GLOBAL HOST EVENT HANDLER: test-condor-site;DOWN;SOFT;1;asteriks-host-phone-call
Mar 21 13:24:50 callme-crt-billing nagios: HOST EVENT HANDLER: test-condor-site;DOWN;SOFT;1;asteriks-host-phone-call
Mar 21 13:25:40 callme-crt-billing nagios: SERVICE ALERT: test-condor-site;check-host-alive-or-not;CRITICAL;HARD;2;PING CRITICAL - Packet loss = 100%
Mar 21 13:26:10 callme-crt-billing nagios: HOST ALERT: test-condor-site;DOWN;SOFT;2;PING CRITICAL - Packet loss = 100%
Mar 21 13:26:10 callme-crt-billing nagios: GLOBAL HOST EVENT HANDLER: test-condor-site;DOWN;SOFT;2;asteriks-host-phone-call
Mar 21 13:26:10 callme-crt-billing nagios: HOST EVENT HANDLER: test-condor-site;DOWN;SOFT;2;asteriks-host-phone-call
Mar 21 13:27:30 callme-crt-billing nagios: HOST ALERT: test-condor-site;DOWN;SOFT;3;PING CRITICAL - Packet loss = 100%
Mar 21 13:27:30 callme-crt-billing nagios: GLOBAL HOST EVENT HANDLER: test-condor-site;DOWN;SOFT;3;asteriks-host-phone-call
Mar 21 13:27:30 callme-crt-billing nagios: HOST EVENT HANDLER: test-condor-site;DOWN;SOFT;3;asteriks-host-phone-call
according to my script on the last line I have to receive email initiated by event_handler, but nothing comes
On the next check email comes from - notify-host-by-email - command

in my nagios.cfg i have:

Code: Select all

global_host_event_handler=asteriks-host-phone-call
#global_service_event_handler=somecommand
enable_notifications=1
enable_event_handlers=1
Can somebody check what's wrong wtih my configs?
All other features of nagios work just perfectly (web-interface, check-commands and so on)

Re: calling script from eventhandler

Posted: Fri Mar 21, 2014 1:23 pm
by vvz
I forgot to mention that in sudoers i have lines
nagios ALL=(ALL) NOPASSWD:/usr/lib64/nagios/plugins/eventhandlers/asteriks-host-phone-call
Defaults:nagios !requiretty
ls -l for eventhandlers folder
-rwxr-xr-x. 1 nagios nagios 1233 Mar 21 12:26 asteriks-host-phone-call
-rwxr-xr-x. 1 nagios nagios 1364 Mar 18 14:10 asteriks-service-phone-call
thank you

Re: calling script from eventhandler

Posted: Fri Mar 21, 2014 2:53 pm
by slansing
Ah, and I was just going to ask if you'd tried executing it as the Nagios user, I would recommend trying this manually even though you defined a sudoers rule for it.

Another thing, have you tried sticking an echo in your script, or having it create a file at a certain point, that way we can see if it is actually being executed when the global event handler is triggered, would this be a possibility for you? We can help you add that in, though it seems as though you are more than capable. :)

Re: calling script from eventhandler

Posted: Fri Mar 21, 2014 3:04 pm
by vvz
Yes, I've tried to create file in eventhandlers/ from asteriks-home-phone-call script

if I call script manually it creates one, if from event_handler (nagios itself) - not

My guestre is : due some reason nagios does not start asteriks-host-phone-call script from commands.cfg , even in log file it is shown as argument

I can not understand why? I'll try to run script as nagios user now, let you know the result.

Of course I can initiate a call from normal notification, creating for that some additioanal user(s), but I thaught it should not be so hard to do from event_handler.

And another thing, it would give me some more opportunities to implement nagios for my future tasks ( i don't know for the moment, which ones exactly)

thank you

Re: calling script from eventhandler

Posted: Fri Mar 21, 2014 4:05 pm
by vvz
yes, you were right, even I have this NOPASSWD line for nagios in sudoers, system still asks for password

Re: calling script from eventhandler

Posted: Mon Mar 24, 2014 11:54 am
by abrist
Your sudoer command most likely needs to be changed. There is nothing in that script that needs elevated privileges except possibly the "touch" and "echo". You need to either:
1) create new sudoer rules for those 2 commands.
OR:
2) Add an asterisk at the end of the current sudoer command so that you are authorized for the command *including* any potential args:

Re: calling script from eventhandler

Posted: Mon Mar 24, 2014 12:49 pm
by vvz
yes I did, now sudoers looks like
root ALL=(ALL) ALL
nagios ALL=(ALL) NOPASSWD:/sbin/hpasmcli
nagios ALL=(ALL) NOPASSWD:/usr/lib64/nagios/plugins/*
nagios ALL=(ALL) NOPASSWD:/usr/lib64/nagios/plugins/check_proliant.py
nagios ALL=(ALL) NOPASSWD:/bin/mail
nagios ALL=(ALL) NOPASSWD:/bin/echo
nagios ALL=(ALL) NOPASSWD:/usr/lib64/nagios/plugins/eventhandlers/*
Defaults:nagios !requiretty
but still doesn't work

Re: calling script from eventhandler

Posted: Mon Mar 24, 2014 1:42 pm
by vvz
another question, my command in commands.cfg looks now (USER2 is activated in resourse.cfg):
define command {
command_name asteriks-host-phone-call
command_line $USER2$/asteriks-host-phone-call $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTATTEMPT$
Should be something like commas, semicolumn or column between macros? or just space?

Re: calling script from eventhandler

Posted: Mon Mar 24, 2014 2:49 pm
by vvz
Now it works,
thank you for your help