July 4, 2011

Opsview and Twitter Notification

This article will help to post your host and service status messages from Opsview to your Twitter account. For configuring this you need to have access to the shell of your Opsview server. Note that I do not assume any responsibility if any of the steps below results in unexpected behavior of Opsview or your system. Comments and improvements are welcomed.

The service notifications from Opsview are posted to my Twitter account (@shib4u) as shown below...

My setup...
OS - CentOS release 5.5
Opsview community Version: 3.11.3
The server should be able to access the internet.

What is Opsview?
In a nutshell, Opsview is better Nagios. Nagios monitors your entire IT infrastructure to ensure systems, applications, services. In the event of a failure, Nagios can alert technical staff of the problem. Here we are using Twitter to notify us.

I am not going to cover the steps for CentOS or Opsview installation, as they can be found here.

To access Twitter from Opsview, I installed Tweepy. Tweepy is a Python library for accessing the Twitter API. It requires Python 2.5 or later, hence, I installed Python 2.6 as described in this blog.

# wget http://dagobah.ftphosting.net/yum/smartfile.repo -O /etc/yum.repos.d/smartfile.repo

Then you can use YUM to install python26 package:

# yum install python26 python26-distribute

To get a list of installed packages, do:

# yum list *python26* | grep -i installed


Installed Packages
python26.x86_64             2.6.5-6.el5   installed
python26-distribute.noarch  0.6.10-4.el5  installed
python26-libs.x86_64        2.6.5-6.el5   installed

Install Tweepy

/usr/bin/easy_install-2.6 tweepy

Next, I had to authenticate twitter from the command line. Jeff Miller has written an excellent blog about achieving this.

I suggest you create a new Twitter account which will be used to post the notifications. Do enable Tweet Privacy under Settings, in the Twitter account, so that posts are not made public. (You do not want others to know that your servers are erratic, do you !?!). After creating the Twitter account, check out the blog and register a new client app with Twitter (as shown in step 2 there).

Do Step 3 "Connect the app to your Twitter account" with a minor change in the first line.. write python26 instead of python

#!/usr/bin/env python26


import tweepy


CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'


auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth_url = auth.get_authorization_url()
print 'Please authorize: ' + auth_url
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
print "ACCESS_KEY = '%s'" % auth.access_token.key
print "ACCESS_SECRET = '%s'" % auth.access_token.secret

Again, in step 4 "Create the command line script", change to python26. Do not forget to add your own keys.

#!/usr/bin/env python26


import sys
import tweepy


CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'
ACCESS_KEY = 'paste your Access Key here'
ACCESS_SECRET = 'paste your Access Secret here'


auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(sys.argv[1])

After step 5 "Send a test tweet from the command line" and if the test is successful, do the following.
  1. Move the script to /usr/local/nagios/libexec/notifications/. I call the script as connect_twitter, hence my script is at /usr/local/nagios/libexec/notifications/connect_twitter
  2. chown nagios:nagios /usr/local/nagios/libexec/notifications/connect_twitter
  3. chmod +x /usr/local/nagios/libexec/notifications/connect_twitter
Create another script to be used by Opsview
/usr/local/nagios/libexec/notifications/notify-by-twitter

The script:


dt=`date '+%H:%M %d%b%y'`
if [ -n "$NAGIOS_SERVICEDESC" ]
then
msg=`echo @$NAGIOS__CONTACTTWITTERID Time:$dt. $NAGIOS_SERVICEDESC @ $NAGIOS_HOSTNAME,$NAGIOS_HOSTADDRESS is $NAGIOS_SERVICESTATE. $NAGIOS_SERVICEOUTPUT | cut -c 0-140`
/usr/local/nagios/libexec/notifications/connect_twitter "$msg"
unset msg
else
msg=`echo @$NAGIOS__CONTACTTWITTERID Time:$dt. $NAGIOS_HOSTNAME,$NAGIOS_HOSTADDRESS is $NAGIOS_HOSTSTATE. $NAGIOS_HOSTOUTPUT| cut -c 0-140`
/usr/local/nagios/libexec/notifications/connect_twitter "$msg"
unset msg
fi



Again, chown nagios:nagios /usr/local/nagios/libexec/notifications/notify-by-twitter

chmod +x /usr/local/nagios/libexec/notifications/notify-by-twitter

Log in to Opsview with an user with admin privileges.
Go to Advanced then Notification Methods from the drop down menu.
On the extreme top right, click Actions and then Create new Notification Method as following...

Next, in Opsview top bar, go to Configuration, then Contacts. Click on the contact you want to set twitter alerts for. Click the Notifications button. Add you Twitter id (this id will receive the Tweets).
Click on Submit and Edit Notification Profiles.

Click Actions, and Create new Notification Profile for user.
Create the profile and submit changes.
Reload your configuration (click on configuration status, top right, then Reload Configuration).


Reboot one of your server or stop a service which is added in Opsview. Note that Twitter allows 250 direct messages per day, do not cross this limit as tweets won't come in after you exceed this number.
The Tweets should start coming in.

July 15, 2010

Go Goa for T-Shirts

Sorry folks, haven't been writing for a long time... pretty tied up with work and better part of the time goes playing with my kid... But I'll keep posting here, more of pictures and less of text. To start off, I post some of the pictures of t-shirts which were on sale at Goa, in a few shops on the road from Calangute to Baga (I went there Jan'10 end). There were a few shops selling those t-shirts. The tailor was doing the embroidery work there itself, in the shop. Amazing skill and designs...