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
Environment Variables as Macros
- rexconsulting
- Posts: 60
- Joined: Fri May 04, 2012 4:27 pm
- Location: Oakland, CA
- Contact:
Environment Variables as Macros
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
--
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
- rexconsulting
- Posts: 60
- Joined: Fri May 04, 2012 4:27 pm
- Location: Oakland, CA
- Contact:
Re: Environment Variables as Macros
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$
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
--
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
Re: Environment Variables as Macros
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
Re: Environment Variables as Macros
I tried it an it doesn't work.
You would only really have to write a wrapper script for you notification command:
Then your command would be:
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}"Code: Select all
$USER1$/your_wrapper_script.sh '$NOTIFICATIONTYPE$' '$HOSTNAME$' '$HOSTSTATE$' '$HOSTADDRESS$' '$HOSTOUTPUT$' '$LONGDATETIME$' '$CONTACTEMAIL$'- rexconsulting
- Posts: 60
- Joined: Fri May 04, 2012 4:27 pm
- Location: Oakland, CA
- Contact:
Re: Environment Variables as Macros
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
--
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
Re: Environment Variables as Macros
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.
I will leave this thread open should you have any questions down the line creating your wrapper.
Former Nagios Employee