Tags

When configuring Nagios for notifications, you will have something like this:

# commands.cfg

# vim: se nowrap

#========================================
# NOTIFICATIONS
#========================================

# 'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
        }

# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }

Now, you might want to extend these by adding the host or service URL. With this addition, the receiver of the message can click on the URL for more data.

The URL for the notes and actions are available; they are in these macros

  • $HOSTACTIONURL$
  • $SERVICEACTIONURL$
  • $HOSTNOTESURL$
  • $SERVICENOTESURL$

It should be simple to add these to the command_line line in the Nagios configuration; unfortunately, it is not that simple. This is because ampersands are removed from the URL when it is inserted. Ampersands are used to separate arguments that are sent to web pages – and may be included in the URLs. PNP4Nagios does this, and sends arguments to its web pages separated by ampersands (as it should).

The Nagios documentation hints at this character cleansing process, but does not mention these macros as ones that are cleaned and stripped of shell characters.

The shell characters that are stripped can be set with the illegal_macro_output_chars setting in the main configuration file. According to the documentation, the affected macros are: $HOSTOUTPUT$, $HOSTPERFDATA$, $HOSTACKAUTHOR$, $HOSTACKCOMMENT$, $SERVICEOUTPUT$, $SERVICEPERFDATA$, $SERVICEACKAUTHOR$, and $SERVICEACKCOMMENT$.

There are ways to escape some characters in certain situations, but escaping the ampersand doesn’t work here – especially since the URLs are taken literally (and uninterpreted) in other situations.

The resolution to this problem is to bypass the macro altogether and use the environment variables instead. Replace the macro with its environment variable equivalent. For example, replace:

$HOSTACTIONURL$

in the command_line entry with this:

$${NAGIOS_HOSTACTIONURL}

With this, the string that is actually passed to the shell is:

${NAGIOS_HOSTACTIONURL}

which then is interpreted as a shell environment variable (which, in this case, is set by Nagios). The squiggly brackets are possibly irrelevant, but they don’t hurt.

Advertisements