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.

1 comment:

  1. https://www.facebook.com/bantalsilikongrosir
    http://bantalsilikon01.blogspot.com/2014/12/a.html
    http://jualsangkarpleci.blogspot.com/2014/12/a.html
    http://kopiluwakliar01.blogspot.com/2014/12/a.html
    http://vvty.in/uncategorized/marinir-seo/

    http://marinirseo.blogspot.com/2014/12/a.html
    http://marinir-seo.blogspot.com/2014/12/a.html
    http://bumbu-pecel-malang.blogspot.com/2014/12/a.html
    http://distro-seo.blogspot.com/2014/12/a.html
    http://restoran-seo.blogspot.com/2014/12/a.html
    http://bantalmalangmurah.blogspot.com/2014/12/a.html
    http://distrobantal.blogspot.com/2014/12/a.html
    http://kesethandukmalang.blogspot.com/2014/12/a.html

    http://tasya.marinirseo.web.id/?p=4http://caca.marinirseo.web.id/?p=4http://anne.marinirseo.web.id/?p=4http://jeannet.marinirseo.web.id/?p=4http://jelita.marinirseo.web.id/?p=4http://brenda.marinirseo.web.id/?p=1http://ruth.marinirseo.web.id/?p=4http://tasya1.marinirseo.web.id/?p=4http://caca1.marinirseo.web.id/?p=4http://anne1.marinirseo.web.id/?p=4http://jeannet1.marinirseo.web.id/?p=4http://jelita1.marinirseo.web.id/?p=4http://brenda1.marinirseo.web.id/?p=1http://ruth1.marinirseo.web.id/?p=4http://tasya2.marinirseo.web.id/?p=4http://caca2.marinirseo.web.id/?p=5http://anne2.marinirseo.web.id/?p=4http://jeannet2.marinirseo.web.id/?p=4http://jelita2.marinirseo.web.id/?p=4http://brenda2.marinirseo.web.id/?p=1http://ruth2.marinirseo.web.id/?p=4http://ruth3.marinirseo.web.id/?p=3http://jelita3.marinirseo.web.id/?p=3http://brenda3.marinirseo.web.id/?p=3http://jeannet3.marinirseo.web.id/?p=3http://anne3.marinirseo.web.id/?p=3http://caca3.marinirseo.web.id/?p=3http://tasya3.marinirseo.web.id/?p=3http://tasya4.marinirseo.web.id/?p=4http://caca4.marinirseo.web.id/?p=4http://anne4.marinirseo.web.id/?p=4http://jeannet4.marinirseo.web.id/?p=4http://jelita4.marinirseo.web.id/?p=4http://brenda4.marinirseo.web.id/?p=6http://ruth4.marinirseo.web.id/?p=4http://tasya5.marinirseo.web.id/?p=4http://caca5.marinirseo.web.id/?p=4http://anne5.marinirseo.web.id/?p=4http://jelita5.marinirseo.web.id/?p=4http://ruth5.marinirseo.web.id/?p=4
    http://tasya.marinirseo.web.id/?p=4http://caca.marinirseo.web.id/?p=4http://anne.marinirseo.web.id/?p=4http://jeannet.marinirseo.web.id/?p=4http://jelita.marinirseo.web.id/?p=4http://brenda.marinirseo.web.id/?p=1http://ruth.marinirseo.web.id/?p=4http://tasya1.marinirseo.web.id/?p=4http://caca1.marinirseo.web.id/?p=4http://anne1.marinirseo.web.id/?p=4http://jeannet1.marinirseo.web.id/?p=4http://jelita1.marinirseo.web.id/?p=4http://brenda1.marinirseo.web.id/?p=1http://ruth1.marinirseo.web.id/?p=4http://tasya2.marinirseo.web.id/?p=4http://caca2.marinirseo.web.id/?p=5http://anne2.marinirseo.web.id/?p=4http://jeannet2.marinirseo.web.id/?p=4http://jelita2.marinirseo.web.id/?p=4http://brenda2.marinirseo.web.id/?p=1http://ruth2.marinirseo.web.id/?p=4http://ruth3.marinirseo.web.id/?p=3http://jelita3.marinirseo.web.id/?p=3http://brenda3.marinirseo.web.id/?p=3http://jeannet3.marinirseo.web.id/?p=3http://anne3.marinirseo.web.id/?p=3http://caca3.marinirseo.web.id/?p=3http://tasya3.marinirseo.web.id/?p=3http://tasya4.marinirseo.web.id/?p=4http://caca4.marinirseo.web.id/?p=4http://anne4.marinirseo.web.id/?p=4http://jeannet4.marinirseo.web.id/?p=4http://jelita4.marinirseo.web.id/?p=4http://brenda4.marinirseo.web.id/?p=6http://ruth4.marinirseo.web.id/?p=4http://tasya5.marinirseo.web.id/?p=4http://caca5.marinirseo.web.id/?p=4http://anne5.marinirseo.web.id/?p=4http://jelita5.marinirseo.web.id/?p=4http://ruth5.marinirseo.web.id/?p=4

    ReplyDelete