Command Variables vs. Macros

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
klee
Posts: 147
Joined: Fri Apr 04, 2014 2:31 pm

Command Variables vs. Macros

Post 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%
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Command Variables vs. Macros

Post 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.
klee
Posts: 147
Joined: Fri Apr 04, 2014 2:31 pm

Re: Command Variables vs. Macros

Post 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
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Command Variables vs. Macros

Post 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.
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.
klee
Posts: 147
Joined: Fri Apr 04, 2014 2:31 pm

Re: Command Variables vs. Macros

Post 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?
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Command Variables vs. Macros

Post 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.
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.
klee
Posts: 147
Joined: Fri Apr 04, 2014 2:31 pm

Re: Command Variables vs. Macros

Post 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
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Command Variables vs. Macros

Post 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.
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.
klee
Posts: 147
Joined: Fri Apr 04, 2014 2:31 pm

Re: Command Variables vs. Macros

Post 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%
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Command Variables vs. Macros

Post 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"
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