How to Post Status Messages from Opsview to your Twitter Account

 How to Get Twitter Notifications in OpsviewOpsview gives you lots of standard options for receiving status notifications, but with a bit of custom scripting you can also get status messages from Opsview and post them to a Twitter account.

This could be useful if you want to keep all your status messages in one place with a timeline. Plus you can also set up an account for other members of your team to follow so they can also see the updates. However it's probably best that the Twitter account is kept within your organisation unless you want the world to see the status of your servers!

This article tells you how to configure Opsview to send messages to Twitter. For configuring this you need to have access to the shell of your Opsview server. To access Twitter from Opsview, you'll need to install Tweepy. Tweepy is a Python library for accessing the Twitter API. It requires Python 2.5 or later.

If you need to install Python 2.6 you can follow the instructions on 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 you'll need to authenticate Twitter from the command line. This blog tells you how.

At this point you'll need to create a new Twitter account which will be used to post the notifications. Make sure you enable Tweet Privacy under Settings, in the Twitter account, so that posts are not made public (unless you want them to be!) After creating the Twitter account, you'll need to register a new client app with Twitter.

Select "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  "Create the command line script", change to python26. Don't 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])

Then "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/.

(call the script 'connect_twitter', hence 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 as a user with admin privileges. Go to Advanced then Notification Methods from the drop down menu. On the top right, click Actions and then Create new Notification Method as following:

Display Opsview Status in Twitter

Next, go to Configuration on the Opsview Menu bar, then Contacts. Click on the contact you want to set Twitter alerts for. Click the Notifications button. Add your Twitter ID (this ID will receive the Tweets).

Display Opsview Status on Twitter

Click on Submit and edit Notification Profiles. Click Actions, and create new Notification Profile for user.

Display Opsview Status on Twitter

Create the profile and submit changes. Reload your configuration (click on Configuration Status, top right, then Reload Configuration). Reboot one of your servers 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 now start coming in!

About the Author

Shibashish Satpathy is an Opsview Community user based in Mumbai, India. Shib works in IT Operations and enjoys photography, travelling and working with Linux!

Legal Disclaimer

This blog post is contributed by a member of the Opsview community. Opsview accept no responsibility for the accuracy of its content and are not liable for any direct or indirect damages caused by its use.

Try this out for yourself by downloading Opsview Core and get our latest tips and updates by following us on Twitter!