Page 1 of 2

Command Variables vs. Macros

Posted: Thu May 15, 2014 7:48 am
by klee
How are Global Event Handler Command Variables different than macros? Is there a different write-up for Command Variables (other than what's on the "XI Global Event Handler" PDF)?

According to "Introduction to Event Handlers" PDF:
$USER1$/event_handler_test.sh $HOSTNAME$ $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTOUTPUT$

According to Global Event Handler Component default configuration:
/tmp/host_change_handler.sh --host="%host%" --hoststate=%hoststate% --hoststateid=%hoststateid% --lasthoststate=%lasthoststate% --lasthoststateid=%lasthoststateid% --hoststatetype=%hoststatetype% --currentattempt=%currentattempt% --maxattempts=%maxattempts% --hosteventid=%hosteventid% --hostproblemid=%hostproblemid% --hostoutput="%hostoutput%" --hostdowntime=%hostdowntime%

Re: Command Variables vs. Macros

Posted: Thu May 15, 2014 5:01 pm
by slansing
There is actually quite a bit of detail on this subject found through the core object definitions documentation:

http://nagios.sourceforge.net/docs/3_0/macros.html
http://nagios.sourceforge.net/docs/3_0/ ... tvars.html
http://nagios.sourceforge.net/docs/3_0/macrolist.html

If you have specific questions on use cases beyond what is shown on those pagers or elsewhere in the documentation tree let us know! Some of it might be answered in your other post.

Re: Command Variables vs. Macros

Posted: Tue May 20, 2014 9:59 am
by klee
OK, how do we define variables in event handlers?
According to "Introduction to Event Handlers" PDF, macros are defined as such (see below), but how would we do the same using variables? %HOST% %HOSTSTATE% %HOSTSTATETYPE% %HOSTOUTPUT%

----------------------------------------------------------------------------------------------------------------------------
$USER1$/event_handler_test.sh $HOSTNAME$ $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTOUTPUT$

#!/bin/bash
HOSTNAME=$1
HOSTSTATE=$2
HOSTSTATETYPE=$3
HOSTOUTPUT=$4
DATE=$(date) #sets the DATE variable to the output of the 'date' command
echo "The host $HOSTNAME has changed to a $HOSTSTATETYPE $HOSTSTATE state at $DATE with the error:
$HOSTOUTPUT" > /tmp/hostinfo.txt
----------------------------------------------------------------------------------------------------------------------------

Furthermore, snmptraps can reference these macros according to NAGIOS-NOTIFY-MIB

i.e. /usr/bin/snmptrap -v 2c -c public 192.168.x.x '' NAGIOS-NOTIFY-MIB::nSvcEvent nSvcHostname s "$HOSTNAME"

How would I do do this with variables?

Thanks,

-klee

Re: Command Variables vs. Macros

Posted: Tue May 20, 2014 11:05 am
by abrist
klee wrote:According to "Introduction to Event Handlers" PDF, macros are defined as such (see below), but how would we do the same using variables? %HOST% %HOSTSTATE% %HOSTSTATETYPE% %HOSTOUTPUT%
The XI global event handler variables are specific to the XI global event hander UI. The same is true for the XI notification message variables.
Most other portions of XI - at least those that are directly related to core, will still use the core macros and syntax.

Re: Command Variables vs. Macros

Posted: Tue May 20, 2014 11:27 am
by klee
Thanks abrist,

XI global event hander UI: meaning how they are set here, yes?

-------------------------------------------------------------------------------------------------------------------------

/tmp/host_change_handler.sh --host="%host%" --hoststate=%hoststate% --hoststateid=%hoststateid% --lasthoststate=%lasthoststate% --lasthoststateid=%lasthoststateid% --hoststatetype=%hoststatetype% --currentattempt=%currentattempt% --maxattempts=%maxattempts% --hosteventid=%hosteventid% --hostproblemid=%hostproblemid% --hostoutput="%hostoutput%" --hostdowntime=%hostdowntime%

/usr/local/nagios/libexec/nms_global_service_event_handler.sh --host="%host%" --service="%service%" --hoststate=%hoststate% --servicestate=%servicestate% --servicestateid=%servicestateid% --lastservicestate=%lastservicestate% --lastservicestateid=%lastservicestateid% --servicestatetype=%servicestatetype% --currentattempt=%currentattempt% --maxattempts=%maxattempts% --serviceeventid=%serviceeventid% --serviceproblemid=%serviceproblemid% --serviceoutput="%serviceoutput%"

-------------------------------------------------------------------------------------------------------------------------

How do they have to be referenced inside of the event handler script?

Re: Command Variables vs. Macros

Posted: Tue May 20, 2014 2:50 pm
by abrist
The percentage variable mappings are only valid for the XI event handler php scripts. I think we talk about your use case on a different thread. If you want to use the XI global event handler UI, you need to change the event handler mappings and command back to default.

Re: Command Variables vs. Macros

Posted: Tue May 20, 2014 4:12 pm
by klee
Hi abrist,

I did change the event handler mappings and command back to default.

But I don't understand what you mean by "XI event handler php scripts".

Can you please elaborate?

Thanks,

-klee

Re: Command Variables vs. Macros

Posted: Tue May 20, 2014 5:07 pm
by abrist
Specifically the event handler commands in nagios.cfg:

Code: Select all

global_host_event_handler=xi_host_event_handler
global_service_event_handler=xi_service_event_handler
If you are using the XI gloabal event handler, you can just use the variables without the mapping: %host%, %hoststate%, etc.

Re: Command Variables vs. Macros

Posted: Tue May 20, 2014 6:51 pm
by klee
Yes, I did switch these configurations back in /usr/local/nagios/etc/nagios.cfg. I'm not sure exactly what xi_host_event_handler & xi_service_event_handler are, or how to access them though...

global_host_event_handler=xi_host_event_handler
global_service_event_handler=xi_service_event_handler

Plus, the only change I made is which scripts get called. So if I indeed "can just use the variables without the mapping: %host%, %hoststate%, etc.", how do I reference these variables within the bash script? Thanks Much, -klee

/usr/local/nagios/libexec/nms_global_host_event_handler.sh --host="%host%" --hoststate=%hoststate% --hoststateid=%hoststateid% --lasthoststate=%lasthoststate% --lasthoststateid=%lasthoststateid% --hoststatetype=%hoststatetype% --currentattempt=%currentattempt% --maxattempts=%maxattempts% --hosteventid=%hosteventid% --hostproblemid=%hostproblemid% --hostoutput="%hostoutput%" --hostdowntime=%hostdowntime%

/usr/local/nagios/libexec/nms_global_service_event_handler.sh --host="%host%" --service="%service%" --hoststate=%hoststate% --servicestate=%servicestate% --servicestateid=%servicestateid% --lastservicestate=%lastservicestate% --lastservicestateid=%lastservicestateid% --servicestatetype=%servicestatetype% --currentattempt=%currentattempt% --maxattempts=%maxattempts% --serviceeventid=%serviceeventid% --serviceproblemid=%serviceproblemid% --serviceoutput="%serviceoutput%" --servicedowntime=%servicedowntime%

Re: Command Variables vs. Macros

Posted: Wed May 21, 2014 11:41 am
by abrist
Are you configuring the following command in the global event handler ui (Admin --> Manage Components --> Global Event Handlers --> Edit)?
klee wrote: /usr/local/nagios/libexec/nms_global_host_event_handler.sh --host="%host%" --hoststate=%hoststate% --hoststateid=%hoststateid% --lasthoststate=%lasthoststate% --lasthoststateid=%lasthoststateid% --hoststatetype=%hoststatetype% --currentattempt=%currentattempt% --maxattempts=%maxattempts% --hosteventid=%hosteventid% --hostproblemid=%hostproblemid% --hostoutput="%hostoutput%" --hostdowntime=%hostdowntime%
You pass these variables to the bash script and then process them in the bash script as you would any other. For example, in the global event handler ui, lets set a script to be passed %host% and %hoststate%:

Code: Select all

/path/to/event/script.sh %host% %hoststate%
The script would then handle these like passed variables:

Code: Select all

#!/bin/bash
HOST=$1
HOSTSTATE=$2
echo "The host: $HOST is in state: $HOSTSTATE"