mirror of
https://github.com/AlexMacocian/ServerManagementUtils.git
synced 2026-07-15 15:19:58 +00:00
102 lines
3.5 KiB
Bash
102 lines
3.5 KiB
Bash
#!/bin/bash
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
. "${SCRIPT_DIR}/../share/logging.sh"
|
|
. "${SCRIPT_DIR}/../share/os-detection.sh"
|
|
|
|
LOGFILE=/var/log/temperature.log
|
|
OS_TYPE=$(DetectOS)
|
|
|
|
# Create log file with proper error handling
|
|
if ! touch "$LOGFILE" 2>/dev/null; then
|
|
LogError "Cannot create log file $LOGFILE"
|
|
exit 1
|
|
fi
|
|
chmod 644 "$LOGFILE" 2>/dev/null
|
|
|
|
# Check thermal monitoring support
|
|
if ! IsThermalMonitoringSupported; then
|
|
if [[ "$OS_TYPE" == "macos" ]]; then
|
|
LogError "Temperature monitoring not available on macOS"
|
|
LogError "Install osx-cpu-temp: brew install osx-cpu-temp"
|
|
LogError "Or use powermetrics (requires sudo)"
|
|
else
|
|
LogError "No thermal zones found in /sys/class/thermal/"
|
|
LogError "This system may not support thermal monitoring"
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
LogInformation "Starting temperature monitoring (OS: $OS_TYPE)"
|
|
|
|
for i in {1..6}; do
|
|
timestamp=$(GetTimestamp)
|
|
|
|
if [[ "$OS_TYPE" == "macos" ]]; then
|
|
# Try osx-cpu-temp first
|
|
if command -v osx-cpu-temp &> /dev/null; then
|
|
temp_output=$(osx-cpu-temp 2>/dev/null)
|
|
if [[ $? -eq 0 && -n "$temp_output" ]]; then
|
|
# Output format: "50.0°C"
|
|
echo "$timestamp CPU $temp_output" >> "$LOGFILE"
|
|
fi
|
|
# Fallback to powermetrics (requires sudo)
|
|
elif [[ -x /usr/bin/powermetrics ]]; then
|
|
# Run powermetrics for 1 second and extract temperature
|
|
temp_data=$(sudo powermetrics -n 1 -i 1000 --samplers smc 2>/dev/null | grep -i "CPU die temperature" | head -1)
|
|
if [[ -n "$temp_data" ]]; then
|
|
# Extract temperature value (format: "CPU die temperature: 50.50 C")
|
|
temp_value=$(echo "$temp_data" | grep -o '[0-9.]\+ C' | head -1)
|
|
if [[ -n "$temp_value" ]]; then
|
|
echo "$timestamp CPU ${temp_value}" >> "$LOGFILE"
|
|
fi
|
|
fi
|
|
fi
|
|
else
|
|
# Linux thermal zone reading
|
|
# Use arrays to avoid complex pipe chains that can hang
|
|
types=()
|
|
temps=()
|
|
|
|
# Read thermal zone types
|
|
for type_file in /sys/class/thermal/thermal_zone*/type; do
|
|
if [[ -r "$type_file" ]]; then
|
|
type_value=$(cat "$type_file" 2>/dev/null)
|
|
if [[ -n "$type_value" ]]; then
|
|
types+=("$type_value")
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# Read thermal zone temperatures
|
|
for temp_file in /sys/class/thermal/thermal_zone*/temp; do
|
|
if [[ -r "$temp_file" ]]; then
|
|
temp_value=$(cat "$temp_file" 2>/dev/null)
|
|
if [[ -n "$temp_value" && "$temp_value" =~ ^[0-9]+$ ]]; then
|
|
# Convert millidegrees to degrees with one decimal place
|
|
temp_celsius=$(echo "scale=1; $temp_value / 1000" | bc 2>/dev/null)
|
|
if [[ $? -eq 0 ]]; then
|
|
temps+=("${temp_celsius}°C")
|
|
else
|
|
temps+=("${temp_value}m°C")
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# Output paired type and temperature data
|
|
for j in "${!types[@]}"; do
|
|
if [[ $j -lt ${#temps[@]} ]]; then
|
|
echo "$timestamp ${types[$j]} ${temps[$j]}" >> "$LOGFILE"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
LogInformation "Temperature data collected (iteration $i/6)"
|
|
|
|
if [ $i -lt 6 ]; then
|
|
sleep 10
|
|
fi
|
|
done
|
|
|
|
LogInformation "Temperature monitoring completed" |