Environment Variables as Macros

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.
Locked
User avatar
rexconsulting
Posts: 60
Joined: Fri May 04, 2012 4:27 pm
Location: Oakland, CA
Contact:

Environment Variables as Macros

Post 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
CP
--
Chris Paul
Rex Consulting, Inc
5652 Florence Terrace, Oakland, CA 94611
email: [email protected]
web: http://www.rexconsulting.net
phone, toll-free: +1 (888) 403-8996 ext 1
User avatar
rexconsulting
Posts: 60
Joined: Fri May 04, 2012 4:27 pm
Location: Oakland, CA
Contact:

Re: Environment Variables as Macros

Post 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$
CP
--
Chris Paul
Rex Consulting, Inc
5652 Florence Terrace, Oakland, CA 94611
email: [email protected]
web: http://www.rexconsulting.net
phone, toll-free: +1 (888) 403-8996 ext 1
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Environment Variables as Macros

Post 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.
Former Nagios Employee
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Environment Variables as Macros

Post 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$'
User avatar
rexconsulting
Posts: 60
Joined: Fri May 04, 2012 4:27 pm
Location: Oakland, CA
Contact:

Re: Environment Variables as Macros

Post 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.
CP
--
Chris Paul
Rex Consulting, Inc
5652 Florence Terrace, Oakland, CA 94611
email: [email protected]
web: http://www.rexconsulting.net
phone, toll-free: +1 (888) 403-8996 ext 1
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Environment Variables as Macros

Post 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.
Former Nagios Employee
Locked