amd-monitor/amd-monitor.sh

68 lines
1.9 KiB
Bash
Raw Permalink Normal View History

2025-11-05 17:45:27 -05:00
#!/bin/bash
# AMD Laptop APU + GPU Live Monitor for ASUS TUF (Debian 13)
# Author: MarkMental
# --- Configuration ---
APU_HWMON="/sys/class/hwmon/hwmon5" # APU sensor
GPU_HWMON="/sys/class/drm/card0/device/hwmon/hwmon4" # dGPU sensor
INTERVAL=1 # seconds between refreshes
LOGFILE="power_log.csv" # CSV output file
MAX_LINES=500 # limit to last 500 lines
# --- Helpers ---
read_watts() {
local file=$1
if [ -f "$file" ]; then
awk '{printf "%.2f", $1/1000000}' "$file"
else
echo "0.00"
fi
}
read_temp() {
local file=$1
if [ -f "$file" ]; then
awk '{printf "%.1f", $1/1000}' "$file"
else
echo "N/A"
fi
}
# --- Ensure CSV exists and has header ---
if [ ! -f "$LOGFILE" ]; then
echo "timestamp,apu_w,gpu_w,total_w,apu_temp,gpu_temp" > "$LOGFILE"
fi
# --- Main loop ---
clear
echo "🧠 AMD APU / GPU Live Power Monitor (Debian 13 TUF)"
echo "--------------------------------------------------"
while true; do
APU_PWR=$(read_watts "$APU_HWMON/power1_input")
GPU_PWR=$(read_watts "$GPU_HWMON/power1_average")
APU_TEMP=$(read_temp "$APU_HWMON/temp1_input")
GPU_TEMP=$(read_temp "$GPU_HWMON/temp1_input")
TOTAL=$(awk -v a="$APU_PWR" -v g="$GPU_PWR" 'BEGIN {printf "%.2f", a+g}')
TIME=$(date -Iseconds)
# --- Print live readout ---
tput cup 2 0
echo "APU Power: ${APU_PWR} W | Temp: ${APU_TEMP} °C "
echo "GPU Power: ${GPU_PWR} W | Temp: ${GPU_TEMP} °C "
echo "-----------------------------------------------"
echo "Total Power: ${TOTAL} W"
echo "Press Ctrl+C to exit"
# --- Append to CSV ---
echo "$TIME,$APU_PWR,$GPU_PWR,$TOTAL,$APU_TEMP,$GPU_TEMP" >> "$LOGFILE"
# --- Keep only last $MAX_LINES lines ---
LINES=$(wc -l < "$LOGFILE")
if (( LINES > MAX_LINES )); then
tail -n $MAX_LINES "$LOGFILE" > "${LOGFILE}.tmp" && mv "${LOGFILE}.tmp" "$LOGFILE"
fi
sleep $INTERVAL
done