notifry_logo
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /var/www/html/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Bonjour à tous…

Ca fait un bout de temps que je cherche une solution de push sur Android comme le propose Pushme.to sur iPhone.

Après en avoir tester plusieurs dont le très bon Notifo mais hélas plus maintenu, j’ai découvert NotiFry qui est l’objet de ce billet.

Bon but premier était de pouvoir être notifié lors d’un évènement quelconque genre :

  • La fin d’un téléchargement
  • Une alerte d’un onduleur
  • Un problème système ou réseau via Nagios

Dans un premier temps j’ai crée un compte sur le site, seul obligation avoir un compte gmail (loin d’être une grande contrainte).

Ma première réelle application est l’utilisation du fichier notifry.py sous linux pour pouvoir le réutilisé à la demande.

#!/usr/bin/env python
 
# Notifry - Python server push script.
#
# Copyright 2011 Daniel Foote
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# This script sends the notification to the backend server for the given source.
# Return codes:
# 0 - Success
# 1 - HTTP error
# 2 - Backend error
 
import urllib
import urllib2
import sys
import getopt
import json
 
# Configuration.
BACKEND = 'https://notifrier.appspot.com/notifry';
 
def usage():
    print "Usage: %s -s  -t Title -m Message [-u url]" % sys.argv[0]
    print "If message is -, read message from stdin."
    sys.exit()
 
# Parse our arguments.
optlist, args = getopt.getopt(sys.argv[1:], 's:t:m:u:')
 
params = {}
params['format'] = 'json'
requiredCount = 0
for key, value in optlist:
    if key == '-s':
        params['source'] = value
        requiredCount += 1
    elif key == '-t':
        params['title'] = value
        requiredCount += 1
    elif key == '-m':
        params['message'] = value
        requiredCount += 1
    elif key == '-u':
        params['url'] = value
 
# Not enough arguments?
if requiredCount != 3:
    usage()
 
# Read message from stdin, if required.
if params['message'] == '-':
    params['message'] = sys.stdin.read()
 
# Prepare our request.
try:
    response = urllib2.urlopen(BACKEND, urllib.urlencode(params))
 
    # Read the body.
    body = response.read()
    # It's JSON - parse it.
    contents = json.loads(body)
 
    if contents.has_key('error'):
        print "Server did not accept our message: %s" % contents['error']
        sys.exit(2)
    else:
        print "Message sent OK. Size: %d." % contents['size']
 
except urllib2.URLError, ex:
    print "Failed to make request to the server: " + str(ex)
    sys.exit(1)

Pour cela j’ai créé un fichier bash notify simplifiant l’utilisation du fichier Python.

#!/bin/bash
if [ $# = 3 ]
then
/home/scripts/notifry.py -s apy_key -t "$1" -m "$2" -u "$3"
elif [ $# = 2 ]
then
/home/scripts/notifry.py -s apy_key -t "$1" -m "$2"
else
echo "Syntaxe erreur"
fi

et s’utilise le plus facilement du monde via la commande suivante :

./notify "sujet" "message"

ou

./notify "sujet" "message" "url"

Une fois tout ça en place, venons en a son intégration dans Nagios et bientôt dans Shinken

Un certain nombre de fichiers sont à modifier pour la prise en compte de la notification via Nofifry.

Dans un premier temps il faut modifier le fichier /usr/local/nagios/etc/contacts.cfg et ajouter un nouvel utilisateur notifry :

define contact {
 
contact_name notifry
 
alias notifry
 
host_notification_options d,u,r,f
 
service_notification_options w,u,c,r,f
 
email api_key
 
host_notification_period 24x7
 
service_notification_period 24x7
 
host_notification_commands check-host-alive,notify-host-by-notifry
 
service_notification_commands service_is_stale,notify-service-by-notifry
 
}

Puis l’intégration de ce nouvel utilisateur dans les groupes de contacts via le fichier /usr/local/nagios/etc/contactgroups.cfg :

define contactgroup {
 
contactgroup_name admins
 
alias Nagios Administrators
 
members admin,notifry
 
}

Et enfin la création des commandes pour la notification en elle même via la création de deux commandes dans le fichier /usr/local/nagios/etc/misccommands.cfg :

define command {
 
command_name notify-host-by-notifry
 
command_line usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /home/scripts/notifry.py -s "$CONTACTEMAIL$" -t "$HOSTNAME$ is $HOSTSTATE$" -m "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -u "http://ip_serveur_nagios/nagios"
 
}
 
define command {
 
command_name notify-service-by-notifry
 
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /home/scripts/notifry.py -s "$CONTACTEMAIL$" -t "$SERVICEDESC$ on $HOSTALIAS$ is $SERVICESTATE$" -m "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -u "http://ip_serveur_nagios/nagios"
 
}

Une fois c’est trois fichiers modifiés, il ne reste plus qu’à vérifier nos modifications avec la commande suivante :

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Et lancer la prise en compte des modifications :

etc/init.d/nagios reload

 

 Bonne lecture et bonne implémentation.

2 thoughts on “Notifry – La notification gratuite pour Android”
  1. Depuis la mort de notifry il faut passer sur newtifry.

    Tout est pareil, l’API fonctionne de manière identique.
    Juste à remplacer ta ligne de backend 🙂

Répondre à Michael Annuler la réponse

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.