Multiple changes
This commit is contained in:
parent
147cebc70d
commit
155655cda5
@ -101,8 +101,8 @@ function install_packages {
|
|||||||
;;
|
;;
|
||||||
[nN])
|
[nN])
|
||||||
# If the user chooses not to install lm-sensors, exit the script with a zero status code
|
# If the user chooses not to install lm-sensors, exit the script with a zero status code
|
||||||
msg "Decided to not install lm-sensors. The mod cannot run without it. Exiting..."
|
msgb "Decided to not install lm-sensors. The mod cannot run without it. Exiting..."
|
||||||
exit 0
|
err "lm-sensors is required. Exiting..."
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# If the user enters an invalid input, print an error message and exit the script with a non-zero status code
|
# If the user enters an invalid input, print an error message and exit the script with a non-zero status code
|
||||||
@ -110,72 +110,100 @@ function install_packages {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Check if lm-sensors is installed correctly and exit if not
|
||||||
|
if (! command -v sensors &>/dev/null); then
|
||||||
|
err "lm-sensors installation failed or 'sensors' command is not available. Please install lm-sensors manually and re-run the script."
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function configure {
|
function configure {
|
||||||
SENSORS_DETECTED=false
|
SENSORS_DETECTED=false
|
||||||
local sensorsOutput
|
local sensorsOutput
|
||||||
|
|
||||||
# Load sensor data
|
# Collect lm-sensors output
|
||||||
if [ "$DEBUG_REMOTE" = true ]; then
|
if [ "$DEBUG_REMOTE" = true ]; then
|
||||||
warn "Remote debugging is used. Sensor readings from dump file $DEBUG_JSON_FILE will be used."
|
warn "Remote debugging is used. Sensor readings from dump file $DEBUG_JSON_FILE will be used."
|
||||||
warn "Remote debugging is used. UPS readings from dump file $DEBUG_UPS_FILE will be used."
|
warn "Remote debugging is used. UPS readings from dump file $DEBUG_UPS_FILE will be used."
|
||||||
sensorsOutput=$(cat "$DEBUG_JSON_FILE")
|
sensorsOutput=$(cat "$DEBUG_JSON_FILE")
|
||||||
else
|
else
|
||||||
sensorsOutput=$(sensors -j 2>/dev/null | python3 -m json.tool)
|
sensorsOutput=$(sensors -j 2>/dev/null)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Apply lm-sensors sanitization
|
||||||
|
sensorsOutput=$(sanitize_sensors_output "$sensorsOutput")
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
err "Sensor output error.\n\nCommand output:\n${sensorsOutput}\n\nExiting..."
|
err "Sensor output error.\n\nCommand output:\n${sensorsOutput}\n\nExiting..."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#### CPU ####
|
#### CPU ####
|
||||||
msgb "\n=== Detecting CPU temperature sensors ==="
|
msgb "\n=== Detecting CPU temperature sensors ==="
|
||||||
ENABLE_CPU=false
|
ENABLE_CPU=false
|
||||||
local cpuList=()
|
local cpuList=""
|
||||||
for item in "${KNOWN_CPU_SENSORS[@]}"; do
|
local cpuCount=0
|
||||||
if echo "$sensorsOutput" | grep -q "$item"; then
|
|
||||||
cpuList+=("$item")
|
|
||||||
ENABLE_CPU=true
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ "$ENABLE_CPU" = true ]; then
|
# Find all CPU sensors that match known patterns
|
||||||
info "Detected CPU sensors (${#cpuList[@]}): $(IFS=,; echo "${cpuList[*]}")"
|
for pattern in "${KNOWN_CPU_SENSORS[@]}"; do
|
||||||
SENSORS_DETECTED=true
|
found_sensors=$(echo "$sensorsOutput" | grep -o "\"${pattern}[^\"]*\"" | sed 's/"//g')
|
||||||
while true; do
|
if [ -n "$found_sensors" ]; then
|
||||||
local choice=$(ask "Display temperatures for all cores [C] or average per CPU [a] (some newer AMD variants support per die)? (C/a)")
|
while read -r sensor; do
|
||||||
case "$choice" in
|
if [ -n "$sensor" ]; then
|
||||||
[cC]|"")
|
cpuCount=$((cpuCount + 1))
|
||||||
CPU_TEMP_TARGET="Core"
|
if [ -z "$cpuList" ]; then
|
||||||
info "Temperatures will be displayed for all cores."
|
cpuList="$sensor"
|
||||||
break
|
else
|
||||||
;;
|
cpuList="$cpuList,$sensor"
|
||||||
[aA])
|
fi
|
||||||
CPU_TEMP_TARGET="Package"
|
ENABLE_CPU=true
|
||||||
info "An average temperature will be displayed per CPU."
|
fi
|
||||||
break
|
done <<< "$found_sensors"
|
||||||
;;
|
fi
|
||||||
*)
|
done
|
||||||
warn "Invalid input, please choose C or a."
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
else
|
|
||||||
warn "No CPU temperature sensors found."
|
|
||||||
fi
|
|
||||||
|
|
||||||
#### RAM ####
|
if [ "$ENABLE_CPU" = true ]; then
|
||||||
msgb "\n=== Detecting RAM temperature sensors ==="
|
info "Detected CPU sensors ($cpuCount): $cpuList"
|
||||||
local ramList=($(echo "$sensorsOutput" | grep -o '"SODIMM[^"]*"' | sed 's/"//g'))
|
SENSORS_DETECTED=true
|
||||||
if [ ${#ramList[@]} -gt 0 ]; then
|
while true; do
|
||||||
info "Detected RAM sensors (${#ramList[@]}): $(IFS=,; echo "${ramList[*]}")"
|
local choice=$(ask "Display temperatures for all cores [C] or average per CPU [a] (some newer AMD variants support per die)? (C/a)")
|
||||||
ENABLE_RAM_TEMP=true
|
case "$choice" in
|
||||||
SENSORS_DETECTED=true
|
[cC]|"")
|
||||||
else
|
CPU_TEMP_TARGET="Core"
|
||||||
warn "No RAM temperature sensors found."
|
info "Temperatures will be displayed for all cores."
|
||||||
ENABLE_RAM_TEMP=false
|
break
|
||||||
fi
|
;;
|
||||||
|
[aA])
|
||||||
|
CPU_TEMP_TARGET="Package"
|
||||||
|
info "An average temperature will be displayed per CPU."
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
warn "Invalid input, please choose C or a."
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
else
|
||||||
|
warn "No CPU temperature sensors found."
|
||||||
|
fi
|
||||||
|
|
||||||
|
#### RAM ####
|
||||||
|
msgb "\n=== Detecting RAM temperature sensors ==="
|
||||||
|
local ramList=$(echo "$sensorsOutput" | grep -o '"SODIMM[^"]*"' | sed 's/"//g' | paste -sd, -)
|
||||||
|
local ramCount=$(echo "$ramList" | tr ',' '\n' | wc -l)
|
||||||
|
|
||||||
|
# Clean up if no RAM found
|
||||||
|
if [ -z "$ramList" ]; then
|
||||||
|
ramCount=0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$ramCount" -gt 0 ]; then
|
||||||
|
info "Detected RAM sensors ($ramCount): $ramList"
|
||||||
|
ENABLE_RAM_TEMP=true
|
||||||
|
SENSORS_DETECTED=true
|
||||||
|
else
|
||||||
|
warn "No RAM temperature sensors found."
|
||||||
|
ENABLE_RAM_TEMP=false
|
||||||
|
fi
|
||||||
|
|
||||||
#### HDD/SSD ####
|
#### HDD/SSD ####
|
||||||
msgb "\n=== Detecting HDD/SSD temperature sensors ==="
|
msgb "\n=== Detecting HDD/SSD temperature sensors ==="
|
||||||
@ -201,34 +229,41 @@ function configure {
|
|||||||
ENABLE_NVME_TEMP=false
|
ENABLE_NVME_TEMP=false
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#### Fans ####
|
#### Fans ####
|
||||||
msgb "\n=== Detecting fan speed sensors ==="
|
msgb "\n=== Detecting fan speed sensors ==="
|
||||||
local fanList=$(echo "$sensorsOutput" | grep -B 1 '"fan[0-9]*_input"' | grep -Po '"[^"]*":\s*\{$' | sed 's/"//g' | sed 's/: {//' | paste -sd ',' -)
|
|
||||||
local fanCount=$(echo "$sensorsOutput" | grep -c '"fan[0-9]*_input"')
|
|
||||||
if [ ${#fanList[@]} -gt 0 ]; then
|
|
||||||
info "Detected fan speed sensors ($fanCount): $fanList"
|
|
||||||
ENABLE_FAN_SPEED=true
|
|
||||||
SENSORS_DETECTED=true
|
|
||||||
|
|
||||||
local choice=$(ask "Display fans reporting zero speed? (Y/n)")
|
local fanList=""
|
||||||
case "$choice" in
|
local fanCount=0
|
||||||
[yY]|"")
|
|
||||||
DISPLAY_ZERO_SPEED_FANS=true
|
# Find all fan names that have fan*_input entries
|
||||||
info "Zero-speed fans will be displayed."
|
fanList=$(echo "$sensorsOutput" | grep -B2 '"fan[0-9]\+_input"' | grep '".*": {' | sed 's/.*"\([^"]*\)": {.*/\1/' | sort -u | paste -sd, -)
|
||||||
;;
|
fanCount=$(echo "$fanList" | tr ',' '\n' | wc -l)
|
||||||
[nN])
|
|
||||||
DISPLAY_ZERO_SPEED_FANS=false
|
if [ "$fanCount" -gt 0 ]; then
|
||||||
info "Only active fans will be displayed."
|
info "Detected fan speed sensors ($fanCount): $fanList"
|
||||||
;;
|
ENABLE_FAN_SPEED=true
|
||||||
*)
|
SENSORS_DETECTED=true
|
||||||
warn "Invalid input. Defaulting to show zero-speed fans."
|
|
||||||
DISPLAY_ZERO_SPEED_FANS=true
|
local choice
|
||||||
;;
|
choice=$(ask "Display fans reporting zero speed? (Y/n)")
|
||||||
esac
|
case "$choice" in
|
||||||
else
|
[yY]|"")
|
||||||
warn "No fan speed sensors found."
|
DISPLAY_ZERO_SPEED_FANS=true
|
||||||
ENABLE_FAN_SPEED=false
|
info "Zero-speed fans will be displayed."
|
||||||
fi
|
;;
|
||||||
|
[nN])
|
||||||
|
DISPLAY_ZERO_SPEED_FANS=false
|
||||||
|
info "Only active fans will be displayed."
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
warn "Invalid input. Defaulting to show zero-speed fans."
|
||||||
|
DISPLAY_ZERO_SPEED_FANS=true
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
else
|
||||||
|
warn "No fan speed sensors found."
|
||||||
|
ENABLE_FAN_SPEED=false
|
||||||
|
fi
|
||||||
|
|
||||||
#### Temperature Units ####
|
#### Temperature Units ####
|
||||||
if [ "$SENSORS_DETECTED" = true ]; then
|
if [ "$SENSORS_DETECTED" = true ]; then
|
||||||
@ -377,6 +412,29 @@ function install_mod {
|
|||||||
ask "Clear the browser cache to ensure all changes are visualized. (any key to continue)"
|
ask "Clear the browser cache to ensure all changes are visualized. (any key to continue)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Sanitize sensors output to handle common lm-sensors parsing issues
|
||||||
|
sanitize_sensors_output() {
|
||||||
|
local input="$1"
|
||||||
|
|
||||||
|
echo "$input" | perl -pe '
|
||||||
|
# Replace ERROR lines with placeholder values
|
||||||
|
s/ERROR:.+\s(\w+):\s(.+)/"$1": 0.000,/g;
|
||||||
|
s/ERROR:.+\s(\w+)!/"$1": 0.000,/g;
|
||||||
|
|
||||||
|
# Remove trailing commas before closing braces
|
||||||
|
s/,\s*(\})/$1/g;
|
||||||
|
|
||||||
|
# Replace NaN values with null
|
||||||
|
s/\bNaN\b/null/g;
|
||||||
|
|
||||||
|
# Fix duplicate SODIMM keys
|
||||||
|
s/"SODIMM":\{"temp(\d+)_input"/"SODIMM $1":\{"temp$1_input"/g;
|
||||||
|
|
||||||
|
# Fix duplicate fan keys
|
||||||
|
s/"([^"]+)":\{"fan(\d+)_input"/"$1 $2":\{"fan$2_input"/g;
|
||||||
|
' | python3 -m json.tool 2>/dev/null || echo "$input"
|
||||||
|
}
|
||||||
|
|
||||||
#region node info insertion
|
#region node info insertion
|
||||||
# Main insertion routine
|
# Main insertion routine
|
||||||
insert_node_info() {
|
insert_node_info() {
|
||||||
@ -402,8 +460,10 @@ collect_sensors_output() {
|
|||||||
sensorsCmd="cat \"$DEBUG_JSON_FILE\""
|
sensorsCmd="cat \"$DEBUG_JSON_FILE\""
|
||||||
else
|
else
|
||||||
# Note: sensors -f (Fahrenheit) breaks fan speeds
|
# Note: sensors -f (Fahrenheit) breaks fan speeds
|
||||||
sensorsCmd="sensors -j 2>/dev/null | python3 -m json.tool"
|
sensorsCmd="sensors -j 2>/dev/null"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Remember to reflect this in sanitize_sensors_output()
|
||||||
#region sensors heredoc
|
#region sensors heredoc
|
||||||
sed -i '/my \$dinfo = df('\''\/'\'', 1);/i\
|
sed -i '/my \$dinfo = df('\''\/'\'', 1);/i\
|
||||||
# Collect sensor data from lm-sensors\
|
# Collect sensor data from lm-sensors\
|
||||||
@ -420,14 +480,18 @@ collect_sensors_output() {
|
|||||||
# Replace NaN values with null for valid JSON\
|
# Replace NaN values with null for valid JSON\
|
||||||
$res->{sensorsOutput} =~ s/\\bNaN\\b/null/g;\
|
$res->{sensorsOutput} =~ s/\\bNaN\\b/null/g;\
|
||||||
\
|
\
|
||||||
# Fix duplicate SODIMM keys by appending temperature sensor number\
|
# Fix duplicate SODIMM keys by appending temperature sensor number\
|
||||||
# This prevents JSON key overwrites when multiple SODIMM sensors exist\
|
# Example: "SODIMM":{"temp3_input":34.0} becomes "SODIMM 3":{"temp3_input":34.0}\
|
||||||
# Example: "SODIMM":{"temp3_input":34.0} becomes "SODIMM3":{"temp3_input":34.0}\
|
$res->{sensorsOutput} =~ s/\\"SODIMM\":\\{\\"temp(\\d+)_input\\"/\\"SODIMM $1\\":\{\\"temp$1_input\\"/g;\
|
||||||
$res->{sensorsOutput} =~ s/\\"SODIMM\\":\\{\\"temp(\\d+)_input\\"/\\"SODIMM$1\\":\\{\\"temp$1_input\\"/g;\
|
|
||||||
\
|
\
|
||||||
# Fix duplicate fans keys by appending fan number\
|
# Fix duplicate fans keys by appending fan number with a space\
|
||||||
# Example: "Processor Fan":{"fan2_input":1000,...} → "Processor Fan 2":{"fan2_input":1000,...}\
|
# Example: "Processor Fan":{"fan2_input":1000,...} → "Processor Fan 2":{"fan2_input":1000,...}\
|
||||||
$res->{sensorsOutput} =~ s/\\"([^"]+)\\":\\{\\"fan(\\d+)_input\\"/\\"$1 $2\\":\{\\"fan$2_input\\"/g;\
|
$res->{sensorsOutput} =~ s/\\"([^"]+)\\":\\{\\"fan(\\d+)_input\\"/\\"$1 $2\\":\{\\"fan$2_input\\"/g;\
|
||||||
|
|
||||||
|
# Format JSON output properly (workaround for lm-sensors >3.6.0 issues)\
|
||||||
|
$res->{sensorsOutput} =~ /^(.*)$/s;\
|
||||||
|
$res->{sensorsOutput} = `echo \\Q$1\\E | python3 -m json.tool 2>/dev/null || echo \\Q$1\E`;\
|
||||||
|
|
||||||
' "$NODES_PM_FILE"
|
' "$NODES_PM_FILE"
|
||||||
#endregion sensors heredoc
|
#endregion sensors heredoc
|
||||||
info "Sensors' retriever added to \"$output_file\"."
|
info "Sensors' retriever added to \"$output_file\"."
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user