Coding Range

Nagios alerts via App.Net

March 28th, 2013

After hearing someone at work mention Nagios, I figured I’d give it a try, considering the only way I could currently tell if my personal services were up was by checking them manually.

Why did nobody tell me about this sooner?

Anyway, I followed a basic tutorial on installing Nagios along with Nconf (because manually screwing around with text-based config files sucks).

For alerting though, I wanted to plug it into the App.Net Messaging system, simply because I prefer keeping email for personal communications, and I don’t have a free way to send alert SMS messages to my phone.

It’s surprisingly simple.

The first step of the puzzle was finding a command-line App.Net client. Fortunately, @doctorlinguist was crazy enough to make one - Texapp.

Next, I had to set up Texapp to be authenticated. To do this I simply ran Texapp under a normal user, then copied the .texappkey key file to /var/lib/nagios/ (the home directory of the Nagios user).

In the same folder, I put two scripts: notify-host-by-adn-pager, and notify-service-by-adn-pager. I use Netbot on my mobile devices for App.Net, and it doesn’t support push notifications for Messages, so I did a two-stage solution. The alert account replies to me with a basic alert (e.g. there was a problem with some service), then sends the full (private) details as a message. The two scripts are as follows:

notify-host-by-adn-pager.sh:

#!/bin/bash

TEXKEYF=/var/lib/nagios/.texappkey
TEXAPP=/var/lib/nagios/texapp
STATUS="@$1 Nagios: $2 notification for host (state: $5)"
PRIVMSG="Nagios: $2 - $4 ($3) is $5"

${TEXAPP} -keyf=${TEXKEYF} -runcommand="/pm $1 ${PRIVMSG}"
${TEXAPP} -keyf=${TEXKEYF} -status="${STATUS}"

notify-host-by-adn-pager.sh:

#!/bin/bash

TEXKEYF=/var/lib/nagios/.texappkey
TEXAPP=/var/lib/nagios/texapp
STATUS="@$1 Nagios: $2 notification for $5 (state: $6)"
PRIVMSG="Nagios: $2 - $5 on $4 ($3) is $6"

${TEXAPP} -keyf=${TEXKEYF} -runcommand="/pm $1 ${PRIVMSG}" >> /tmp/adn.log
${TEXAPP} -keyf=${TEXKEYF} -status="${STATUS}" >> /tmp/adn.log

To tie this in with Nagios, I set up two misccommands in NConf, as follows:

notify-service-by-adn-pager

/var/lib/nagios/notify-service-by-adn-pager.sh $CONTACTPAGER$ $NOTIFICATIONTYPE$ $HOSTADDRESS$ $HOSTALIAS$ "$SERVICEDESC$" $SERVICESTATE$

notify-host-by-adn-pager

/var/lib/nagios/notify-host-by-adn-pager.sh $CONTACTPAGER$ $NOTIFICATIONTYPE$ $HOSTADDRESS$ $HOSTALIAS$ $HOSTSTATE$

Once that’s done, create a new contact or modify an existing one. Add notify-host-by-adn-pager to ‘host notification commands’ and notify-service-by-adn-pager to ‘service notification commands’. Enter the App.Net username in the contact’s pager field, and there you go!