apt-get install python-dev python-pip pip install tweepyThen cd into your nagios libexec folder (mines at /usr/local/nagios/libexec)
cd /usr/local/nagios/libexec/We now add a new file called twitternagiosstatus.py
nano -w twitternagiosstatus.pyCopy and paste the following code into the file
#!/usr/bin/env python2.7 # tweet.py by Alex Eames http://raspi.tv/?p=5908 import tweepy import sys import logging # Setup Debug Logging logging.basicConfig(filename='/tmp/twitternagios.log',level=logging.DEBUG) logging.debug('Starting Debug Log') # Consumer keys and access tokens, used for OAuth consumer_key = 'jNgRhCGx7NzZn1Cr01mucA' consumer_secret = 'nTUDfUo0jH2oYyG8i6qdyrQXfwQ6QXT7dwjVykrWho' access_token = '2360118330-HP5bbGQgTw5F1UIN3qOjdtvqp1ZkhxlHroiETIQ' access_token_secret = 'rXjXwfoGGNKibKfXHw9YYL927kCBQiQL58Br0qMdaI5tB' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creation of the actual interface, using authentication api = tweepy.API(auth) if len(sys.argv) >= 2: tweet_text = sys.argv[1] logging.debug('Argument #1 ' + tweet_text) if len(tweet_text) <= 140: logging.debug('Tweeting: ' + tweet_text) api.update_status(tweet_text) else: print "tweet sent truncated. Too long. 140 chars Max." logging.debug('Too Long. Tweet sent truncated.') api.update_status(tweet_text[0:140])Replace consumer_key with your API key, consumer_secret with your API secret, access_token with your access token and access_token_secret with your Access token secret.
CTRL+x then Y then Enter.With the file saved, we need to make it executable.
chmod +x twitternagiosstatus.pyYou can now test that the script works by typing
./twitternagiosstatus.py "testy testy"You should now be able to see the Tweet on your new account (you may need to refresh the page).
cd /usr/local/nagios/etc/Edit your commands.cfg (mine is inside objects)
nano -w objects/commands.cfgWhere you choose to place the new configurations doesn't really matter, but to keep things in order I choose just below the email commands.
# 'notify-host-by-twitter' command definition define command{ command_name notify-host-by-twitter command_line /usr/local/nagios/libexec/twitternagiosstatus.py "$NOTIFICATIONTYPE$: $HOSTALIAS$ is $HOSTSTATE$" } # 'notify-service-by-twitter' command definition define command{ command_name notify-service-by-twitter command_line /usr/local/nagios/libexec/twitternagiosstatus.py "$NOTIFICATIONTYPE$: $SERVICEDESC$ ON $HOSTALIAS$ is $SERVICESTATE$" }You can adjust the specifics, but adding other $$ arguments (Use the email notification commands as an example). Save and exit
CTRL+x, then Y, then ENTERNow we add a new contact. Edit contacts.cfg
nano -w objects/contacts.cfgCopy and Paste the following
define contact{ contact_name nagios-twitter alias Nagios Twitter service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r,f host_notification_options d,u,r,f,s service_notification_commands notify-service-by-twitter host_notification_commands notify-host-by-twitter } define contactgroup{ contactgroup_name nagiostwitter alias Nagios Twitter Notifications members nagios-twitter }I decided to create a specific contact and contact-group for this, but you can adjust as you wish, add the contact to other contact-groups if you wish.
define host{ use linux-server ; Name of host template$ ; This host definition $ ; in (or inherited by) $ host_name excalibur alias Excalibur address 192.168.1.27 parents switch-netgear8 hostgroups linux-servers statusmap_image linux40.gd2 contact_groups nagiostwitter,sysadm }An example service on this host
define service{ use generic-service ; Name of servi$ host_name excalibur service_description PING check_command check_ping!100.0,20%!500.0,60% contact_groups nagiostwitter,sysadm }That's it, hopefully if all's done right you can restart the nagios service.
/etc/init.d/nagios restartNow your twitter feed will start to be populated with each alert. I can't emphasis enough that if the nagios configuration is done wrong you may break other alerts that are already setup.
Labels: Nagios, RaspberryPI, Twitter, Ubuntu