#!/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