check_temperature_all.sh
· 6.7 KiB · Bash
Исходник
#!/usr/bin/env bash
# =========================================================
# Nom : check_temp_all.sh
# Objet : Afficher les températures système en console
#
# Description :
# - Affiche les températures système détectées via `sensors` (lm-sensors),
# NVMe et GPU (NVIDIA).
# - Classe les valeurs par catégorie : CPU, Carte mère, RAM, NVMe, GPU, Autres.
# - Colore les valeurs selon leur plage :
# < 30°C → Bleu
# 30 – 60°C → Vert
# 60 – 80°C → Jaune
# > 80°C → Rouge
# - Pour CPU, Carte mère et NVMe, affiche aussi la température "high" (max) en gris.
# - Option `--raw` : affiche la sortie brute de `sensors`.
# - Option `--watch` : rafraîchit toutes les 2 secondes (équivalent à `watch -c script.sh`).
#
# Niveaux visuels :
# 🔵 < 30°C | 🟢 30–60°C | 🟡 60–80°C | 🔴 > 80°C
#
# Utilisation :
# ./check_temp_all.sh → Affichage normal
# ./check_temp_all.sh --raw → Sortie brute
# ./check_temp_all.sh --watch → Rafraîchit toutes les 2s avec couleurs
#
# Prérequis :
# - Linux (Debian/Ubuntu/Proxmox…)
# - lm-sensors installé et configuré (`sudo sensors-detect`)
# - (Optionnel) nvidia-smi pour GPU NVIDIA
# =========================================================
set -euo pipefail
LC_ALL=C
# Couleurs ANSI
BLUE=$'\033[1;34m'
RED=$'\033[1;31m'
GREEN=$'\033[1;32m'
YELLOW=$'\033[1;33m'
CYAN=$'\033[1;36m'
GRAY=$'\033[90m'
RESET=$'\033[0m'
BOLD=$'\033[1m'
show_help() {
printf "%b\n" "${BOLD}Usage:${RESET} $0 [--raw] [--watch]"
echo " --raw : Affiche la sortie brute de sensors"
echo " --watch : Rafraîchit toutes les 2 secondes (équivalent à: watch -c script.sh)"
echo " --help : Affiche cette aide"
}
print_section() {
local title="$1"
printf "\n%b--- %s ---%b\n" "$CYAN" "$title" "$RESET"
}
# Colorise un nombre (°C) selon seuils
colorize_number() {
local n="$1"
awk -v n="$n" -v b="$BLUE" -v g="$GREEN" -v y="$YELLOW" -v r="$RED" -v reset="$RESET" 'BEGIN{
c = (n < 30) ? b : (n < 60) ? g : (n < 80) ? y : r;
printf "%s%.1f°C%s", c, n, reset;
}'
}
# Extrait le premier nombre décimal trouvé depuis une chaîne
first_number() {
grep -Eo '[+-]?[0-9]+(\.[0-9]+)?' | head -n1 | sed -E 's/^\+?//'
}
# Extrait la valeur après "high ="
extract_high() {
local line="$1"
local h
h="$(echo "$line" | grep -Eo 'high[[:space:]]*=[[:space:]]*[+-]?[0-9]+(\.[0-9]+)?' | tail -n1 | awk '{print $NF}' | sed -E 's/^\+?//')"
echo -n "$h"
}
# Affiche "Nom temp_coloree (max: …)"
render_with_high() {
local name="$1"
local temp_num="$2"
local high_num="$3"
local colored
colored="$(colorize_number "$temp_num")"
if [[ -n "${high_num:-}" ]]; then
printf "%-20s %s %b(max: %.1f°C)%b\n" "$name" "$colored" "$GRAY" "$high_num" "$RESET"
else
printf "%-20s %s\n" "$name" "$colored"
fi
}
# Options
RAW_MODE=false
WATCH_MODE=false
for arg in "$@"; do
case "$arg" in
--raw) RAW_MODE=true ;;
--watch) WATCH_MODE=true ;;
--help) show_help; exit 0 ;;
*) printf "%bOption inconnue:%b %s\n" "$RED" "$RESET" "$arg"; exit 1 ;;
esac
done
# Mode --raw
if $RAW_MODE; then sensors; exit 0; fi
# Mode --watch : relance le script avec watch -c
if $WATCH_MODE; then
exec watch -c "$0"
fi
collect_temps() {
local sensors_out
sensors_out="$(sensors 2>/dev/null || true)"
printf "%b\n" "${BOLD}==== TEMPÉRATURES SYSTÈME (auto) ====${RESET}"
# CPU
print_section "CPU"
cpu_lines="$(echo "$sensors_out" | grep -E '^(Package id [0-9]+|Core [0-9]+):' || true)"
if [[ -n "${cpu_lines:-}" ]]; then
while IFS= read -r line; do
name="${line%%:*}"; name="${name%:}"
after="${line#*:*}"
temp_num="$(echo "$after" | first_number)"
high_num="$(extract_high "$line")"
[[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" "$high_num"
done <<< "$cpu_lines"
else
echo "(aucune donnée)"
fi
# Carte mère
print_section "Carte mère"
mb_lines="$(echo "$sensors_out" | grep -E '^(CPUTIN|SYSTIN|PECI Agent 0):' || true)"
if [[ -n "${mb_lines:-}" ]]; then
while IFS= read -r line; do
name="${line%%:*}"
after="${line#*:*}"
temp_num="$(echo "$after" | first_number)"
high_num="$(extract_high "$line")"
[[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" "$high_num"
done <<< "$mb_lines"
else
echo "(aucune donnée)"
fi
# RAM
print_section "RAM"
ram_lines="$(echo "$sensors_out" | grep -Ei 'DIMM' || true)"
if [[ -n "${ram_lines:-}" ]]; then
while IFS= read -r line; do
if grep -q ':' <<< "$line"; then
name="${line%%:*}"
after="${line#*:*}"
temp_num="$(echo "$after" | first_number)"
[[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" ""
fi
done <<< "$ram_lines"
else
echo "(aucune donnée)"
fi
# NVMe
print_section "NVMe"
nvme_lines="$(echo "$sensors_out" | awk '
/^nvme-/ {dev=$1; next}
/Composite:/ && dev!="" {gsub(/^[ \t]+/,"",$0); printf dev": %s\n", $0}
' || true)"
if [[ -n "${nvme_lines:-}" ]]; then
while IFS= read -r line; do
name="${line%%:*}"
after="${line#*:*}"
temp_num="$(echo "$after" | first_number)"
high_num="$(extract_high "$line")"
[[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" "$high_num"
done <<< "$nvme_lines"
else
echo "(aucune donnée)"
fi
# GPU NVIDIA
print_section "GPU"
if command -v nvidia-smi &>/dev/null; then
nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader | while read -r t; do
printf -v tfmt "%.1f" "$t"
render_with_high "NVIDIA GPU" "$tfmt" ""
done
else
echo "(aucune donnée)"
fi
# Autres
print_section "Autres"
other_lines="$(echo "$sensors_out" | grep -i 'temp' | grep -Ev '^(Core|Package id|CPUTIN|SYSTIN|PECI Agent 0|DIMM|nvme-|Adapter:)' || true)"
if [[ -n "${other_lines:-}" ]]; then
while IFS= read -r line; do
if grep -q ':' <<< "$line"; then
name="${line%%:*}"
after="${line#*:*}"
temp_num="$(echo "$after" | first_number)"
[[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" ""
fi
done <<< "$other_lines"
else
echo "(aucune donnée)"
fi
printf "%b\n" "${BOLD}=====================================${RESET}"
}
collect_temps
| 1 | #!/usr/bin/env bash |
| 2 | # ========================================================= |
| 3 | # Nom : check_temp_all.sh |
| 4 | # Objet : Afficher les températures système en console |
| 5 | # |
| 6 | # Description : |
| 7 | # - Affiche les températures système détectées via `sensors` (lm-sensors), |
| 8 | # NVMe et GPU (NVIDIA). |
| 9 | # - Classe les valeurs par catégorie : CPU, Carte mère, RAM, NVMe, GPU, Autres. |
| 10 | # - Colore les valeurs selon leur plage : |
| 11 | # < 30°C → Bleu |
| 12 | # 30 – 60°C → Vert |
| 13 | # 60 – 80°C → Jaune |
| 14 | # > 80°C → Rouge |
| 15 | # - Pour CPU, Carte mère et NVMe, affiche aussi la température "high" (max) en gris. |
| 16 | # - Option `--raw` : affiche la sortie brute de `sensors`. |
| 17 | # - Option `--watch` : rafraîchit toutes les 2 secondes (équivalent à `watch -c script.sh`). |
| 18 | # |
| 19 | # Niveaux visuels : |
| 20 | # 🔵 < 30°C | 🟢 30–60°C | 🟡 60–80°C | 🔴 > 80°C |
| 21 | # |
| 22 | # Utilisation : |
| 23 | # ./check_temp_all.sh → Affichage normal |
| 24 | # ./check_temp_all.sh --raw → Sortie brute |
| 25 | # ./check_temp_all.sh --watch → Rafraîchit toutes les 2s avec couleurs |
| 26 | # |
| 27 | # Prérequis : |
| 28 | # - Linux (Debian/Ubuntu/Proxmox…) |
| 29 | # - lm-sensors installé et configuré (`sudo sensors-detect`) |
| 30 | # - (Optionnel) nvidia-smi pour GPU NVIDIA |
| 31 | # ========================================================= |
| 32 | |
| 33 | set -euo pipefail |
| 34 | LC_ALL=C |
| 35 | |
| 36 | # Couleurs ANSI |
| 37 | BLUE=$'\033[1;34m' |
| 38 | RED=$'\033[1;31m' |
| 39 | GREEN=$'\033[1;32m' |
| 40 | YELLOW=$'\033[1;33m' |
| 41 | CYAN=$'\033[1;36m' |
| 42 | GRAY=$'\033[90m' |
| 43 | RESET=$'\033[0m' |
| 44 | BOLD=$'\033[1m' |
| 45 | |
| 46 | show_help() { |
| 47 | printf "%b\n" "${BOLD}Usage:${RESET} $0 [--raw] [--watch]" |
| 48 | echo " --raw : Affiche la sortie brute de sensors" |
| 49 | echo " --watch : Rafraîchit toutes les 2 secondes (équivalent à: watch -c script.sh)" |
| 50 | echo " --help : Affiche cette aide" |
| 51 | } |
| 52 | |
| 53 | print_section() { |
| 54 | local title="$1" |
| 55 | printf "\n%b--- %s ---%b\n" "$CYAN" "$title" "$RESET" |
| 56 | } |
| 57 | |
| 58 | # Colorise un nombre (°C) selon seuils |
| 59 | colorize_number() { |
| 60 | local n="$1" |
| 61 | awk -v n="$n" -v b="$BLUE" -v g="$GREEN" -v y="$YELLOW" -v r="$RED" -v reset="$RESET" 'BEGIN{ |
| 62 | c = (n < 30) ? b : (n < 60) ? g : (n < 80) ? y : r; |
| 63 | printf "%s%.1f°C%s", c, n, reset; |
| 64 | }' |
| 65 | } |
| 66 | |
| 67 | # Extrait le premier nombre décimal trouvé depuis une chaîne |
| 68 | first_number() { |
| 69 | grep -Eo '[+-]?[0-9]+(\.[0-9]+)?' | head -n1 | sed -E 's/^\+?//' |
| 70 | } |
| 71 | |
| 72 | # Extrait la valeur après "high =" |
| 73 | extract_high() { |
| 74 | local line="$1" |
| 75 | local h |
| 76 | h="$(echo "$line" | grep -Eo 'high[[:space:]]*=[[:space:]]*[+-]?[0-9]+(\.[0-9]+)?' | tail -n1 | awk '{print $NF}' | sed -E 's/^\+?//')" |
| 77 | echo -n "$h" |
| 78 | } |
| 79 | |
| 80 | # Affiche "Nom temp_coloree (max: …)" |
| 81 | render_with_high() { |
| 82 | local name="$1" |
| 83 | local temp_num="$2" |
| 84 | local high_num="$3" |
| 85 | local colored |
| 86 | colored="$(colorize_number "$temp_num")" |
| 87 | if [[ -n "${high_num:-}" ]]; then |
| 88 | printf "%-20s %s %b(max: %.1f°C)%b\n" "$name" "$colored" "$GRAY" "$high_num" "$RESET" |
| 89 | else |
| 90 | printf "%-20s %s\n" "$name" "$colored" |
| 91 | fi |
| 92 | } |
| 93 | |
| 94 | # Options |
| 95 | RAW_MODE=false |
| 96 | WATCH_MODE=false |
| 97 | for arg in "$@"; do |
| 98 | case "$arg" in |
| 99 | --raw) RAW_MODE=true ;; |
| 100 | --watch) WATCH_MODE=true ;; |
| 101 | --help) show_help; exit 0 ;; |
| 102 | *) printf "%bOption inconnue:%b %s\n" "$RED" "$RESET" "$arg"; exit 1 ;; |
| 103 | esac |
| 104 | done |
| 105 | |
| 106 | # Mode --raw |
| 107 | if $RAW_MODE; then sensors; exit 0; fi |
| 108 | |
| 109 | # Mode --watch : relance le script avec watch -c |
| 110 | if $WATCH_MODE; then |
| 111 | exec watch -c "$0" |
| 112 | fi |
| 113 | |
| 114 | collect_temps() { |
| 115 | local sensors_out |
| 116 | sensors_out="$(sensors 2>/dev/null || true)" |
| 117 | |
| 118 | printf "%b\n" "${BOLD}==== TEMPÉRATURES SYSTÈME (auto) ====${RESET}" |
| 119 | |
| 120 | # CPU |
| 121 | print_section "CPU" |
| 122 | cpu_lines="$(echo "$sensors_out" | grep -E '^(Package id [0-9]+|Core [0-9]+):' || true)" |
| 123 | if [[ -n "${cpu_lines:-}" ]]; then |
| 124 | while IFS= read -r line; do |
| 125 | name="${line%%:*}"; name="${name%:}" |
| 126 | after="${line#*:*}" |
| 127 | temp_num="$(echo "$after" | first_number)" |
| 128 | high_num="$(extract_high "$line")" |
| 129 | [[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" "$high_num" |
| 130 | done <<< "$cpu_lines" |
| 131 | else |
| 132 | echo "(aucune donnée)" |
| 133 | fi |
| 134 | |
| 135 | # Carte mère |
| 136 | print_section "Carte mère" |
| 137 | mb_lines="$(echo "$sensors_out" | grep -E '^(CPUTIN|SYSTIN|PECI Agent 0):' || true)" |
| 138 | if [[ -n "${mb_lines:-}" ]]; then |
| 139 | while IFS= read -r line; do |
| 140 | name="${line%%:*}" |
| 141 | after="${line#*:*}" |
| 142 | temp_num="$(echo "$after" | first_number)" |
| 143 | high_num="$(extract_high "$line")" |
| 144 | [[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" "$high_num" |
| 145 | done <<< "$mb_lines" |
| 146 | else |
| 147 | echo "(aucune donnée)" |
| 148 | fi |
| 149 | |
| 150 | # RAM |
| 151 | print_section "RAM" |
| 152 | ram_lines="$(echo "$sensors_out" | grep -Ei 'DIMM' || true)" |
| 153 | if [[ -n "${ram_lines:-}" ]]; then |
| 154 | while IFS= read -r line; do |
| 155 | if grep -q ':' <<< "$line"; then |
| 156 | name="${line%%:*}" |
| 157 | after="${line#*:*}" |
| 158 | temp_num="$(echo "$after" | first_number)" |
| 159 | [[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" "" |
| 160 | fi |
| 161 | done <<< "$ram_lines" |
| 162 | else |
| 163 | echo "(aucune donnée)" |
| 164 | fi |
| 165 | |
| 166 | # NVMe |
| 167 | print_section "NVMe" |
| 168 | nvme_lines="$(echo "$sensors_out" | awk ' |
| 169 | /^nvme-/ {dev=$1; next} |
| 170 | /Composite:/ && dev!="" {gsub(/^[ \t]+/,"",$0); printf dev": %s\n", $0} |
| 171 | ' || true)" |
| 172 | if [[ -n "${nvme_lines:-}" ]]; then |
| 173 | while IFS= read -r line; do |
| 174 | name="${line%%:*}" |
| 175 | after="${line#*:*}" |
| 176 | temp_num="$(echo "$after" | first_number)" |
| 177 | high_num="$(extract_high "$line")" |
| 178 | [[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" "$high_num" |
| 179 | done <<< "$nvme_lines" |
| 180 | else |
| 181 | echo "(aucune donnée)" |
| 182 | fi |
| 183 | |
| 184 | # GPU NVIDIA |
| 185 | print_section "GPU" |
| 186 | if command -v nvidia-smi &>/dev/null; then |
| 187 | nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader | while read -r t; do |
| 188 | printf -v tfmt "%.1f" "$t" |
| 189 | render_with_high "NVIDIA GPU" "$tfmt" "" |
| 190 | done |
| 191 | else |
| 192 | echo "(aucune donnée)" |
| 193 | fi |
| 194 | |
| 195 | # Autres |
| 196 | print_section "Autres" |
| 197 | other_lines="$(echo "$sensors_out" | grep -i 'temp' | grep -Ev '^(Core|Package id|CPUTIN|SYSTIN|PECI Agent 0|DIMM|nvme-|Adapter:)' || true)" |
| 198 | if [[ -n "${other_lines:-}" ]]; then |
| 199 | while IFS= read -r line; do |
| 200 | if grep -q ':' <<< "$line"; then |
| 201 | name="${line%%:*}" |
| 202 | after="${line#*:*}" |
| 203 | temp_num="$(echo "$after" | first_number)" |
| 204 | [[ -n "${temp_num:-}" ]] && render_with_high "$name" "$temp_num" "" |
| 205 | fi |
| 206 | done <<< "$other_lines" |
| 207 | else |
| 208 | echo "(aucune donnée)" |
| 209 | fi |
| 210 | |
| 211 | printf "%b\n" "${BOLD}=====================================${RESET}" |
| 212 | } |
| 213 | |
| 214 | collect_temps |
| 215 |