Nagios and the Ampersand Character in URLs

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.

9 thoughts on “Nagios and the Ampersand Character in URLs”

  1. I can tell you how infuriating I was finding this. I tried replacing & with %26 but then nagios replaced the % in %26 with %25, so I ended up with %2526!

    Thank you so much for writing this down somewhere.

    1. How did you make it work? I am having the ampersand problem.. Did you have to work around your way to get it functioning right?

      1. Just got your question from email. Can I see your command line in notify-service-by-email? Anyway, the trick I learned from here is $${NAGIOS_SERVICEOUTPUT}. I wrote a customized script to handle the alert but I didn’t do any special treatment of serveroutput in the script. Here my command line:

        $USER1$/nagios_alert “$HOSTNAME$” “$NOTIFICATIONTYPE$” “$SERVICEDESC$” “$HOSTADDRESS$ ” “$SERVICESTATE$” “$${NAGIOS_SERVICEOUTPUT}” “$CONTACTEMAIL$” “$SERVICEACKCOMMENT$” “$SERVICEACKAUTHOR$” “$SERVICEGROUPNAME$”

        Hope this helps.

  2. I haven’t been able to solve this yet. Could anyone suggest me what to do to include the ampersand character in the URL please?

  3. Good morning all,

    I’ve put up a nagios/nrpe script which in its output has an URL containing ampersand symbols. On the web interface everything is visualized correctly but when i receive the nagios alert email containing the URL.. the ampersand symbols have magically disappeared. Could somebody help me find the lost ampersands?

    Many Thanks,

    Maggie

    1. Just got your question from the email too. Please see my reply to Nitin. The trick is $${NAGIOS_SERVICEOUTPUT} in your notify-service-by-email command line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: