Twitter as Nagios notification gateway

March 26th, 2007 by Alexander Mamchenkov

Here it is - I managed to get it all together and set up SMS notification from Nagios alerts via twitter. The job to be done is very easy:

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 :)

Posted in Technology, Network |

14 Responses

  1. Leonid Mamchenkov Says:

    Alex,

    First of all, thanks. Nicely done.

    Secondly, if I understand this right, this setup will only work for a single set of watchers. If there are several sets of notifications from the same Nagios installation, which are to be received by several groups of people, this setup won’t work. Right?

    It seems to me that a different aproach is needed, with several Twitter accounts…

  2. Alexander Mamchenkov Says:

    Oh, I just needed the simpliest setup. If you need to have more complex notifications, you can have multiple twitter accounts with different subscribers or for different types of notifications, then you can modify the twitter.pl script to accept two params like the twitter account name to be used and the message to be posted and route the messages from inside of the twitter.pl accordingly. Very simple to be done.

  3. Mike Says:

    Still works :-)

  4. Alexander Mamchenkov Says:

    It does :)

  5. Naeem Says:

    Thanks for the script. It wasn’t working for me initially, then I noticed you’re missing ! from the first line of the perl script, so it should be:

    #!/usr/bin/perl -w

    works for me now.

  6. jammarlibre Says:

    You have an outstanding good and well structured site. I enjoyed browsing through it.i

  7. Willie Says:

    I am trying to use this script and twitter to get nagios to send me notificaitons to my cell phone. However, I am a little confused in the part where you say “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”, I am not sure what do you mean?

    Do you mean to create twitter account for nagios? If so, what would nagios’ email be?

    I hope you can help me on this.
    Thanks.
    –Willie

  8. Willie Says:

    Hello again:

    I tried running twitter.pl from the command line, and it gave me a whole bunch of errors. I do not know perl, so I really do not understand what is the problem. These are the error messages:

    Can’t locate Net/Twitter.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi ….
    …. /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./twitter.pl line 3.
    BEGIN failed–compilation aborted at ./twitter.pl line 3.

    Please someone help me.
    Thanks.

  9. Willie Says:

    Hello again:

    I figured out what the problem was. I had to install Net:Twitter.
    Using centos 5.2, this is how I did it:

    yum install “*JSON*”
    /usr/bin/perl -MCPAN -e ‘install Net::Twitter’

    Now, when I try the above command:

    /usr/local/nagios/libexec/twitter.pl “$NOTIFICATIONTYPE$ - $HOSTNAME$-$SERVICEDESC$ - $SERVICESTATE$ - $SERVICEOUTPUT$”

    I get a message on my twitter account, but how to I make it to get to my cell phone?

    Remember, I want nagios to send me notifications to my cell phone as text messages. I am also confused as how to create the nagios account and make it a friend with my account. I have been looking on twitter.com how to do so, but I have not found the way to do it.

    Please, someone help me.

    Thanks.
    –Willie

  10. Alexander Mamchenkov Says:

    Hi,

    here is the logic that might be easier:

    1. You need a separate account on Twitter for your nagios
    2. Make sure that in the settings of that account (for nagios) you set messages to be private, so that only people who are friends of that nagios account can see posts, otherwise all your nagios notifications will appear in the public post stream
    3. Create yourself a twitter account.
    4. Make your own twitter account to be a friend of nagios account and enable nofications of you whenever something appears on nagios twitter account
    5. Specify email and (optionally) gtalk account in you twitter settings and enable notifications.

    Now what happens is that whenever there is an alert at nagios, it is posted onto nagios twitter wall, from there, you, being a friend of this nagios, receive a notification )

  11. Willie Says:

    Hello Alexander:

    Thanks for your answer.

    I have followed steps 1-5 on your instructions. I get notifications from the nagios account to my personal account on twitter. However, I do not get anything on my cell phone. I entered my cell phone number on my personal account so that when nagios sends a notification to my personal account, I can get it on my cell phone. I sent the text string specified on devices, and enabled it by clicking to theleft of:

    It’s okay for Twitter to send txt messages to my phone. Standard rates apply.

    However, I do not get any notifications on my cell yet. I just get them from nagios (twiter) account to my personal (twiter) account.

    I do you think could be the problem?

    Thanks.
    –Willie

  12. Willie Says:

    When I try to update my cell phone, after sending the string to the 40404 number, I get this disappointing message:

    40404 NUMBER IS NOT SUPPORTED OR USER HAS NOT SUBSCRIBED TO SMS.

    My provider is MetroPCS. I have unlimited and free text messages, so I really do not understand what is going on.

    I hope someone can help me on this.
    Thanks.
    –Willie

  13. Willie Says:

    Hello Alexander and the rest of the guys:

    I am happy to report to you all that we finally got this working.
    I am receiving cell phone notifications from nagios. Just a note for anyone out there trying to get this working, for some reason, I do not get notifications on my MetroPCS cell phone, but I do on my Verizon Wireless phone. Just keep in mind that, according to what I have experienced, the cell phone carrier matters.

    Thanks again to you Alexander and the rest of you all for all your help
    –Willie

  14. Alexander Mamchenkov Says:

    I m glad you managed to set it up finally :) It is a while i used this system, since now I use SMS gateway for notifications, but good to know old methods also work :)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.