#!/usr/bin/env bash
#
# This bash script installs a modification to the Proxmox Virtual Environment (PVE) web user interface (UI) to display sensors information.
#
################### Configuration #############
# Display configuration for HDD, NVME, CPU
# Set to 0 to disable line breaks
# Note: use these settings only if the displayed layout is broken
CPU_ITEMS_PER_ROW=0
NVME_ITEMS_PER_ROW=0
HDD_ITEMS_PER_ROW=0
# Known CPU sensor names. They can be full or partial but should ensure unambiguous identification.
# Should new ones be added, also update logic in configure() function.
KNOWN_CPU_SENSORS=("coretemp-isa-" "k10temp-pci-")
# Overwrite default backup location
BACKUP_DIR=""
##################### DO NOT EDIT BELOW #######################
# Only to be used to debug on other systems. Save the "sensor -j" output into a json file.
# Information will be loaded for script configuration and presented in Proxmox.
# DEV NOTE: lm-sensors version >3.6.0 breakes properly formatted JSON output using 'sensors -j'. This implements a workaround using uses a python3 for formatting
DEBUG_REMOTE=false
DEBUG_JSON_FILE="/tmp/sensordata.json"
# This script's working directory
SCRIPT_CWD="$(dirname "$(readlink -f "$0")")"
# Debug location
JSON_EXPORT_DIRECTORY="$SCRIPT_CWD"
JSON_EXPORT_FILENAME="sensorsdata.json"
# File paths
PVE_MANAGER_LIB_JS_FILE="/usr/share/pve-manager/js/pvemanagerlib.js"
NODES_PM_FILE="/usr/share/perl5/PVE/API2/Nodes.pm"
# Helper functions
function msg {
echo -e "\e[0m$1\e[0m"
}
#echo message in bold
function msgb {
echo -e "\e[1m$1\e[0m"
}
function info {
echo -e "\e[0;32m[info] $1\e[0m"
}
function warn {
echo -e "\e[0;93m[warning] $1\e[0m"
}
function err {
echo -e "\e[0;31m[error] $1\e[0m"
exit 1
}
function ask {
read -p $'\n\e[0;32m'"$1:"$'\e[0m'" " response
echo $response
}
# End of helper functions
# Function to display usage information
function usage {
msgb "\nUsage:\n$0 [install | uninstall | save-sensors-data]\n"
exit 1
}
# Define a function to install packages
function install_packages {
# Check if the 'sensors' command is available on the system
if (! command -v sensors &>/dev/null); then
# If the 'sensors' command is not available, prompt the user to install lm-sensors
local choiceInstallLmSensors=$(ask "lm-sensors is not installed. Would you like to install it? (y/n)")
case "$choiceInstallLmSensors" in
[yY])
# If the user chooses to install lm-sensors, update the package list and install the package
apt-get update
apt-get install lm-sensors
;;
[nN])
# 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..."
exit 0
;;
*)
# If the user enters an invalid input, print an error message and exit the script with a non-zero status code
err "Invalid input. Exiting..."
;;
esac
fi
}
function configure {
SENSORS_DETECTED=false
local sensorsOutput
if [ $DEBUG_REMOTE = true ]; then
warn "Remote debugging is used. Sensor readings from dump file $DEBUG_JSON_FILE will be used."
sensorsOutput=$(cat $DEBUG_JSON_FILE)
else
sensorsOutput=$(sensors -j 2>/dev/null | python3 -m json.tool)
fi
if [ $? -ne 0 ]; then
err "Sensor output error.\n\nCommand output:\n${sensorsOutput}\n\nExiting...\n"
fi
# Check if CPU is part of known list for autoconfiguration
msg "\nDetecting support for CPU temperature sensors..."
supportedCPU=false
for item in "${KNOWN_CPU_SENSORS[@]}"; do
if (echo "$sensorsOutput" | grep -q "$item"); then
echo $item
supportedCPU=true
fi
done
# Prompt user for which CPU temperature to use
if [ $supportedCPU = true ]; then
while true; do
local choiceTempDisplayType=$(ask "Do you wish to display temperatures for all cores [C] or just an average temperature per CPU [a] (note: AMD only supports average)? (C/a)")
case "$choiceTempDisplayType" in
# Set temperature search criteria
[cC] | "")
CPU_TEMP_TARGET="Core"
info "Temperatures will be displayed for all cores."
;;
[aA])
CPU_TEMP_TARGET="Package"
info "An average temperature will be displayed per CPU."
;;
*)
# If the user enters an invalid input, print an warning message and retry as>
warn "Invalid input."
continue
;;
esac
break
done
SENSORS_DETECTED=true
else
warn "No CPU temperature sensors found."
fi
# Look for ram temps
msg "\nDetecting support for RAM temperature sensors..."
if (echo "$sensorsOutput" | grep -q '"SODIMM":'); then
msg "Detected RAM temperature sensors:\n$(echo "$sensorsOutput" | grep -o '"SODIMM[^"]*"' | sed 's/"//g')"
ENABLE_RAM_TEMP=true
SENSORS_DETECTED=true
else
warn "No RAM temperature sensors found."
ENABLE_RAM_TEMP=false
fi
# Check if HDD/SSD data is installed
msg "\nDetecting support for HDD/SDD temperature sensors..."
if (lsmod | grep -wq "drivetemp"); then
# Check if SDD/HDD data is available
if (echo "$sensorsOutput" | grep -q "drivetemp-scsi-"); then
msg "Detected sensors:\n$(echo "$sensorsOutput" | grep -o '"drivetemp-scsi[^"]*"' | sed 's/"//g')"
ENABLE_HDD_TEMP=true
SENSORS_DETECTED=true
else
warn "Kernel module \"drivetemp\" is not installed. HDD/SDD temperatures will not be available."
ENABLE_HDD_TEMP=false
fi
else
warn "No HDD/SSD temperature sensors found."
ENABLE_HDD_TEMP=false
fi
# Check if NVMe data is available
msg "\nDetecting support for NVMe temperature sensors..."
if (echo "$sensorsOutput" | grep -q "nvme-"); then
msg "Detected sensors:\n$(echo "$sensorsOutput" | grep -o '"nvme[^"]*"' | sed 's/"//g')"
ENABLE_NVME_TEMP=true
SENSORS_DETECTED=true
else
warn "No NVMe temperature sensors found."
ENABLE_NVME_TEMP=false
fi
# Look for fan speeds
msg "\nDetecting support for fan speed readings..."
if (echo "$sensorsOutput" | grep -q "fan[0-9]*_input"); then
msg "Detected fan speed sensors:\n$(echo $sensorsOutput | grep -Po '"[^"]*":\s*\{\s*"fan[0-9]*_input[^}]*' | sed -E 's/"([^"]*)":.*/\1/')"
ENABLE_FAN_SPEED=true
SENSORS_DETECTED=true
# Prompt user for display zero speed fans
local choiceDisplayZeroSpeedFans=$(ask "Do you wish to display fans reporting a speed of zero? If no, only active fans will be displayed. (Y/n)")
case "$choiceDisplayZeroSpeedFans" in
# Set temperature search criteria
[yY]|"")
DISPLAY_ZERO_SPEED_FANS=true
;;
[nN] )
DISPLAY_ZERO_SPEED_FANS=false
;;
*)
# If the user enters an invalid input, print an error message and exit the script with a non-zero status code
err "Invalid input. Exiting..."
;;
esac
else
warn "No fan speed sensors found."
ENABLE_FAN_SPEED=false
fi
if [ $SENSORS_DETECTED = true ]; then
local choiceTempUnit=$(ask "Do you wish to display temperatures in degrees Celsius [C] or Fahrenheit [f]? (C/f)")
case "$choiceTempUnit" in
[cC] | "")
TEMP_UNIT="C"
info "Temperatures will be presented in degrees Celsius."
;;
[fF])
TEMP_UNIT="F"
info "Temperatures will be presented in degrees Fahrenheit."
;;
*)
warn "Invalid unit selected. Temperatures will be displayed in degrees Celsius."
TEMP_UNIT="C"
;;
esac
fi
# DMI Type:
# 1 ... System Information
# 2 ... Base Board Information (for self-made PC)
for i in 1 2; do
echo "type ${i})"
dmidecode -t ${i} | awk -F': ' '/Manufacturer|Product Name|Serial Number/ {print $1": "$2}'
done
local choiceEnableSystemInfo=$(ask "Do you wish to enable system information? (1/2/n)")
case "$choiceEnableSystemInfo" in
[1] | "")
ENABLE_SYSTEM_INFO=true
SYSTEM_INFO_TYPE=1
info "System information will be displayed..."
;;
[2])
ENABLE_SYSTEM_INFO=true
SYSTEM_INFO_TYPE=2
info "Motherboard information will be displayed..."
;;
[nN])
ENABLE_SYSTEM_INFO=false
info "System information will NOT be displayed..."
;;
*)
warn "Invalid selection. System information will be displayed."
ENABLE_SYSTEM_INFO=true
;;
esac
echo # add a new line
}
# Function to install the modification
function install_mod {
if [[ -n $(cat $NODES_PM_FILE | grep -e "$res->{sensorsOutput}") ]] && [[ -n $(cat $NODES_PM_FILE | grep -e "$res->{systemInfo}") ]]; then
err "Mod is already installed. Uninstall existing before installing."
exit
fi
msg "\nPreparing mod installation..."
# Provide sensor configuration
configure
perform_backup
if [ $SENSORS_DETECTED = true ]; then
local sensorsCmd
if [ $DEBUG_REMOTE = true ]; then
sensorsCmd="cat \"$DEBUG_JSON_FILE\""
else
# WTF: sensors -f used for Fahrenheit breaks the fan speeds :|
#local sensorsCmd=$([[ "$TEMP_UNIT" = "F" ]] && echo "sensors -j -f" || echo "sensors -j")
sensorsCmd="sensors -j 2>/dev/null | python3 -m json.tool"
fi
sed -i '/my \$dinfo = df('\''\/'\'', 1);/i\'$'\t''$res->{sensorsOutput} = `'"$sensorsCmd"'`;\n\t# sanitize JSON output\n\t$res->{sensorsOutput} =~ s/ERROR:.+\\s(\\w+):\\s(.+)/\\"$1\\": 0.000,/g;\n\t$res->{sensorsOutput} =~ s/ERROR:.+\\s(\\w+)!/\\"$1\\": 0.000,/g;\n\t$res->{sensorsOutput} =~ s/,(.*[.\\n]*.+})/$1/g;\n' "$NODES_PM_FILE"
msg "Sensors' output added to \"$NODES_PM_FILE\"."
fi
if [ $ENABLE_SYSTEM_INFO = true ]; then
local systemInfoCmd=$(dmidecode -t ${SYSTEM_INFO_TYPE} | awk -F': ' '/Manufacturer|Product Name|Serial Number/ {print $1": "$2}' | awk '{$1=$1};1' | sed 's/$/ |/' | paste -sd " " - | sed 's/ |$//')
sed -i "/my \$dinfo = df('\/', 1);/i\\\t\$res->{systemInfo} = \"$(echo "$systemInfoCmd")\";\n" "$NODES_PM_FILE"
msg "System information output added to \"$NODES_PM_FILE\"."
fi
# Add new item to the items array in PVE.node.StatusView
if [[ -z $(cat "$PVE_MANAGER_LIB_JS_FILE" | grep -e "itemId: 'thermal[[:alnum:]]*'") ]]; then
local tempHelperCtorParams=$([[ "$TEMP_UNIT" = "F" ]] && echo '{srcUnit: PVE.mod.TempHelper.CELSIUS, dstUnit: PVE.mod.TempHelper.FAHRENHEIT}' || echo '{srcUnit: PVE.mod.TempHelper.CELSIUS, dstUnit: PVE.mod.TempHelper.CELSIUS}')
# Expand space in StatusView
sed -i "/Ext.define('PVE\.node\.StatusView'/,/\},/ {
s/\(bodyPadding:\) '[^']*'/\1 '20 15 20 15'/
s/height: [0-9]\+/minHeight: 360,\n\tflex: 1,\n\tcollapsible: true,\n\ttitleCollapse: true/
s/\(tableAttrs:.*$\)/trAttrs: \{ valign: 'top' \},\n\t\1/
}" "$PVE_MANAGER_LIB_JS_FILE"
msg "Expanded space in \"$PVE_MANAGER_LIB_JS_FILE\"."
sed -i "/^Ext.define('PVE.node.StatusView'/i\
Ext.define('PVE.mod.TempHelper', {\n\
//singleton: true,\n\
\n\
requires: ['Ext.util.Format'],\n\
\n\
statics: {\n\
CELSIUS: 0,\n\
FAHRENHEIT: 1\n\
},\n\
\n\
srcUnit: null,\n\
dstUnit: null,\n\
\n\
isValidUnit: function (unit) {\n\
return (\n\
Ext.isNumber(unit) && (unit === this.self.CELSIUS || unit === this.self.FAHRENHEIT)\n\
);\n\
},\n\
\n\
constructor: function (config) {\n\
this.srcUnit = config && this.isValidUnit(config.srcUnit) ? config.srcUnit : this.self.CELSIUS;\n\
this.dstUnit = config && this.isValidUnit(config.dstUnit) ? config.dstUnit : this.self.CELSIUS;\n\
},\n\
\n\
toFahrenheit: function (tempCelsius) {\n\
return Ext.isNumber(tempCelsius)\n\
? tempCelsius * 9 / 5 + 32\n\
: NaN;\n\
},\n\
\n\
toCelsius: function (tempFahrenheit) {\n\
return Ext.isNumber(tempFahrenheit)\n\
? (tempFahrenheit - 32) * 5 / 9\n\
: NaN;\n\
},\n\
\n\
getTemp: function (value) {\n\
if (this.srcUnit !== this.dstUnit) {\n\
switch (this.srcUnit) {\n\
case this.self.CELSIUS:\n\
switch (this.dstUnit) {\n\
case this.self.FAHRENHEIT:\n\
return this.toFahrenheit(value);\n\
\n\
default:\n\
Ext.raise({\n\
msg:\n\
'Unsupported destination temperature unit: ' + this.dstUnit,\n\
});\n\
}\n\
case this.self.FAHRENHEIT:\n\
switch (this.dstUnit) {\n\
case this.self.CELSIUS:\n\
return this.toCelsius(value);\n\
\n\
default:\n\
Ext.raise({\n\
msg:\n\
'Unsupported destination temperature unit: ' + this.dstUnit,\n\
});\n\
}\n\
default:\n\
Ext.raise({\n\
msg: 'Unsupported source temperature unit: ' + this.srcUnit,\n\
});\n\
}\n\
} else {\n\
return value;\n\
}\n\
},\n\
\n\
getUnit: function(plainText) {\n\
switch (this.dstUnit) {\n\
case this.self.CELSIUS:\n\
return plainText !== true ? '\°C' : '\\\'C';\n\
\n\
case this.self.FAHRENHEIT:\n\\n\
return plainText !== true ? '\°F' : '\\\'F';\n\
\n\
default:\n\
Ext.raise({\n\
msg: 'Unsupported destination temperature unit: ' + this.srcUnit,\n\
});\n\
}\n\
},\n\
});\n" "$PVE_MANAGER_LIB_JS_FILE"
if [ $ENABLE_SYSTEM_INFO = true ]; then
sed -i "/^Ext.define('PVE.node.StatusView',/ {
:a;
/items:/!{N;ba;}
:b;
/cpus.*},/!{N;bb;}
a\
\\
{\n\
itemId: 'sysinfo',\n\
colspan: 2,\n\
printBar: false,\n\
title: gettext('System Information'),\n\
textField: 'systemInfo',\n\
renderer: function(value){\n\
return value;\n\
}\n\
},
}" "$PVE_MANAGER_LIB_JS_FILE"
fi
sed -i "/^Ext.define('PVE.node.StatusView',/ {
:a;
/items:/!{N;ba;}
:b;
/cpus.*},/!{N;bb;}
a\
\\
{\n\
itemId: 'thermalCpu',\n\
colspan: 2,\n\
printBar: false,\n\
title: gettext('CPU Thermal State'),\n\
iconCls: 'fa fa-fw fa-thermometer-half',\n\
textField: 'sensorsOutput',\n\
renderer: function(value){\n\
// sensors configuration\n\
const cpuTempHelper = Ext.create('PVE.mod.TempHelper', $tempHelperCtorParams);\n\
// display configuration\n\
const itemsPerRow = $CPU_ITEMS_PER_ROW;\n\
// ---\n\
let objValue;\n\
try {\n\
objValue = JSON.parse(value) || {};\n\
} catch(e) {\n\
objValue = {};\n\
}\n\
const cpuKeysI = Object.keys(objValue).filter(item => String(item).startsWith('coretemp-isa-')).sort();\n\
const cpuKeysA = Object.keys(objValue).filter(item => String(item).startsWith('k10temp-pci-')).sort();\n\
const bINTEL = cpuKeysI.length > 0 ? true : false;\n\
const INTELPackagePrefix = '$CPU_TEMP_TARGET' == 'Core' ? 'Core ' : 'Package id';\n\
const INTELPackageCaption = '$CPU_TEMP_TARGET' == 'Core' ? 'Core' : 'Package';\n\
let AMDPackagePrefix = 'Tccd';\n\
let AMDPackageCaption = 'Chiplet';\n\
if (cpuKeysA.length > 0) {\n\
let bTccd = false;\n\
let bTctl = false;\n\
let bTdie = false;\n\
cpuKeysA.forEach((cpuKey, cpuIndex) => {\n\
let items = objValue[cpuKey];\n\
bTccd = Object.keys(items).findIndex(item => { return String(item).startsWith('Tccd'); }) >= 0;\n\
bTctl = Object.keys(items).findIndex(item => { return String(item).startsWith('Tctl'); }) >= 0;\n\
bTdie = Object.keys(items).findIndex(item => { return String(item).startsWith('Tdie'); }) >= 0;\n\
});\n\
if (bTccd && bTctl && '$CPU_TEMP_TARGET' == 'Core') {\n\
AMDPackagePrefix = 'Tccd';\n\
AMDPackageCaption = 'Chiplet';\n\
} else if (bTdie) {\n\
AMDPackagePrefix = 'Tdie';\n\
AMDPackageCaption = 'Temp';\n\
} else if (bTctl) {\n\
AMDPackagePrefix = 'Tctl';\n\
AMDPackageCaption = 'Temp';\n\
} else {\n\
AMDPackagePrefix = 'temp';\n\
AMDPackageCaption = 'Temp';\n\
}\n\
}\n\
const cpuKeys = bINTEL ? cpuKeysI : cpuKeysA;\n\
const cpuItemPrefix = bINTEL ? INTELPackagePrefix : AMDPackagePrefix;\n\
const cpuTempCaption = bINTEL ? INTELPackageCaption : AMDPackageCaption;\n\
const formatTemp = bINTEL ? '0' : '0.0';\n\
const cpuCount = cpuKeys.length;\n\
let temps = [];\n\
cpuKeys.forEach((cpuKey, cpuIndex) => {\n\
let cpuTemps = [];\n\
const items = objValue[cpuKey];\n\
const itemKeys = Object.keys(items).filter(item => { return String(item).includes(cpuItemPrefix); });\n\
itemKeys.forEach((coreKey) => {\n\
try {\n\
let tempVal = NaN, tempMax = NaN, tempCrit = NaN;\n\
Object.keys(items[coreKey]).forEach((secondLevelKey) => {\n\
if (secondLevelKey.endsWith('_input')) {\n\
tempVal = cpuTempHelper.getTemp(parseFloat(items[coreKey][secondLevelKey]));\n\
} else if (secondLevelKey.endsWith('_max')) {\n\
tempMax = cpuTempHelper.getTemp(parseFloat(items[coreKey][secondLevelKey]));\n\
} else if (secondLevelKey.endsWith('_crit')) {\n\
tempCrit = cpuTempHelper.getTemp(parseFloat(items[coreKey][secondLevelKey]));\n\
}\n\
});\n\
if (!isNaN(tempVal)) {\n\
let tempStyle = '';\n\
if (!isNaN(tempMax) && tempVal >= tempMax) {\n\
tempStyle = 'color: #FFC300; font-weight: bold;';\n\
}\n\
if (!isNaN(tempCrit) && tempVal >= tempCrit) {\n\
tempStyle = 'color: red; font-weight: bold;';\n\
}\n\
let tempStr = '';\n\
let tempIndex = coreKey.match(\/(?:P\\\s+Core|E\\\s+Core|Core)\\\s*(\\\d+)\/);\n\
if (tempIndex !== null && tempIndex.length > 1) {\n\
tempIndex = tempIndex[1];\n\
let coreType = coreKey.startsWith('P Core') ? 'P Core' :\n\
coreKey.startsWith('E Core') ? 'E Core' :\n\
cpuTempCaption;\n\
tempStr = \`\${coreType} \${tempIndex}: \${Ext.util.Format.number(tempVal, formatTemp)}\${cpuTempHelper.getUnit()}\`;\n\
} else {\n\
// fallback for CPUs which do not have a core index\n\
let coreType = coreKey.startsWith('P Core') ? 'P Core' :\n\
coreKey.startsWith('E Core') ? 'E Core' :\n\
cpuTempCaption;\n\
tempStr = \`\${coreType}: \${Ext.util.Format.number(tempVal, formatTemp)}\${cpuTempHelper.getUnit()}\`;\n\
}\n\
cpuTemps.push(tempStr);\n\
}\n\
} catch (e) { /*_*/ }\n\
});\n\
if(cpuTemps.length > 0) {\n\
temps.push(cpuTemps);\n\
}\n\
});\n\
let result = '';\n\
temps.forEach((cpuTemps, cpuIndex) => {\n\
const strCoreTemps = cpuTemps.map((strTemp, index, arr) => { return strTemp + (index + 1 < arr.length ? (itemsPerRow > 0 && (index + 1) % itemsPerRow === 0 ? '
' : ' | ') : ''); })\n\
if(strCoreTemps.length > 0) {\n\
result += (cpuCount > 1 ? \`CPU \${cpuIndex+1}: \` : '') + strCoreTemps.join('') + (cpuIndex < cpuCount ? '
' : '');\n\
}\n\
});\n\
return '