• 🏠 Accueil
  • 🧰 Générateurs
  • 📰 Blog
  • 🔎 Rechercher
  • 📧 Contact
  • 📘 Livre d'or
  • 🗞️ News
  • 💰 Soutien
  • ℹ️ Info : Le site a été modernisé pour être plus lisible et compatible avec les smartphones. N'hésitez pas à me signaler tout bug via le formulaire de contact.

    Envoi de notifications (mails, sms, jabber) lors d'un problème détecté


    Cette partie regroupe les méthodes de notifications utilisables par Nagios.

     

    Notification par mail

    Afin d'être notifié par mail d'un problème détecté par Nagios, il faut modifier le fichier contacts.cfg, puis ajouter son mail dans la partie:

    define contact{
            contact_name                    nagiosadmin        ; Short name of user
            use                             generic-contact    ; Inherit default values from generic-contact template (defined above)
            alias                           Nagios Admin       ; Full name of user
            email                           VotreMail@VotreDomaine.fr    ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
    
            service_notification_commands   notify-service-by-email
            host_notification_commands      notify-host-by-email
    
    }

     

     

     

    Notification par SMS

    Si vous êtes abonné chez Free, l'opérateur propose l'envoi de SMS depuis une URL Web. Ceci permet de mettre en place une solution rapide et gratuite. L'envoi ne peut être fait que sur l'URL d'un abonné, mais dans le cadre d'une utilisation domestique, ceci répond parfaitement au besoin.

    Pour être notifié par SMS, il faut ajouter deux entrées dans le fichier commands.cfg. Une première pour l'envoi de SMS lorsque l'on détecte un problème de supervision sur un service, la seconde lorsque l'on détecte un problème de supervision sur le serveur (qui ne répond plus, par exemple)

    Ajout des lignes dans le fichier commands.cfg

    # Notify Service problem by SMS
    define command{
        command_name notify-service-by-sms
        command_line /yourpathto/send_sms.sh "Service Alert: $HOSTALIAS$/$SERVICEDESC$ nService State: $SERVICESTATE$ nDate/Time: $LONGDATETIME$"
    }
    
    # Notify Host problem by SMS
    define command{
        command_name notify-host-by-sms
        command_line /yourpathto/send_sms.sh "Host Alert: $HOSTNAME$ State: $HOSTSTATE$ nDate/Time: $LONGDATETIME$"
    }

    Création du script send_sms.sh

    Une fois les lignes ajoutées, il faut créer le script send_sms.sh qui contient les lignes suivantes:

    #!/bin/bash
    
    ### Send an SMS, using Free platform
    CURL=/usr/bin/curl
    URL=https://smsapi.free-mobile.fr/sendmsg
    PASS={EntrezICIVotreMotDePasseFree}
    USER={EntrezICIVotreIdentifiantFree}
    
    # Getting Text
    SMSData="$*"
    
    # Sending SMS
    $CURL -k -X POST "https://smsapi.free-mobile.fr/sendmsg?user=$USER&pass=$PASS&msg=$SMSData"
    
    ### End of script

    Ajout de la méthode de notification dans les contacts

    Il suffit maintenant de rajouter l'appel aux notifications depuis l'entrée contact du fichier contacts.cfg (vu en début de cet article):

     service_notification_commands   notify-service-by-email,notify-service-by-sms
     host_notification_commands      notify-host-by-email,notify-host-by-sms
    
    

    On relance Nagios, et les notifications arriveront désormais par SMS.

     

    Notifications par Jabber

    Pour recevoir des notifications par Jabber, les manipulations seront identiques à celles des SMS, c'est à dire qu'il faut déclarer les deux commandes dans le fichier commands.cfg, créer le script de notification, et ajouter la méthode de contact dans le fichier contacts.cfg

    Ajout des lignes dans le fichier commands.cfg

    # To notify problems by Jabber
        define command{
        command_name host-notify-by-jabber
        command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$ \nState: $HOSTSTATE$\n Address: $HOSTADDRESS$\nInfo: $HOST OUTPU
    T$\n\nDate/Time: $LONGDATETIME$\n" | /yourpathto/nagios_notify_via_jabber.pl $CONTACTPAGER$ "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
        }
    
        define command{
        command_name notify-by-jabber
        command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$ \nState: $HOSTSTATE$\n Address: $HOSTADDRESS$\nInfo: $HOST OUTPU
    T$\n\nDate/Time: $LONGDATETIME$\n" | /yourpathto/nagios_notify_via_jabber.pl $CONTACTPAGER$ "** $NOTIFICATIONTYPE$ Service Alert: $SERVICEDESC$ on $HOSTNAME$ State: $SERVIC
    ESTATE$ Additional Info: $SERVICEOUTPUT$ **"
        }

    Le script nagios_notify_via_jabber.pl

    #!/usr/bin/perl -w
    #
    # script for nagios notify via Jabber / Google Talk Instant Messaging
    #   using XMPP protocol and SASL PLAIN authentication.
    #
    # author: Andrew Elwell <A.Elwell@physics.gla.ac.uk>
    # based on work by Thus0 <Thus0@free.fr> and  David Cox
    #
    # released under the terms of the GNU General Public License v2
    # Copyright 2007 Andrew Elwell.
    use strict;
    use Net::XMPP;
    ## Configuration
    my $username = "nagios";
    my $password = "VotreMotDePasseJabber";
    my $resource = "nagios";
    ## End of configuration
    my $len = scalar @ARGV;
    if ($len ne 2) {
       die "Usage...\n $0 [jabberid] [message]\n";
    }
    my @field=split(/,/,$ARGV[0]);
    #------------------------------------
    # Google Talk & Jabber parameters :
    my $hostname = 'AdresseIPDuServeurJabber';
    my $port = 5222;
    my $componentname = 'DomaineDuServeurNagios';
    my $connectiontype = 'tcpip';
    my $tls = 0;
    #------------------------------------
    my $Connection = new Net::XMPP::Client();
    # Connect to talk.google.com
    my $status = $Connection->Connect(
           hostname => $hostname, port => $port,
           componentname => $componentname,
           connectiontype => $connectiontype, tls => $tls);
    if (!(defined($status))) {
       print "ERROR:  XMPP connection failed.\n";
       print "        ($!)\n";
       exit(0);
    }
    # Change hostname
    my $sid = $Connection->{SESSION}->{id};
    $Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;
    # Authenticate
    my @result = $Connection->AuthSend(
           username => $username, password => $password,
           resource => $resource);
    if ($result[0] ne "ok") {
       print "ERROR: Authorization failed: $result[0] - $result[1]\n";
       exit(0);
    }
    # Send messages
    foreach ( @field ) {
    $Connection->MessageSend(
            to       => "$_\@$componentname",
            resource => $resource,
            subject  => "Notification",
            type     => "chat",
            body     => $ARGV[1]);
    }

     

    Ajout de la méthode de notification dans les contacts

    Il suffit maintenant de rajouter l'appel aux notifications depuis l'entrée contact du fichier contacts.cfg (vu en début de cet article):

     service_notification_commands   notify-service-by-email,notify-service-by-jabber
     host_notification_commands      notify-host-by-email,notify-host-by-jabber
    
    

    On relance Nagios, et les notifications arriveront désormais par Jabber (XMPP).

     

    Retour



    (c) 2005-2025 Jérôme DESMOULINS
    Mentions légales