Vous pouvez également tweeter vos notifications de surveillance Icinga2 grâce à un script personnalisé écrit en Python avec l'aide du programme Python "tweepy".
1. Installez le programme python "tweepy"
Vous devez avoir un environnement python installé et un gestionnaire de paquets python "pip".
pip install tweepy
2. Autorisation pour votre compte Twitter
Rendez-vous sur la page: http://twitter.com/oauth_clients
Veuillez vous connecter à votre compte Twitter, que vous souhaitez utiliser pour vos notifications de surveillance Icinga2. Ensuite, veuillez créer une nouvelle application.
Ajoutez la "Clé API consommateur" et le "Secret API consommateur" (dans l'onglet "Clés et tokens" -> "Clés API consommateur") de votre nouvelle application créée dans le script Python suivant :
#!/usr/bin/env python
import tweepy
#*** Editez ces deux lignes ***
API_CONSUMER_KEY = 'ADD YOUR CONSUMER KEY HERE'
API_CONSUMER_SECRET = 'ADD YOUR CONSUMER SECRET HERE'
#*** Editez ces deux lignes ***
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
Créez ce script Python, insérez vos clés API et exécutez ce script pour autoriser votre application Twitter pour votre serveur.
chmod 755 /tmp/authorize_twitter_app
./authorize_twitter_app
Suivez le résultat de votre nouveau script créé. Vous devez ouvrir l'URL (page d'autorisation) qui est affichée dans la sortie. Entrez le code PIN de votre script qui est affiché sur la page d'autorisation de Twitter.
Maintenant, vous avez également besoin de la "Clé d'accès" et du "Secret d'accès", que vous pouvez trouver dans l'onglet "Clés et jetons" des paramètres de votre application Twitter. Elles sont également affichées après l'exécution du nouveau script Python créé ("authorize_twitter_app").
3. Créez le script python qui est utilisé pour les notifications et ajoutez les 4 clés mentionnées :
#!/usr/bin/env python
import sys
import tweepy
import string
#*** Editez ces 4 lignes ***
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'
#*** Editez ces 4 lignes ***
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
Ce script est utilisé pour créer des tweets sur twitter. Il reçoit une chaîne de caractères comme argument.
4. Scripts de notification personnalisés pour Icinga2
Notice: Avant de pouvoir utiliser le script de notification personnalisé, vous devez créer un nouvel objet de notification dans vos paramètres Icinga2. Vous trouverez plus d'informations sur les paramètres de notification dans les liens ci-dessous.
Créez ces deux scripts de notification (pour les hôtes et les services) dans le dossier /etc/icinga2/scripts :
- Script de notification "tweet-host-notification.sh
#!/bin/bash
if [ $NOTIFICATIONTYPE = 'CUSTOM' ]
then
/usr/bin/twagios "#$NOTIFICATIONTYPE - Server $HOSTALIAS. $NOTIFICATIONCOMMENT"
else
if [ "$HOSTSTATE" = 'UP' ] || [ "$HOSTSTATE" = 'OK' ]
then
/usr/bin/twagios "#$NOTIFICATIONTYPE OK - SERVER #$HOSTALIAS is $HOSTSTATE. Thanks for your patience."
else
/usr/bin/twagios "#$NOTIFICATIONTYPE - SERVER #$HOSTALIAS is $HOSTSTATE. Will be fixed asap."
fi
fi
- Script de notification "tweet-services-notification.sh
#!/bin/bash
if [ $NOTIFICATIONTYPE = 'CUSTOM' ]
then
/usr/bin/twagios "#$NOTIFICATIONTYPE - Service $SERVICEDISPLAYNAME. $NOTIFICATIONCOMMENT"
else
if [ "$SERVICESTATE" = 'UP' ] || [ "$SERVICESTATE" = 'OK' ]
then
/usr/bin/twagios "#$NOTIFICATIONTYPE OK - SERVICE #$SERVICEDISPLAYNAME is $SERVICESTATE. Thanks for your patience."
else
/usr/bin/twagios "#$NOTIFICATIONTYPE - SERVICE #$SERVICEDISPLAYNAME on #$HOSTALIAS is $SERVICESTATE. Will be fixed asap."
fi
fi
Pour plus d'informations sur tweepy :
https://github.com/tweepy/tweepy
Plus d'informations sur les notifications icinga2 :
https://icinga.com/docs/icinga2/latest/doc/03-monitoring-basics/#notifications