Hi,
How can I pass a custom date into email output? The formats provided from the macros $LONGDATETIME$ or $SHORTDATETIME$ aren't exactly what i'm after, so I'm trying to figure out a way to substitute output from Bash's date function or via some other method.
I've taken a couple simple swings at it, but figured I'd ask before tinkering with wrapper scripts and investing siginificant time. I tried assigning $(date +%m/%d/%Y" "%I:%m:%S" "%p) as a user defined macro in resources.cfg, then calling that given user macro in the command definition, but all the email returned was the raw text of the date function instead of its output. Next I attempted to add the command directly to the command definition, meaning I substituted in /bin/date date +%m/%d/%Y" "%I:%m:%S" "%p in place of $SHORTDATETIME$? Another thought I had was putting a wrapper around NRPE - since that's what we're using to collect most of our data - that would return the custom date in the additional info section.
What's an easy way to have a notification command execute Bash or pull in a custom date?
Any assistance is greatly appreciated.
Thanks!
Bryant
Custom Date in Email Output
Re: Custom Date in Email Output
Can you post your current command definition? This is likely some kind of quoting issue, and it would help to have some context.
Re: Custom Date in Email Output
Here's the command definition:
define command {
command_name service-notify-by-email-xml
command_line /usr/bin/printf "%b" "<TTEmail><Submit><FieldList>***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\n<FieldData><FieldName>TITLE</FieldName><FieldValue>Service: $SERVICEDESC$</FieldValue></FieldData><FieldData><FieldName>DESCRIPTION</FieldName><FieldValue>\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nAdditional Info:\n\n$SERVICEOUTPUT$</FieldValue></FieldData>\n\nDate/Time: <FieldData><FieldName>INCIDENT_START_DATE_TIME</FieldName><FieldValue>$(date +%m/%d/%Y" "%I:%m:%S" "%p)</FieldValue></FieldData></FieldList></Submit></TTEmail>" | /bin/mail -s "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
If I run that exact command from the command line (with a real email address obviously), it subsitutes in the date just fine.
I essentially need one command for a particular user that will send emails with a custom date. I'm doing this for a new ticketing system that we need email to ticket functionality on. The data needs to be in mm/dd/yyyy hh:ss:mm format to map to the proper field in this other application (which I don't have any control over).
- Bryant
define command {
command_name service-notify-by-email-xml
command_line /usr/bin/printf "%b" "<TTEmail><Submit><FieldList>***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\n<FieldData><FieldName>TITLE</FieldName><FieldValue>Service: $SERVICEDESC$</FieldValue></FieldData><FieldData><FieldName>DESCRIPTION</FieldName><FieldValue>\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nAdditional Info:\n\n$SERVICEOUTPUT$</FieldValue></FieldData>\n\nDate/Time: <FieldData><FieldName>INCIDENT_START_DATE_TIME</FieldName><FieldValue>$(date +%m/%d/%Y" "%I:%m:%S" "%p)</FieldValue></FieldData></FieldList></Submit></TTEmail>" | /bin/mail -s "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
If I run that exact command from the command line (with a real email address obviously), it subsitutes in the date just fine.
I essentially need one command for a particular user that will send emails with a custom date. I'm doing this for a new ticketing system that we need email to ticket functionality on. The data needs to be in mm/dd/yyyy hh:ss:mm format to map to the proper field in this other application (which I don't have any control over).
- Bryant
Re: Custom Date in Email Output
Try this instead:
Code: Select all
/usr/bin/printf "%b" "<TTEmail><Submit><FieldList>***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\n<FieldData><FieldName>TITLE</FieldName><FieldValue>Service: $SERVICEDESC$</FieldValue></FieldData><FieldData><FieldName>DESCRIPTION</FieldName><FieldValue>\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nAdditional Info:\n\n$SERVICEOUTPUT$</FieldValue></FieldData>\n\nDate/Time: <FieldData><FieldName>INCIDENT_START_DATE_TIME</FieldName><FieldValue>\\$$(date +%m/%d/%Y" "%I:%m:%S" "%p)</FieldValue></FieldData></FieldList></Submit></TTEmail>" | /bin/mail -s "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$Re: Custom Date in Email Output
Fantastic! Thank you, agriffin. It worked just as I needed without the \\ escaping, which made the body of the email an attachment. $$ is exactly what I needed to know.
Re: Custom Date in Email Output
Glad I could help! Please don't hesitate to let us know if you run into any other issues.