Wednesday, February 22, 2012

Use Twitter for SMS Notification in Nagios


First create an account on twitter to be used along with nagios. You probably also want to indicate that the account should be private via twitter settings in order for the whole internet not to see what is going on with you monitoring :). So go twitter and get an account.

Second, here is a piece of code for a script which I use as a notification handler:


#!/usr/bin/perl -w
use strict;
use Net::Twitter;
my $msg = shift;
my $twit = new Net::Twitter(username => "twitter_user",password => "twitter_pass");
if (defined($msg) && $msg !~ /^\s*$/) {
$twit->update($msg);
}

Don’t forget to replace twitter_user and twitter_pass with the details of twitter account you have created in above stage.

Now modify the nagios notification commands in the misc. commands config file (/etc/nagios/misccommands.cfg in my case) to utilize the erlier created twitter.pl script. I have the following definitions:


define command {
command_name notify-by-epager
command_line /path/to/twitter.pl "$NOTIFICATIONTYPE$ - $HOSTNAME$-$SERVICEDESC$ - $SERVICESTATE$ - $SERVICEOUTPUT$"
}

and the same for host-notify-by-epager.

Finally adjust your nagios contact definition to use the above commands for notifications and don’t forget to enable all the notifications you need

The above part will post all alerts to nagios twitter account. In order to be able to receive them via SMS or GTalk, add nagios twitter account as a friend to your personal twitter account (don’t forget to grant access to become a fried from nagios twitter account if you made it private) and follow up.
Note: in perl must install Twitter:
install Net::Twitter
or:

force install Twitter