The Usual Tech Ramblings

Nagios Notifications via Twitter

For several months I’ve had Nagios sending notifications for some services via Twitter. I set this up so that I could offer a couple of friends a little more visibility into their services. This used to be a trivial task as Twitter supported basic authentication, however they decided to go with a more secure methodology, and enforced OAuth authentication…

What is OAuth?

There is a good explanation of OAuth over here, but in brief, it is a method of permitting a service, application, or person, access to another service without giving them access to your username or password.

Before the Enforcement

As I stated, Twitter used to allow the use of basic authentication. If you have ever gone to a website, and it has popped up a box (not a form on a page) asking for your username and password, that is usually basic authentication. Wikipedia has an explanation of basic authentication here. Because they supported basic authentication, it was easy to use a multitude of tools to submit requests to Twitter. For example, here is an example using cURL:

/usr/bin/curl --basic --user "user:password" --data-ascii "status=cURL to Twitter Test"

Pretty simple, and easy to adjust to Nagios to use, there is an example here.

Post Enforcement

Once they made OAuth mandatory, using the above command would result in an error saying that basic authentication isn’t enabled, and nothing would be sent. This requires us to get a little more complicated.

Fortunately the fabulous folk of the Perl community have rolled a couple of libraries to make our lives easier; Net::Twitter::Lite and Net::OAuth. To make things even easier, over on the 0wned wiki is a script called Nitter, which rolls the two together, and makes usage in Nagios easier. There are a few setup steps required for the scripts, and a couple of prerequisites that need to be satisfied. Most of the prerequisites can be satisfied using your package system on your Linux distribution, for example on Debian:

aptitude install libnet-twitter-lite-perl libnet-oauth-perl

Once installed, follow the setup instructions, you’ll have to register your own application under your Twitter account for this to work, it’s a 5-6 field form, and you’re set. Once all done, sending messages is very easy…

./ --update --tweet "Test tweet using Nitter"

There is a switch to send a direct message to all accounts that are following the account you’re working with, however if you have multiple levels of security (different host contacts) you most likely won’t want to use that.

Integrating into Nagios

I’m going to assume you already know the basics, and have a contact setup. I have several, and because I didn’t want to break any of the existing contact methods, I created a custom object variables for the contacts. So this is what my contact now looks like:

define contact {
        contact_name                    jangliss
        alias                           Jonathan Angliss
        service_notification_commands   notify-by-email,notify-by-pager,service-notify-twitter
        host_notification_commands      host-notify-by-email,host-notify-by-pager,host-notify-twitter
        _TWIT                           j_angliss

I’ve snipped some of the options out, but you can see the basics. Now for the commands necessary:

define command {
        command_name    host-notify-twitter
        command_line    $USER1$/ --update --tweet "d @$_CONTACTTWIT$ [Nagios] $HOSTNAME$ is $HOSTSTATE$ [$SHORTDATETIME$]"

define command {
        command_name    service-notify-twitter
        command_line    $USER1$/ --update --tweet "d @$_CONTACTTWIT$ [Nagios] $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ [$SHORTDATETIME$]"

Both of these commands send direct messages (using twitters message syntax) to the contact they should be destined, using the contact macro we created earlier. To the right you can see a screen shot (click for bigger image) of a Nagios message using the new format.

With a couple of scripts, you’re now able to notify users using Twitter. This is just one of the many ways that Nagios is extensible, and an incredibly powerful bit of monitoring software.