rss

Planifier une surveillance de mon serveur

Voici un petit exemple pour Linux pour surveiller la présence d'un serveur avec un shell et une tâche planifiée (crontab) :

Fichier checkMyServerIsRunning.sh



#!/bin/bash
# vers qui envoyer l'alerte ?
DIFFUSION=toto@yoyo.fr
# fichier ProcessId du serveur
PID_FILE=/appli/MyServer/MyServer.pid
# rapport d'alerte
CHECK_REPORT=/tmp/checkMyServer.log
# le serveur est-il actif O/N ? 1/0
SERVER_RUNNING=0
echo - RAPPORT $0 DU `date` - `hostname` >> ${CHECK_REPORT}
# si un fichier pid est present on teste la présence du processus
if [ -e "${PID_FILE}" ]
then
SERVERPID=`cat ${PID_FILE}`
MSGPID="PID is ${SERVERPID}"
SERVER_RUNNING=`ps xaf | grep ${SERVERPID} |grep -v grep|wc -l`
fi
if [ "${SERVER_RUNNING}" == "1" ]
then
# le serveur est actif on ne fait rien
echo server is running ${MSGPID} >> ${CHECK_REPORT}
exit
fi

echo server is not running ... so restart it >> ${CHECK_REPORT}

# redémarrage du serveur
/etc/init.d/myServer status >> ${CHECK_REPORT}
/etc/init.d/myServer start >> ${CHECK_REPORT}
# mail avec PJ avec mutt
echo FICHIERS DE LOG MODIFIES CE JOUR EN PJ >> ${CHECK_REPORT}
zip lesLogsDuJour.zip `find . -name "*.log" -mtime -1` >> ${CHECK_REPORT}
mutt -s "`hostname` : serveur MyServer" -a lesLogsDuJour.zip ${DIFFUSION} < ${CHECK_REPORT}
rm -f lesLogsDuJour.zip
# remise a zero du rapport
echo > ${CHECK_REPORT}


Explications :
Dans un premier temps on paramètre les hypothèses de départ (emplacement du serveur, des fichiers de rapport, des destinataires)
Ensuite on vérifie la présence du serveur en contrôlant la présence de son fichier ".pid" et en vérifiant la présence du processus correspondant.
Si le serveur est effectivement démarré, on ne fait rien (si ce n'est augmenter le rapport de surveillance).
Si le serveur n'est pas démarré (fichier pid absent ou processus absent) alors on le redémarre :

  • on constitue une archive ZIP des fichiers de logs modifiés ce jour

  • on envoie un email du rapport de surveillance à la liste des destinataires paramétrés

  • on remet à zero le rapport de surveillance.



Tâche planifiée



La prochaine et dernière étape consiste à planifier une tâche toutes les heures pour lancer ce script.
Pour éditer la planification il faut exécuter la commande suivante :


crontab -e 


Il faut ajouter les 2 lignes suivantes :


# verification de MyServer toutes les heures 
0 * * * * cd /appli/MyServer && sh checkMyServerIsRunning.sh

Toutes remarques et/ou suggestions d'amélioration sont les bienvenues...

Aucun commentaire:

Enregistrer un commentaire

Mots clés du blog

10.1 4G acceptancetest adb androï Android androïd Android7 api appender appengine application applications archive array assistantematernelle astuce auth0 authentication authority automation Axis bash bearer blog boot bootloader bower build bundle c calendrier camille combal cdi certificate cf client cloudfoundry collaboratif command commandes connexion console css cyanogen decrypt démasquées démasquer développement dll dump easter eggs écologie écrit employeur EMUI EMUI5.0 encrypt enfant évènement export-package ExtJS fab fastboot fiche find firefox gadget galaxytab gelf gem git gmail gnupg gooelappengine google gparted gpg gpg2 gps graylog grenoble Grid gui harddrive heroku hover howto HTML http https IE ihm immobilier imprimante innovation insolite instance integration Java JavaScript jenkins jeu jobs json json-schema-validator key keystore labs linux livre log log4j logger logs lombok masquées masquer maven maven-gae-plugin Mémoire microsoft mobile mockito mondialisation monitor MUSE musique en ligne myopera nodejs npm NT NTEventLogger onglet openstack osgi paas package parameters parent php politique prosyst prototype proxies proxy quartz radio rappel recherche regex repository resize RIA ridge rock ROM route ruby rubygems s8500 samsung scheduler scm secret secure sel selenium Serializer server shared shell sign signature slf4j smartphone so société song spy ssh ssl struct swagger swig tâches téléphone téléréalité test thunderbird timeout token Tomcat tooltip tooltips truststore TWRP ubuntu unit test validator verify virgin virtualbox wave waze web WebApp wiki wikimedia wikipédia wikipen wiko windows windows10 yahoo youtube yum