Info16.fr

Le blog de B@rtounet

#Non classé

Mise en place d'un script de surveillance d'un noeud

27 juillet 2011 Rédigé par bartounet

J'ai eut besoin il y a quelque temps de pouvoir surveiller la connectivité d'un poste distant, et d'être prévenu si ce dernier n'etait plus joignable... Je débute dans l'élaboration des scripts, mais j'ai tout de même réussi à avoir les fonctionnalité que je voulais... A savoir un script qui se lance en tant que daemon et qui peut etre demarrer ou arreter avec les commandes basique start stop...

Pour ne pas perdre ce petit script je préfère le mettre ici...

Le script se divise en deux fichiers... 1- Le script en lui même (/sbin/watchnode.sh) 2- Lz script qui ve permettre son lancement et son arret propre.. (/etc/init.d/watchnode)

Le watchnode.sh (à placer dans /sbin)

#!/bin/bash #Antony MARTINEAU 2007 #Connectivity test whith a node node=192.168.1.115 numofping=2 mail=root@appart.lan while  1          do                 for i in `seq 1 $numofping`                 do                 ping=$(ping -c1 -s16 $node |grep "24 bytes from"|cut -d"=" -f4|cut -d" " -f1)                         if  -z $ping ; then                         date=$(date)                         echo $date Warning: Peer node unreacheable. >> /var/log/watchnode                                 if  "$i" = "$numofping" ; then                                 date=$(date)                                 echo $date Failed: Consider peer node to be dead. >> /var/log/watchnode                                 echo "$node semble etre injoignable" | mail -s "Warning sur $node" $mail                                 fi                         fi                 done         sleep 30         done

Et le watchnode à placer dans /etc/init.d

 #!/bin/bash # #MARTINEAU Antony 2007 # /etc/init.d/watchnode # ### BEGIN INIT INFO # Provides:                     Watchnode plugin # Required-Start:       $network # Required-Stop:        $network # Default-Start:        3 5 # Default-Stop:         0 1 2 4 6 # Description:  Verify conectivity about a node and advise Administrator about node unreachable ### END INIT INFO watchnode_daemon="/sbin/watchnode.sh" watchnodelogpath="/var/log/watchnode" date=$(date) case "$1" in         start)                                 id=$(pgrep -x watchnode.sh)                                 if  ! -z $id ; then                                         echo "Watchnode plugin is already running."                                         exit 0                                 else                                         /sbin/start-stop-daemon start exec $watchnode_daemon &                                         date=$(date)                                         echo $date Watchnode plugin started >> $watchnodelogpath                                         echo "Watchnode plugin started."                                         exit                                 fi                 ;;                 stop)                                 id=$(pgrep -x watchnode.sh)                                 if  ! -z $id ; then                                         kill -9 $id                                 echo $date Watchnode plugin stopped >> $watchnodelogpath                                         while  1 ; do                                                 id=$(pgrep -x xenha.sh)                                                 if  -z $id ; then                                                         echo "Watchnode plugin stopped."                                                         exit 0 else                                                         kill -9 $id                                                 fi                                         done                                 else                                         echo "Watchnode plugin is already stopped."                                         exit 1                                 fi                                 echo $date Watchnode plugin stopped >> $watchnodelogpath                                 ;;                 restart)                                         while  1 ; do                                                 id=$(pgrep -x watchnode.sh)                                                 if  -z $id ; then                                                         $watchnode_daemon                                                         echo "Watchnode plugin restarted."                                                         exit 0                                                 else                                                         kill -9 $id                                                 fi                                         done                 ;;                 status)                                 id=$(pgrep -x watchnode.sh)                                 if  ! -z $id ; then                                         echo "Watchnode plugin is running."                                         exit 0                                 else                                         echo "Watchnode plugin is not running."                                         exit 0                                 fi                                 ;;         *)                 echo "Usage: $0 start"                 exit 1 esac

Comme on peut le constater le script de lancement est plus compliqué que le programme en lui même...

Ce script va faire 2 ping toutes les 30 secondes sur le noeud distant... au bout de deux timeout, il enverra un mail à l'administrateur... de plus il envoie tout ce qu'il fait dans les logs (/var/log/watchnode)

Information sur bartounet auteur de l'article

Les commentaires sont fermés.