Bonjour à tous, après vous avoir montré comment surveiller simplement la charge ou l’utilisation de la mémoire sous Linux, je vais aujourd’hui vous montrer comment surveiller l’espace disque et déclencher une action lorsqu’un seuil est atteint.
Pour ça, on va simplement utiliser df, grep, awk, cut et if/then :
#!/bin/bash
# Ex alert_threshold=90 -> You will be alerted if your occupied space is more or equal than threshold
alert_threshold=90
# Email for alert
rcpt1="nospam@nospam.com"
# Hostname
host=`hostname -f`
i=0
for disk in `df |grep dev |grep -v tmpfs |grep -v udev| awk -F" " '{print $1}' | cut -d/ -f3`
do
space_use=`df | grep $disk | awk -F" " '{print $5}' | cut -d% -f1`
if [ "$space_use" -gt "$alert_threshold" ]
then
i=$((i + 1))
over_threshold["$i"]="$disk"
fi
done
if [ ${#over_threshold[*]} -gt 0 ]
then
echo >/tmp/mail.txt
subject="Disk space over threshold on $host"
echo "">> /tmp/mail.txt
echo "Disks with space problem with more than $alert_threshold% occupied space" >> /tmp/mail.txt
echo "">> /tmp/mail.txt
for disk in ${over_threshold[*]}
do
info_disk=(`df -h | grep $disk | awk -F" " '{print $6, $2, $3, $4, $5}'`)
echo "- Mount point : ${info_disk[O]} - Total space : ${info_disk[1]} - Used space : ${info_disk[2]} - Free space : ${info_disk[3]} - Used space in percents : ${info_disk[4]}" >> /tmp/mail.txt ; echo "" >> /tmp/mail.txt
done
# Now send the email alert
cat /tmp/mail.txt |mail -s "$subject" $rcpt1
fi
Il suffit de le mettre dans votre crontab pour recevoir une alerte quand vous utilisez trop d’espace sur vos disques. Vous pouvez aussi remplacer l’envoi d’email par les actions de votre choix. Le script est téléchargeable directement ici : diskcheck.sh ou avec wget :
wget --no-check-certificate https://www.christophe-casalegno.com/tools/diskcheck.sh ; chmod +x diskcheck.sh
Enjoy !
Christophe Casalegno
Vous pouvez me suivre sur : Twitter | Facebook | Linkedin | Telegram | Youtube
Laisser un commentaire
Ajouter un commentaire