Une collection de scripts sans prétention pour aider dans le boulot de tous les jours avec Nagios.
Ce script sert à vérifier la taille des dossiers dans le répertoire donné en argument. Cela peut-être utile pour vérifier quel répertoire à grossi de façon anormale lorsqu’un disque est presque rempli ou rempli. Ce script s’appelle duf parce qu’il s’appuie sur du et complète df en diagnostic de remplissage de filesystem.
#!/bin/bash # Usage ./duf.sh /var/ # Use trailing slash to given path or it will output an error # Return the space used by each directory in the path given as arguments for Directory in `ls $1` do if [ -d $1$Directory ] then # du for each Directory returned by ls echo `du -hs $1$Directory` fi done exit 0
exemple
./duf /var/
sortie
2,7M /var/backups 109M /var/cache 24K /var/default 385M /var/lib 4,0K /var/local 4,0K /var/lock 83M /var/log 51M /var/mail 3,6M /var/opt 64K /var/run 732K /var/spool 4,0K /var/tmp 93M /var/www
Petit script pour remplacer une chaîne de caractères dans tous les fichiers d’un certain type du répertoire courant.
#! /bin/bash for file in *.cfg do cp $file $file.bak && sed "s|$1|$2|g" $file.bak > $file done exit 0
exemple
./replace.sh test new_test
Squelette d’un script de synchronisation de serveurs Nagios. C’est le central qui pousse leurs configurations aux serveurs distribués.
#!/bin/bash # This script synchronizes central nagios server with distributed ones. prefix=/usr/local/nagios/etc # configuration of nagios distributed servers. Each one needs application and server properties. # next one is application[2] and server[2] and so on... application=mail application[1]=web server=192.168.1.1 server[1]=192.168.1.2 # loop initialisation is done with -1 in order to get the first loop id with 0. Corresponds to the first row of our tables. # lt value should be equal to the biggest value in table [] i=-1 while [ $i -lt 1 ] # doing our stuff do i=$(($i+1)) # send to all distributed servers configuration files that each ditributed server share with others. echo "sending global configuration files to ${application[$i]} at ${server[$i]}..." rsync -avP --delete --exclude=local --exclude=private --exclude=.bzrignore --exclude=.bzr --exclude=ndomod.cfg $prefix/collecteurs/ system@${server$ # send specific configuration files to each distributed nagios server. This include hosts, contacts, services... echo "sending local configuration files to ${application[$i]} at ${server[$i]}..." rsync -avP --delete $prefix/applications/${application[$i]}/ system@${server[$i]}:$prefix/local/ /bin/echo "synchro with ${application[$i]} distributed nagios server ip ${server[$i]} has been sucessfull" # placeholder for restarting each distributed server. done exit 0
#!/bin/bash # to be executed as nagios or root for file in perfdata *.xml do /usr/bin/find /usr/local/nagios/var/perfdata/ -name $file -exec rm {} \; done echo "cleanup terminated" exit 0
On a souvent besoin de faire une conversion depuis ou vers le format de date epoch, notamment pour le fichier de log de Nagios qui est à ce format. Une simple commande suffit
date -d @1223554404 jeudi 9 octobre 2008, 14:13:24 (UTC+0200)