Pre-req reading:

Nagios customization: Alerting via SMS, or anything you like!

Making the bird tweet using python

or
Update twitter in a single line

This entry will cover how to send nagios alerts to twitter, in the examples to follow curl will be used however you can choose to use the python example (link above) in place of this.

Firstly edit /usr/local/nagios/etc/objects/commands.cfg

And add the two following commands.

UPDATE 24/03/2011 Twitter no longer supports basic auth, use my oAuth updater here

1
2
3
4
5
6
7
8
9
define command {
        command_name    notify-by-twitter
        command_line    /usr/bin/curl --basic --user "twitteruser:twitterpassword" --data-ascii "status=[Nagios] $NOTIFICATIONTYPE$ $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" http://twitter.com/statuses/update.json
}

define command {
        command_name    host-notify-by-twitter
        command_line    /usr/bin/curl --basic --user "twitteruser:twitterpassword" --data-ascii "status=[Nagios] $HOSTSTATE$ alert for $HOSTNAME$" http://twitter.com/statuses/update.json
}

Now define a contact for this twitter service

/usr/local/nagios/etc/objects/contacts.cfg

1
2
3
4
5
6
7
8
9
define contact{
        contact_name                    twitter
        service_notification_commands   notify-by-twitter
        host_notification_commands      host-notify-by-twitter
        service_notification_period 24x7
        host_notification_period 24x7
        service_notification_options a
        host_notification_options a
}

Choose your own notification options, for my feed I only choose alerts, I also have this send updated to a ‘private feed’ which I then follow.

Add this contact into your existing contact groups, i.e.

1
2
3
4
5
define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin,sms_alert,twitter
        }

Then run a nagios-verify to ensure you have no syntax errors, and restart nagios.

Trigger an alert by manually switching a monitored service off or entering a manual result to test.

Tags: , , , ,
4 Responses to “Nagios – Send alerts to twitter”
  1. LJ says:

    A couple of thoughts…
    First your service/host_notification_options are “a” (for ‘Alerts’)… I’ve never seen this being a Nagios option, it’s not mentioned in the documentation that I know of
    http://nagios.sourceforge.net/docs/2_0/xodtemplate.html
    http://nagios.sourceforge.net/docs/3_0/objectdefinitions.html#contact
    Is there something I’m missing?

    Secondly… you’re configs didn’t work for me! :) And not surprisingly, I don’t (yet) know why. If I run the CURL by hand, it seems to update, but I get a ‘response’ back from the http server, so maybe that screws up the nagios daemon? None the less, I’ve had a couple of ‘down’ & ‘recovery’ cycles, and the nagios logs show notifications sent to my normal email clients, and to the twitter group & user… but nothing shows up on twitter, but trusty old email works fine.

    [1253872055] SERVICE ALERT: jnu-frontstreet-2600-d1;PING;OK;HARD;4;PING OK – Packet loss = 0%, RTA = 17.84 ms
    [1253872055] SERVICE NOTIFICATION: twitter;frontstreet-2600-d1;PING;OK;notify-by-twitter;PING OK – Packet loss = 0%, RTA = 17.84 ms
    [1253872056] SERVICE NOTIFICATION: adam;frontstreet-2600-d1;PING;OK;notify-by-email;PING OK – Packet loss = 0%, RTA = 17.84 ms

    Hmmmphfff.

  2. Buzz says:

    Hi LJ,

    Try piping the output to a log file.

    i.e.

    /usr/bin/curl –basic –user “twitteruser:twitterpassword” –data-ascii “status=[Nagios] $NOTIFICATIONTYPE$ $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$” http://twitter.com/statuses/update.json >> /var/log/nagios-twitter.log

    If entries are being written to the log check the returned JSON data from twitter to check for indications of any issues.

    Bare in mind if your password has charectrs such as a $ this can interfere with this working due to Nagios trying to read $Partofpasswordhere as a variable.

  3. [...] it uses the basic statuses/update API method, it is instantly adaptable to StatusNet. Nagios can do the same [...]

  4. Monty Cantsin says:

    … or use twidge. https://github.com/jgoerzen/twidge/wiki or apt-get install twidge.

    nagios@host:~# vi /etc/nagios/twidgerc

    put your twidge configuration in it. e.g.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [DEFAULT]
    oauthaccesstoken: %(serverbase)s/oauth/access_token
    oauthauthorize: %(serverbase)s/oauth/authorize
    oauthdata: [("user_id","XXXXXX"),("screen_name","YOUR_SCREENNAME"),("oauth_verifier","XXXXXX"),("oauth_token","XXXXXX"),("oauth_token_secret","XXXXXX"),("oauth_callback_confirmed","true")]
    oauthrequesttoken: %(serverbase)s/oauth/request_token
    sendmail: /usr/sbin/sendmail
    serverbase: https://api.twitter.com
    shortenurls: yes
    urlbase: %(serverbase)s/1

    Make sure the file is readable by Nagios.

    nagios@host:~# chown nagios /etc/nagios/twidgerc

    add the following lines to contacts.cfg (on my GNU/Linux Debian in /etc/nagios3/conf.d/contacts_nagios2.cfg)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    define contact{
            contact_name                    twitter
            alias                           Twitter
            service_notification_period     24x7
            host_notification_period        24x7
            service_notification_options    w,u,c,r
            host_notification_options       d,r
            service_notification_commands   notify-service-by-twitter
            host_notification_commands      notify-host-by-twitter
            email                           twitteraccount_to_contact
            }

    and add “twitter” to the members in the contactgroup (in the same file).

    add these lines to commands.cfg (on my GNU/Linux Debian in /etc/nagios3/commands.cfg)

    1
    2
    3
    4
    5
    6
    7
    8
    define command {
            command_name    notify-service-by-twitter
            command_line    echo "#Nagios $NOTIFICATIONTYPE$ $HOSTNAME$($SERVICEDESC$) is $SERVICESTATE$" | twidge -c /etc/nagios/twidgerc update
    }
    define command {
            command_name    notify-host-by-twitter
            command_line    echo "#Nagios $HOSTSTATE$ alert for $HOSTNAME$" | twidge -c /etc/nagios/twidgerc dmsend $CONTACTEMAIL$
    }

    =^.^=

  5.  
Leave a Reply