Page 1 of 1

Environment Variables as Macros

Posted: Wed May 04, 2016 11:35 pm
by rexconsulting
Is there a way, in a notification command, to populate a macro with output or variable from a subshell?

The Nagios Core Document "Understanding Macros and How They Work": https://assets.nagios.com/downloads/nag ... acros.html describes using Macros as Environment Variables. I want to do the opposite.

The reason is that I want to do a lookup based on a host/service combination, derive a priority level to include with the notification, using something like:

mylookup | /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nPriority: $PRIORITY$ Host: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

where "mylookup" would populate $PRIORITY$.

Yes I can do this by writing a complete replacement notification script and I will likely do that but I am now wondering about whether this is possible. Hence this post.

So can it be done?

thanks,

CP

Re: Environment Variables as Macros

Posted: Wed May 04, 2016 11:37 pm
by rexconsulting
Actually I guess the command may be something more like:

mylookup && /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nPriority: $PRIORITY$ Host: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

Re: Environment Variables as Macros

Posted: Thu May 05, 2016 10:35 am
by rkennedy
With what you're doing, it sounds like you need to have your notifications send to a 'wrapper' script, which then could parse all of the variables Nagios is sending and generate the one for you that is '$PRIORITY$'. Then, have it run the printf / mail commands within that script.

Re: Environment Variables as Macros

Posted: Thu May 05, 2016 10:43 am
by ssax
I tried it an it doesn't work.

You would only really have to write a wrapper script for you notification command:

Code: Select all

#!/bin/bash

$NOTIFICATIONTYPE=$1
$HOSTNAME=$2
$HOSTSTATE=$3
$HOSTADDRESS=$4
$HOSTOUTPUT=$5
$LONGDATETIME=$6
$CONTACTEMAIL=$7


...Your priority code goes here, make sure to set PRIORITY variable...

/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: ${NOTIFICATIONTYPE}\nPriority: ${PRIORITY} Host: ${HOSTNAME}\nState: ${HOSTSTATE}\nAddress: ${HOSTADDRESS}\nInfo: ${HOSTOUTPUT}\n\nDate/Time: ${LONGDATETIME}\n" | /bin/mail -s "** ${NOTIFICATIONTYPE} Host Alert: ${HOSTNAME} is ${HOSTSTATE} **" "${CONTACTEMAIL}"
Then your command would be:

Code: Select all

$USER1$/your_wrapper_script.sh '$NOTIFICATIONTYPE$' '$HOSTNAME$' '$HOSTSTATE$' '$HOSTADDRESS$' '$HOSTOUTPUT$' '$LONGDATETIME$' '$CONTACTEMAIL$'

Re: Environment Variables as Macros

Posted: Thu May 05, 2016 12:28 pm
by rexconsulting
Yeah thanks for the ideas. That's kind of what I was thinking. Or just ditch sendmail and write the notification mailer using python's smtplib.

Re: Environment Variables as Macros

Posted: Thu May 05, 2016 1:17 pm
by rkennedy
That works as well, once you have the input vars ($1,$2,$3, etc.), you're able to manipulate them however you need to for your mailing function.

I will leave this thread open should you have any questions down the line creating your wrapper.