event handler

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
vvz
Posts: 187
Joined: Wed Oct 30, 2013 5:15 pm

event handler

Post by vvz »

Hello!
Could somebody have a look at my configs and say what's wrong?

I'm trying to send a phone call via Asteriks and have a script to initiate call

Code: Select all

#!/bin/bash

#what state of host?

case "$1" in

OK)
        ;;
UNREACHABLE)
        ;;
DOWN)
        case "$2" in

        SOFT)
                  case "$3" in
                        1) touch checkfile.txt
                        ;;

                        3)
                                case "$4" in
                                     'sip:300@localhost')
                                                        sudo /usr/local/bin/linphonec -s sip:300@localhost
                                      ;;
                                     'sip:200@localhost')
                                                        sudo /usr/local/bin/linphonec -s sip:200@localhost
                                      ;;
                                esac
                        ;;


                    esac
           ;;


        HARD)

           ;;
        esac
;;
esac

exit 0
the name of the script - asteriks-host-phone-call - -rwxr-xr-x nagios nagios

if I initiate this script from command line like ./asteriks-host-phone-call DOWN SOFT 3 sip:300@localhost - everything works, and I receive a call, I'm doing this under root account. That means for me - script itself has no errors.

This is host definition

Code: Select all

define host {
        host_name               test-condor-site
        alias                   test-condor-site
        address                 172.16.37.44
        check_command           check-host-alive
        max_check_attempts      4
        check_interval          1
        retry_interval          1
        active_checks_enabled   1
        check_period            24x7
        contacts                vassiliy,vassiliy-phone          ;mike,mike-phone
        #contact_groups         admins
        notification_interval   3
        notification_period     24x7
        notification_options    d
        notifications_enabled   1
        event_handler           asteriks-host-phone-call
        event_handler_enabled   1
        }

global event handler - also switched on

contacts definitions
vassiliy

Code: Select all

define contact {
        contact_name                    vassiliy
        alias                           vassiliy
        contactgroups                   admins
        host_notifications_enabled      1
        service_notifications_enabled   1
        host_notification_period        24x7;vassiliy-oncall
        service_notification_period     24x7;vassiliy-oncall
        host_notification_options       d,u
        service_notification_options    w,c,r
        host_notification_commands      notify-host-by-email
        service_notification_commands   notify-service-by-email
        email                           vvins@starscriber.com
        can_submit_commands             1
        }
vassiliy-phone contact

Code: Select all

define contact {
        contact_name                    vassiliy-phone
        alias                           vassiliy-phone
        contactgroups                   admins-phone
        host_notifications_enabled      1
        service_notifications_enabled   1
        host_notification_period        24x7;vassiliy-oncall
        service_notification_period     24x7;vassiliy-oncall
        host_notification_options       d
        service_notification_options    c
#       host_notification_commands      notify-host-by-phone
#       service_notification_commands   notify-service-by-phone
        host_notification_commands      asteriks-host-phone-call
        service_notification_commands   asteriks-service-phone-call
        email                           sip:300@localhost
        can_submit_commands             1
        }

commands.cfg

Code: Select all

## these are commands to make calls via asteriks , comment them if you don't need them

define command {
          command_name   asteriks-host-phone-call
          command_line   /usr/lib64/nagios/plugins/eventhandlers/asteriks-host-phone-call  $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTATTEMPT$ $CONTACTEMAIL$
                 }


define command {
         command_name    asteriks-service-phone-call
         command_line    $USER2$/asteriks-service-phone-call    $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ $CONTACTEMAIL$
                 }


define command {
          command_name   notify-host-by-phone
          command_line   sudo /usr/local/bin/linphonec -s $CONTACTEMAIL$ && sleep 30s && killall linphonec
 }


define command {
          command_name   notify-service-by-phone
          command_line   sudo /usr/local/bin/linphonec -s $CONTACTMAIL$ && sleep 30s && killall linphonec
 }

At host status - DOWN SOFT 3 - nothing happens
If I remove $CONTACTMAIL$ macros from commands.cfg for asteriks-host-phone-call and make corresponding changes in script - I get a call from Nagios

sudoers have these strings:

Code: Select all

nagios  ALL=(ALL)       NOPASSWD:/usr/lib64/nagios/plugins/eventhandlers/*
nagios  ALL=(ALL)       NOPASSWD:/usr/local/bin/linphonec
Defaults:nagios !requiretty

if you need more info - let me know
Last edited by abrist on Thu Apr 24, 2014 3:01 pm, edited 2 times in total.
Reason: Changed [quote] to [code] so that the indents display properly. Much easier to read the scripts now.
vvz
Posts: 187
Joined: Wed Oct 30, 2013 5:15 pm

Re: event handler

Post by vvz »

one more - in /var/log/messages I have
Apr 22 13:41:17 callme-crt-billing nagios: HOST EVENT HANDLER: test-condor-site;DOWN;SOFT;2;asteriks-host-phone-call
As you can see in logs Host event handler shows only 3 parameters - DOWN,SOFT,2 and event handler name. It should be this way?
Because, as you can see in my commands.cfg I give 4 parameters to script ($CONTACTMAIL$ is not in log file messages)
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: event handler

Post by abrist »

The $CONTACTEMAIL$ macro is not available to event handlers. You would need to hardcode the email addresses into the event handler script, or use this script as a notification handler.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
vvz
Posts: 187
Joined: Wed Oct 30, 2013 5:15 pm

Re: event handler

Post by vvz »

OK, thank you. Can I give address value to event handler?
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: event handler

Post by abrist »

Yes. The commands would resemble:

Code: Select all

define command {
          command_name   asteriks-host-phone-call
          command_line   /usr/lib64/nagios/plugins/eventhandlers/asteriks-host-phone-call  $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTATTEMPT$ "youremail@domain.tld"
                 }


define command {
         command_name    asteriks-service-phone-call
         command_line    $USER2$/asteriks-service-phone-call    $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ "youremail@domain.tld"
                 }
Just a heads up, if you ever pass a macro that could potentially contain a special char (space, etc), make sure to wrap the macro in quotes in your command and then once again in your script. This is most important for macros like "$PLUGINOUTPUT$", "$HOSTNAME", etc.

One other tip: I would suggest loading the passed macros into readable variable names in the scripts, then you do not need to reference the command in order to identify what is being passed:

Code: Select all

#!/bin/bash

HOSTSTATE=$1
HOSTSTATETYPE=$2
HOSTATTEMPT=$3
EMAIL=$4

#what state of host?

case "$HOSTSTATE" in

OK)
        ;;
UNREACHABLE)
        ;;
DOWN)
        case "$HOSTSTATETYPE" in
        SOFT)
                  case "$HOSTATTEMPT" in
                        1) touch checkfile.txt
                        ;;
                        3)
                                case "$EMAIL" in
                                     'sip:300@localhost')
                                                        sudo /usr/local/bin/linphonec -s sip:300@localhost
                                      ;;
                                     'sip:200@localhost')
                                                        sudo /usr/local/bin/linphonec -s sip:200@localhost
                                      ;;
                                esac
                        ;;
                    esac
           ;;
        HARD)
           ;;
        esac
;;
esac

exit 0
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
vvz
Posts: 187
Joined: Wed Oct 30, 2013 5:15 pm

Re: event handler

Post by vvz »

but in my configuration (check vassiliy-phone contact) I'm using host_notification_commands asteriks-host-phone-call,

so I'm using this script as notification command, right? or I do not understand something?
vvz
Posts: 187
Joined: Wed Oct 30, 2013 5:15 pm

Re: event handler

Post by vvz »

sorry, I just have read your last post.
I need to think about
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: event handler

Post by abrist »

vvz wrote:so I'm using this script as notification command, right? or I do not understand something?
It all depends on which object the command is attached to. If the command is called from a contact as their notification handler, $CONTACTEMAIL$ will populate. If you are calling the command from an event handler, it will not.
vvz wrote:I need to think about
No problem, I find a nice pint of Guiness can help :P
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
vvz
Posts: 187
Joined: Wed Oct 30, 2013 5:15 pm

Re: event handler

Post by vvz »

but in my case I call asteriks-host-phone-call from vassiliy-phone contact as an argument of host_notification_commands and $CONTACTMAIL$ macros is not populated. So what's the problem?


A can of beer would be good :)
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: event handler

Post by abrist »

it looks like you have 2 contacts and an event handler configured for that host. Force a few of the notifications to attempt to be sent by forcing the host down. Then get a nice large tail of the nagios log:

Code: Select all

tail -35 /usr/local/nagios/var/nagios.log
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
Locked