Nagios to Tweet on Alarm

In this entry I’m going to explain how to tweet when an alarm is triggered, this approach allows you to use twitter as a communication medium for Nagios this can be useful in case you don’t have SMSC accessible, personally I believe a tweet is a perfect replacement for an SMS. There are concerns about twitter not being as resilient however lately the fail whale became a rare sight, moreover Twitter alerts is to be used in parallel with the normal email alerting thus providing functionality that wasn’t there to begin with.

Part I , Setting up a shell script to update twitter:

1. Go to dev.twitter.com and create a new app make sure to set the access is set to read and write, note down the Consumer Key and Consumer Secret.

2. Download (OAuth.sh, TwitterOAuth.sh, tcli.sh), and place them in /usr/bin, chmod them to 777 (tcli.sh can be placed anywhere you want to).

3. Create a file named .tcli.rc and add the following two lines to it, using hte data you acquired from step 1.

oauth_consumer_key="YOURCONSUMERKEY" 
oauth_consumer_secret="YOURCONSUMERSECRET"

4. Log into twitter using the account u want the alarms to be tweeted from.
5. Go to where you placed tcli.sh and run “./tcli.sh -c statuses_update -s hello” you will be given a link, click on it to authorize the script to use your twitter account, copy the pin number twitter provides into the shell. Now the script is ready to tweet for you, check your timeline to see if the script did update it.

6. copy tcli.rc to nagios home, and chown it to nagios.

Part II , Configuring Nagios to use that Script:

To tell Nagios it can tweet for alarms, you’ll need to modify the commands.cfg file, adding new notification methods, Modify the command file to include 2 new commands, notify-service-by-twitter and notify-host-by-twitter.

1. Locate and open commands.cfg for editing, go to section”Notification Commands”

2. copy paste “notify-service-by-email” and “notify-host-by-email”, renaming them to *-*-*-twitter.

# 'notify-host-by-twitter' command definition
define command{
	command_name	notify-host-by-twitter
	command_line	/usr/bin/tcli.sh -c statuses_update -s </usr/bin/printf "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n"
	}

# 'notify-service-by-twitter' command definition
define command{
	command_name	notify-service-by-twitter
	command_line	/usr/bin/tcli.sh -c statuses_update -s </usr/bin/printf  "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$"
	}

3. run “nagios3 –verify-config nagios.cfg” to verify that the configuration are valid, if everything checks out, procceed.

word of advice here though, you need to make sure that the alarm would never exceed the  140 characters, other wise the tweet wont go out.

Now Nagios knows that it can use twitter to notify certain events, all you need to do now is integrate it into the services/hosts you need Nagios to tweet in case of an event.

4. go to your contacts.cfg file, this file has all your contact groups and how they are to be contacted in case of an event, I’m using the default group here.

# In this simple config file, a single contact will receive all alerts.

define contact{
        contact_name                    root
        alias                           Root
        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-email, notify-service-by-twitter
        host_notification_commands      notify-host-by-email, notify-host-by-twitter
        email                           root@localhost
        }

5. Verify the settings again as you did in step 3.

6. Now lets test if it worked, to do this i’m going to simulate a failure, by redefining the check_users service.

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       localhost
        service_description             Current Users
        check_command                   check_users!1!50
        }

7. Reload Nagios, and wait for it to tweet, if it didn’t start by checking if the alarm is exceeding 140 characters, keep in mind that twitter refuses duplicate statuses, so if you remove the time stamp only one alert will appear.

Advertisements

10 thoughts on “Nagios to Tweet on Alarm

    • yes, did that. I seems to be reading it in ok.
      I noticed it’s setting “oath_callback” to “oob” however, I do not have a callback URL configured in my application. Could this be the cause?

  1. FYI: I was able to get the script working on a newer CentOS system… my nagios server is old and running on FC4, so must be some function not working right there. Any guesses?

  2. after much debugging, I still don’t have it working, but found a clue:
    It seems the header that is getting generated on the FC4 box has some extra parenthesis in it:

    Not working auth_header (FC4):
    Authorization: OAuth realm=”http://api.twitter.com”, (oauth_consumer_key=”nSE…CAw”, oauth_signature_method=”HMAC-SHA1″, oauth_version=”1.0″, oauth_nonce=”d83e7e028bf79c80a452be3d55208f8a”, oauth_timestamp=”1312500743)”, oauth_token=”348…O8W”, oauth_signature=”v%2B…D%0D”

    (notice the paren before oauth_consumer_key and after timestamp)

    Working auth_header (CentOS):
    Authorization: OAuth realm=”http://api.twitter.com”, oauth_consumer_key=”nSE…CAw”, oauth_signature_method=”HMAC-SHA1″, oauth_version=”1.0″, oauth_nonce=”86ecf0e910dd681f47e6f4982f494ad8″, oauth_timestamp=”1312500759″, oauth_token=”348…O8W”, oauth_signature=”8X5…%3D”

    I haven’t been able to track down where/why this is happening yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s