Añadido nuevo script

This commit is contained in:
2026-01-25 13:15:08 +01:00
parent 789f46aab2
commit 81e5a42fed
+133
View File
@@ -0,0 +1,133 @@
#!/usr/bin/env bash
GREEN=$'\033[0;32m'
YELLOW=$'\033[1;33m'
RED=$'\033[0;31m'
NC=$'\033[0m'
USE_COLOR=0
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
USE_COLOR=1
fi
deps=(smartctl lsblk awk)
for dep in "${deps[@]}"; do
if ! command -v "$dep" >/dev/null 2>&1; then
echo "Falta dependencia: $dep" >&2
exit 1
fi
done
if sudo -n true 2>/dev/null; then
SUDO="sudo -n"
else
SUDO="sudo"
if ! $SUDO -v; then
SUDO=""
echo "Aviso: sin sudo, no se leeran datos SMART." >&2
fi
fi
header="Disco\tModelo\tSerial\tCapacidad\tTemp\tUptime(h)\tUptime\tMontaje\tSMART\tRealloc\tPending\tOffline"
rows=()
shopt -s nullglob
disks=(/dev/sd? /dev/nvme?n?)
shopt -u nullglob
if [ "${#disks[@]}" -eq 0 ]; then
echo "No se encontraron discos en /dev/sd? o /dev/nvme?n?" >&2
exit 1
fi
for disk in "${disks[@]}"; do
if [ -n "$SUDO" ]; then
model=$($SUDO smartctl -i "$disk" | awk -F: '/Device Model|Model Number/ {gsub(/^ +| +$/, "", $2); print $2; exit}')
serial=$($SUDO smartctl -i "$disk" | awk -F: '/Serial Number/ {gsub(/^ +| +$/, "", $2); print $2; exit}')
health=$($SUDO smartctl -H "$disk" | awk -F: '/SMART overall-health|SMART Health Status/ {gsub(/^ +| +$/, "", $2); print $2; exit}')
else
model="—"
serial="—"
health="—"
fi
[ -z "$model" ] && model="—"
[ -z "$serial" ] && serial="—"
[ -z "$health" ] && health="—"
size_bytes=$(lsblk -ndo SIZE -b "$disk")
if [ "$size_bytes" -ge 1000000000000 ]; then
size=$(awk -v s="$size_bytes" 'BEGIN {printf "%.2f", s/1024/1024/1024/1024}')
size="${size}T"
else
size=$(awk -v s="$size_bytes" 'BEGIN {printf "%.2f", s/1024/1024/1024}')
size="${size}G"
fi
temp=$($SUDO smartctl -A "$disk" 2>/dev/null | awk '/Temperature_Celsius|Current_Temperature|Airflow_Temperature_Cel|Temperature/ {print $10; exit}')
[ -z "$temp" ] && temp="N/A"
if [[ "$temp" != "N/A" ]]; then
if [ "$temp" -lt 45 ]; then
color=$GREEN
elif [ "$temp" -lt 51 ]; then
color=$YELLOW
else
color=$RED
fi
if [ "$USE_COLOR" -eq 1 ]; then
temp_color="${color}${temp}°C${NC}"
else
temp_color="${temp}°C"
fi
else
temp_color="N/A"
fi
uptime_raw=$($SUDO smartctl -A "$disk" 2>/dev/null | awk '/Power_On_Hours/ {print $10}' | grep -o '^[0-9]*')
if [ -z "$uptime_raw" ]; then
uptime_raw="—"
uptime_fancy="—"
else
years=$(( uptime_raw / 8760 ))
days=$(( (uptime_raw % 8760) / 24 ))
uptime_fancy="${years}y ${days}d"
fi
mountpoint=$(lsblk -ndo MOUNTPOINT "${disk}"* | grep -v "^$" | head -n 1)
[ -z "$mountpoint" ] && mountpoint="—"
reallocated=$($SUDO smartctl -A "$disk" 2>/dev/null | awk '/Reallocated_Sector_Ct|Reallocated_Event_Count/ {print $10; exit}')
pending=$($SUDO smartctl -A "$disk" 2>/dev/null | awk '/Current_Pending_Sector/ {print $10; exit}')
offline=$($SUDO smartctl -A "$disk" 2>/dev/null | awk '/Offline_Uncorrectable/ {print $10; exit}')
[ -z "$reallocated" ] && reallocated="—"
[ -z "$pending" ] && pending="—"
[ -z "$offline" ] && offline="—"
line=$(printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" \
"$disk" "$model" "$serial" "$size" "$temp_color" "$uptime_raw" "$uptime_fancy" "$mountpoint" "$health" "$reallocated" "$pending" "$offline")
rows+=("$line")
done
{
echo -e "$header"
printf '%s\n' "${rows[@]}"
} | column -t -s $'\t'
cat <<'EOF'
Leyenda:
Disco : Dispositivo detectado.
Modelo : Modelo reportado por SMART.
Serial : Numero de serie del disco.
Capacidad : Tamano total del disco.
Temp : Temperatura actual (SMART).
Uptime(h) : Horas encendido (Power_On_Hours).
Uptime : Antiguedad aproximada (anos/dias).
Montaje : Punto de montaje detectado.
SMART : Estado general de salud.
Realloc : Sectores reasignados.
Pending : Sectores pendientes de reasignacion.
Offline : Sectores no corregibles offline.
N/A / — : Dato no disponible.
EOF