You can also tweet your Icinga2 monitoring notifications through a custom script written in Python with the help of the Python program “tweepy”.
1. Install the python program “tweepy”
You need to have an installed python environment and python package manager “pip”.
pip install tweepy
2. Authorization for your Twitter account
Go to the page http://twitter.com/oauth_clients
Please login into your Twitter account, which you want to use for your Icinga2 monitoring notifications. Then, please create a new app.
Add the “API Consumer Key” and “API Consumer Secret” (in the tab “Keys and tokens” -> “Consumer API keys”) of your new created app into the following Python script:
#!/usr/bin/env python
import tweepy
#*** Edit these two lines ***
API_CONSUMER_KEY = 'ADD YOUR CONSUMER KEY HERE'
API_CONSUMER_SECRET = 'ADD YOUR CONSUMER SECRET HERE'
#*** Edit these two lines ***
auth = tweepy.OAuthHandler(API_CONSUMER_KEY, API_CONSUMER_SECRET)
auth_url = auth.get_authorization_url()
print 'Please authorize: ' + auth_url
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
print "API CONSUMER KEY = '%s'" % auth.access_token.key
print "API CONSUMER SECRET = '%s'" % auth.access_token.secret
Create this Python script, insert your API keys and run this script to authorize your Twitter app for your server.
chmod 755 /tmp/authorize_twitter_app
./authorize_twitter_app
Follow the output of your new created script. You have to open the URL (authorization page) that is displayed in the output. Enter the PIN in your script that is displayed on the Twitter authorization page.
Now, You need also the “Access Key” and “Access Secret”, which you can find them in the tab “Keys and tokens” of your Twitter app settings. They are also displayed after you run the new created Python script (“authorize_twitter_app”).
3. Create the tweet Python script and add all the 4 mentioned keys:
#!/usr/bin/env python
import sys
import tweepy
import string
#*** Edit these 4 lines ***
API_CONSUMER_KEY = 'ADD YOUR CONSUMER KEY HERE'
API_CONSUMER_SECRET = 'ADD YOUR CONSUMER SECRET HERE'
API_ACCESS_KEY = 'ADD YOUR ACCESS KEY HERE'
API_ACCESS_SECRET = 'ADD YOUR ACCESS SECRET HERE'
#*** Edit these 4 lines ***
auth = tweepy.OAuthHandler(API_CONSUMER_KEY, API_CONSUMER_SECRET)
auth.set_access_token(API_ACCESS_KEY, API_ACCESS_SECRET)
api = tweepy.API(auth)
# save commandline argument to shorten the tweet message
s = sys.argv[1]
# Lets tweet!
api.update_status(s[0:280])
chmod 755 /usr/bin/tweet_cli
This script is used to create tweets on Twitter. It does get a string as an argument.
4. Custom notification scripts for Icinga2
Notice: Before you can use the custom notification script, you have to a new notification object in your Icinga2 settings. You can find more about the notification settings in the links below.
Create these two notifications scripts (for hosts and services) in the folder /etc/icinga2/scripts:
- Notification script “tweet-host-notification.sh”
#!/bin/bash
if [ $NOTIFICATIONTYPE = 'CUSTOM' ]
then
/usr/bin/tweet_cli "#$NOTIFICATIONTYPE - Server $HOSTALIAS. $NOTIFICATIONCOMMENT"
else
if [ "$HOSTSTATE" = 'UP' ] || [ "$HOSTSTATE" = 'OK' ]
then
/usr/bin/tweet_cli "#$NOTIFICATIONTYPE OK - SERVER #$HOSTALIAS is $HOSTSTATE. Thanks for your patience."
else
/usr/bin/tweet_cli "#$NOTIFICATIONTYPE - SERVER #$HOSTALIAS is $HOSTSTATE. Will be fixed asap."
fi
fi
- Notification script “tweet-services-notification.sh”
#!/bin/bash
if [ $NOTIFICATIONTYPE = 'CUSTOM' ]
then
/usr/bin/tweet_cli "#$NOTIFICATIONTYPE - Service $SERVICEDISPLAYNAME. $NOTIFICATIONCOMMENT"
else
if [ "$SERVICESTATE" = 'UP' ] || [ "$SERVICESTATE" = 'OK' ]
then
/usr/bin/tweet_cli "#$NOTIFICATIONTYPE OK - SERVICE #$SERVICEDISPLAYNAME is $SERVICESTATE. Thanks for your patience."
else
/usr/bin/tweet_cli "#$NOTIFICATIONTYPE - SERVICE #$SERVICEDISPLAYNAME on #$HOSTALIAS is $SERVICESTATE. Will be fixed asap."
fi
fi
For more information about tweepy:
https://github.com/tweepy/tweepy
More information about icinga2 notifications:
https://icinga.com/docs/icinga2/latest/doc/03-monitoring-basics/#notifications