Update hwmon.sh

This commit is contained in:
alu
2026-08-22 23:10:16 +00:00
parent b66b7cad59
commit 3369adfeeb
+43 -10
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#
# power.sh - Universal CPU Power & Multi-Core Temperature Monitor
# power.sh - Universal CPU Power, Multi-Core & NIC Temperature Monitor
INTERVAL=1.0 # Refresh rate in seconds
@@ -13,7 +13,7 @@ cleanup() {
trap cleanup SIGINT SIGTERM
if [[ $EUID -ne 0 ]]; then
echo "Error: This script must be run as root to access CPU RAPL counters."
echo "Error: This script must be run as root to access CPU RAPL counters and ethtool."
exit 1
fi
@@ -22,6 +22,11 @@ if ! command -v perf &>/dev/null; then
exit 1
fi
if ! command -v ethtool &>/dev/null; then
echo "Error: 'ethtool' command not found. Install with: apt install ethtool"
exit 1
fi
VENDOR_ID=$(grep -m 1 'vendor_id' /proc/cpuinfo | awk '{print $3}')
# --- Detect RAPL Power Events ---
@@ -48,7 +53,6 @@ if [[ ${#EVENTS[@]} -eq 0 ]]; then
fi
# --- Map Thermal Sensors ---
# Arrays to hold ordered (label, file_path) pairs
CORE_LABELS=()
CORE_PATHS=()
PKG_LABEL=""
@@ -60,7 +64,6 @@ for hwmon in /sys/class/hwmon/hwmon*; do
# Intel Sensors (coretemp)
if [[ "$DRIVER_NAME" == "coretemp" ]]; then
# Sort label files numerically (temp1_label, temp2_label...)
for label_file in $(ls -v "$hwmon"/temp*_label 2>/dev/null); do
if [[ -f "$label_file" ]]; then
LABEL=$(cat "$label_file" 2>/dev/null)
@@ -112,11 +115,6 @@ read_temp() {
# Colorize temperature values
format_temp() {
local temp="$1"
if [[ "$temp" == "N/A" ]]; then
echo -e "\e[1;30mN/A\e[0m"
return
fi
if awk "BEGIN {exit !($temp >= 80)}"; then
printf "\e[1;31m%5.1f°C\e[0m" "$temp" # Red (>= 80C)
elif awk "BEGIN {exit !($temp >= 65 && $temp < 80)}"; then
@@ -148,7 +146,7 @@ while true; do
# --- UI Rendering ---
echo "=================================================="
echo " CPU Power & Temperature Monitor "
echo " CPU Power & Hardware Temp Monitor "
echo "=================================================="
echo " Hostname : $(hostname)"
echo " CPU Vendor : $VENDOR_ID"
@@ -190,6 +188,41 @@ while true; do
if [[ -n "$DRAM_WATTS" ]]; then
printf " DRAM Memory Power : \e[1;35m%7.2f Watts\e[0m\n" "$DRAM_WATTS"
fi
# --- Network Interface Temperatures ---
echo "--------------------------------------------------"
echo " Physical NIC Optics Temperatures:"
found_iface=0
# Iterate through all network interfaces
for iface_path in /sys/class/net/*; do
iface="${iface_path##*/}"
[[ "$iface" == "lo" ]] && continue
# Skip virtual interfaces
if [[ -L "$iface_path" && $(readlink -f "$iface_path") == *"/virtual/"* ]]; then
continue
fi
# Grab strictly the current temperature line, avoiding the high/low thresholds
temp_line=$(ethtool -m "$iface" 2>/dev/null | grep -E "^[[:space:]]*Module temperature[[:space:]]*:" -m 1)
if [[ -n "$temp_line" ]]; then
# Extract just the numeric value
iface_temp=$(echo "$temp_line" | awk -F':' '{print $2}' | awk '{print $1}')
# Verify the result is actually a number before trying to format it
if [[ "$iface_temp" =~ ^[0-9]+(\.[0-9]+)?$ || "$iface_temp" =~ ^-[0-9]+(\.[0-9]+)?$ ]]; then
found_iface=1
printf " %-15s: %b\n" "$iface" "$(format_temp "$iface_temp")"
fi
fi
done
if [[ $found_iface -eq 0 ]]; then
echo " No readable optic temperatures found."
fi
echo "=================================================="
echo "Press [CTRL+C] to exit."
done