Page 1 of 2

macros return as $

Posted: Fri Apr 04, 2014 7:06 pm
by klee
Not sure what we're doing wrong with macros. Can someone please advise? Please and thank you. ~klee

1) I created this command called "service_event_test" incorporating use of macros: $USER1$/service_event_test.sh $HOSTNAME$ $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTOUTPUT$ $SERVICEDESC$ $SERVICESTATEID$ $SERVICEOUTPUT$
2) Using the Monitoring Wizard, I enabled Website URL Monitoring to watch a specific webpage.
3) Created the script below which points to macros
4) Under Nagios CCM > Monitoring > Services > Service Management > added 'service_event test' to Event Handler
5) Under NagiosXI, Details > Service Detail > I submitted a passive check to trigger an alert for this specific webpage
6) Results of script are returning "The host WEBPAGE has changed to a HARD UP state at Fri Apr 4 19:36:55 UTC 2014 with the error: OK and $ and $ and $"
------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash

HOSTNAME=$1
HOSTSTATE=$2
HOSTSTATETYPE=$3
HOSTOUTPUT=$4
SERVICEDESC=$5
SERVICESTATEID=$6
SERVICEOUTPUT=$7

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 and $SERVICEDESC and $SERVICESTATEID and $SERVICEOUTPUT" >> /tmp/hostinfo.txt
---------------------------------------------------------------------------------------------------------------------------------

Re: macros return as $

Posted: Mon Apr 07, 2014 10:04 am
by abrist
Are you sure this event is configured on a service and not the host? I ask because none of the service macros are not getting populated.
Additionally, any macros that could contain a space need to be wrapped with quotes:

Code: Select all

$USER1$/service_event_test.sh "$HOSTNAME$" $HOSTSTATE$ $HOSTSTATETYPE$ "$HOSTOUTPUT$" "$SERVICEDESC$" $SERVICESTATEID$ "$SERVICEOUTPUT$"
And:

Code: Select all

HOSTNAME="$1"
HOSTSTATE=$2
HOSTSTATETYPE=$3
HOSTOUTPUT="$4"
SERVICEDESC="$5"
SERVICESTATEID=$6
SERVICEOUTPUT="$7"

Re: macros return as $

Posted: Mon Apr 07, 2014 1:58 pm
by klee
Hi Abrist,

Thanks so much for the quick reply.

I'm calling this event handler script from from: CCM > Monitoring > Services > Service Management > Event Handler. I believe that is a service instead of host configuration. Then I'm submitting the passive check result via NagiosXI, Details > Service Detail (instead of host detail). Please let me know if this should be configured in some other way.

The good news is, I used the quotes as you suggested and the script worked fine. Unfortunately, I am now testing with the older VM from Nagios trial DVD instead of the vSphere OVF template we set up in our test lab (not reachable remotely). I'll head in to the office to make the changes remotely and I'll let you know...

Thanks a lot!!!

~klee

Re: macros return as $

Posted: Mon Apr 07, 2014 3:01 pm
by abrist
klee wrote: I'll head in to the office to make the changes remotely and I'll let you know...
Great! I will stop any further speculation on this post until you have checked the solution above in the proper environment. Talk to you soon!

Re: macros return as $

Posted: Tue Apr 08, 2014 1:22 pm
by klee
Hello Abrist,

It seems our problem were 2 fold:

1) I followed the instruction in the Nagios 'Introduction to Event Handlers' PDF and created a dummy host. Subsequent configurations (i.e. pointing event handler to shell script) were done using the corresponding Service > Service Management for said host. This is why the results were retuning $ for service macros. (When I tested your fix on my laptop using VM from DVD instead of vSphere OVF image on our test machine, I configured it as a service, hence the contradiction in result)

*It seems there's a corresponding Service item for each Host item (and vice versa). I find this very confusing, especially since Nagios' policies seem to restrict configuration for either one or the other (host & service configurations are mutually exclusive). Please, is there any documentation that would explain their differences in detail?

2) The quotes certainly helped as the non-quoted items affected other results (i.e. results for macros that don't need quotes) as seen in the pre/post examples below:

-The host WEBPAGE has changed to a HARD UP state at Tue Apr 8 14:03:20 UTC 2014 with the error: HTTP and OK and - and HTTP/1.1
-The host WEBPAGE has changed to a HARD UP state at Tue Apr 8 14:11:07 UTC 2014 with the error: HTTP OK - HTTP/1.1 302 Redirect - 0.009 second response time and HTTP and 1 and test

Re: macros return as $

Posted: Wed Apr 09, 2014 12:35 pm
by abrist
1) Yeah, the macros can be confusing. They are listed (with their associations) at: http://nagios.sourceforge.net/docs/3_0/macrolist.html
If you need access to a macro that is not for the particular object the event is acting from, you could use cross-object macros. From the event handlers doc:
On-Demand Macros (Cross-Object)
Complex event handler scripts occasionally need access to macros from other objects. The use cases include failover scenarios,
elastic clusters, load-balancing, etc. Usually macros only reference variables that are values of the object in question. Occasionally, a
scripter will want to check the values of other object macros. For example, in a failover scenario, your event handler may want to check
the status of a particular service on a different host other than the host that triggered the event.
If you would like to reference values for another host or service in an event handler, you can use what are called "on-demand" macros.
On-demand macros look like normal macros, except for the fact that they contain an identifier for the host or service from which they
should get their value. Here's the basic format for on-demand macros:

$HOSTMACRONAME:host_name$
$SERVICEMACRONAME:host_name:service_description$

Note that the macro name is separated from the host or service identifier by a colon (:). For on-demand service macros, the service
identifier consists of both a host name and a service description - these are separated by a colon (:) as well.
Examples of on-demand host and service macros follow:

$HOSTDOWNTIME:myhost$
$SERVICESTATEID:novellserver:DS Database$
$SERVICESTATEID::CPU Load$
I wrote the Introduction to event handlers doc, so if I should clarify of add something to it, let me know what is needed.

Re: macros return as $

Posted: Sat Apr 12, 2014 10:14 pm
by klee
Hi Abrist,

I'm very sorry for the delay in reply, but I'm still trying to get up to speed with the on-demand macros.

Regarding standard macros, I can't understand why certain macros don't have corresponding reference in the NAGIOS-NOTIFY-MIB file. Can you please elaborate?

$HOSTNAME$ = nHostname
$HOSTSTATE$ = ?
$LASTHOSTSTATE$ = ?

Regarding the Introduction to Event Handlers PDF, if you can show how to configure an event handler for services;or at least distinguish between the two, I think it would help beginners a great deal.

Thanks Again,

-klee

Re: macros return as $

Posted: Mon Apr 14, 2014 4:15 pm
by abrist
klee wrote: $HOSTSTATE$ = ?
$LASTHOSTSTATE$ = ?

Code: Select all

$HOSTSTATE$ = nHostStateID
$LASTHOSTSTATE$ = See below
I am not sure if there is an equivalent - you may have to grab all the last*state oid, grab the newest one, and treat that like the last state.

Re: macros return as $

Posted: Tue Apr 15, 2014 9:05 am
by klee
I thought the corresponding object for $HostStateID$ is nHostStateID ?

Re: macros return as $

Posted: Tue Apr 15, 2014 9:46 am
by abrist
klee wrote:I thought the corresponding object for $HostStateID$ is nHostStateID ?
Yep. The mib does not actually include the text states, just the state ids.