ajout du script check_smart

This commit is contained in:
2026-03-18 08:06:25 +01:00
parent b2c083eb8d
commit 8074151300
3 changed files with 133 additions and 1 deletions

View File

@@ -0,0 +1,66 @@
#!/bin/bash
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
LOG_BIN="/opt/monitoring/bin/log-cli.php"
# --- Vérification ROOT ---
if [ "${EUID}" -ne 0 ]; then
echo "ERREUR : Ce script doit être exécuté en tant que root." >&2
$LOG_BIN ERROR "internal_error" "Tentative d'exécution sans privilèges root."
exit 1
fi
# --- Vérification et installation de smartctl ---
if ! command -v smartctl >/dev/null 2>&1; then
# On tente l'installation (nécessite root, ce qui est le cas via cron)
if command -v apt-get >/dev/null 2>&1; then
apt-get update && apt-get install -y smartmontools
fi
# Re-vérification après tentative
if ! command -v smartctl >/dev/null 2>&1; then
$LOG_BIN ERROR "internal_error" "smartctl non trouvé et installation impossible."
exit 1
fi
fi
# On récupère les disques qui ont un transport physique (SATA, NVMe, USB)
# Cela exclut d'office les /dev/mapper, /dev/dm-X, /dev/loopX
DISKS=$(lsblk -dno NAME,TRAN | awk '$2!="" {print "/dev/"$1}')
for disk in $DISKS; do
# Vérification : est-ce que smartctl peut lire ce périphérique ?
# --scan-open vérifie si le disque est capable de répondre
if ! smartctl -i "$disk" | grep -q "SMART support is: Enabled" 2>/dev/null; then
# On peut logguer en INFO que le disque est ignoré car non-SMART (ex: clé USB basique)
continue
fi
# 1. État de santé global
smart_output=$(smartctl -H "$disk" 2>/dev/null)
exit_code=$?
if [ $exit_code -ne 0 ]; then
$LOG_BIN CRITICAL "smart_health_bad" "État de santé PHYSIQUE CRITIQUE sur $disk"
else
# 2. Température
temp=$(smartctl -A "$disk" 2>/dev/null | awk '/Temperature_Celsius/ {print $10}' | head -n 1)
[ -z "$temp" ] && temp=$(smartctl -a "$disk" 2>/dev/null | awk '/Temperature:/ {print $2}' | head -n 1)
if [ -n "$temp" ]; then
if [ "$temp" -ge 60 ]; then
$LOG_BIN WARNING "disk_temp_high" "Surchauffe physique sur $disk : ${temp}°C"
fi
fi
$LOG_BIN INFO "smart_health_ok" "Disque physique $disk sain."
fi
done