From 76e28b9d7c4e423423ddaba373c82b34ea0f43b9 Mon Sep 17 00:00:00 2001 From: Meliox <5264368+Meliox@users.noreply.github.com> Date: Sat, 15 Jun 2024 17:01:42 +0200 Subject: [PATCH 1/3] Add system information #2 (#47) --- pve-mod-gui-sensors.sh | 63 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/pve-mod-gui-sensors.sh b/pve-mod-gui-sensors.sh index 640071d..856c8c0 100644 --- a/pve-mod-gui-sensors.sh +++ b/pve-mod-gui-sensors.sh @@ -198,6 +198,22 @@ function configure { ;; esac fi + echo "" + read -p "Do you wish to enable System Information. [Yn]: " ENABLE_SYS_INFO + case "$ENABLE_SYS_INFO" in + [yY]|"") + enableSystemInfo=true + msg "Displaying System Information... yes" + ;; + [nN]) + enableSystemInfo=false + msg "Displaying System Information... no" + ;; + *) + warn "Invalid selection. System information will be displayed." + enableSystemInfo=true + ;; + esac echo # add a new line } @@ -213,27 +229,37 @@ function install_mod { local timestamp=$(date '+%Y-%m-%d_%H-%M-%S') - # Add new line to Nodes.pm file - if [[ -z $(cat $nodespm | grep -e "$res->{sensorsOutput}") ]]; then + # Perform backup + if [[ -z $(cat $nodespm | grep -e "$res->{sensorsOutput}") ]] || [[ -z $(cat $nodespm | grep -e "$res->{systemInfo}") ]]; then # Create backup of original file cp "$nodespm" "$BACKUP_DIR/Nodes.pm.$timestamp" msg "Backup of \"$nodespm\" saved to \"$BACKUP_DIR/Nodes.pm.$timestamp\"." + # Create backup of original file + cp "$pvemanagerlibjs" "$BACKUP_DIR/pvemanagerlib.js.$timestamp" + msg "Backup of \"$pvemanagerlibjs\" saved to \"$BACKUP_DIR/pvemanagerlib.js.$timestamp\"." + else + err "Mod is already installed. Uninstall existing before installing." + exit + fi + + enableSensors=true + if [[ "$enableSensors" == true ]]; then # WTF: sensors -f used for Fahrenheit breaks the fan speeds :| #local sensorsCmd=$([[ "$TEMP_UNIT" = "F" ]] && echo "sensors -j -f" || echo "sensors -j") local sensorsCmd="sensors -j" 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' "$nodespm" msg "Sensors' output added to \"$nodespm\"." - else - warn "Sensors' output already integrated in in \"$nodespm\"." + fi + + if [[ "$enableSystemInfo" == true ]]; then + local systemInfoCmd=$(dmidecode -t 1 | 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" "$nodespm" + msg "System Information output added to \"$nodespm\"." fi # Add new item to the items array in PVE.node.StatusView if [[ -z $(cat "$pvemanagerlibjs" | grep -e "itemId: 'thermal[[:alnum:]]*'") ]]; then - # Create backup of original file - cp "$pvemanagerlibjs" "$BACKUP_DIR/pvemanagerlib.js.$timestamp" - msg "Backup of \"$pvemanagerlibjs\" saved to \"$BACKUP_DIR/pvemanagerlib.js.$timestamp\"." - 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'/,/\},/ { @@ -331,6 +357,27 @@ Ext.define('PVE.mod.TempHelper', {\n\ },\n\ });\n" "$pvemanagerlibjs" + if [[ $enableSystemInfo == "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\ + }, + }" "$pvemanagerlibjs" + fi + sed -i "/^Ext.define('PVE.node.StatusView',/ { :a; /items:/!{N;ba;} From d13d02ab36a199643eb9cdce2feccd40b42d01ac Mon Sep 17 00:00:00 2001 From: Meliox <5264368+Meliox@users.noreply.github.com> Date: Sun, 16 Jun 2024 00:02:54 +0200 Subject: [PATCH 2/3] save-sensors-data argument (#52) --- pve-mod-gui-sensors.sh | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/pve-mod-gui-sensors.sh b/pve-mod-gui-sensors.sh index 856c8c0..21d504f 100644 --- a/pve-mod-gui-sensors.sh +++ b/pve-mod-gui-sensors.sh @@ -26,6 +26,10 @@ BACKUP_DIR="$SCRIPT_CWD/backup" pvemanagerlibjs="/usr/share/pve-manager/js/pvemanagerlib.js" nodespm="/usr/share/perl5/PVE/API2/Nodes.pm" +# Debug location +DEBUG_SAVE_PATH="$SCRIPT_CWD" +DEBUG_SAVE_FILENAME="sensorsdata.json" + ############################################### # Helper functions @@ -54,7 +58,7 @@ function err { # Function to display usage information function usage { - msgb "\nUsage:\n$0 [install | uninstall]\n" + msgb "\nUsage:\n$0 [install | uninstall | save-sensors-data]\n" exit 1 } @@ -768,6 +772,35 @@ function restart_proxy { systemctl restart pveproxy } +function save_sensors_data { + # Check if DEBUG_SAVE_PATH exists and is writable + if [[ ! -d "$DEBUG_SAVE_PATH" || ! -w "$DEBUG_SAVE_PATH" ]]; then + err "Directory $DEBUG_SAVE_PATH does not exist or is not writable. No file could be saved" + return + fi + + # Check if command exists + if (command -v sensors &>/dev/null); then + # Save sensors output + local filepath="${DEBUG_SAVE_PATH}/${DEBUG_SAVE_FILENAME}" + echo "Sensors data will be saved in $filepath" + + # Prompt user for confirmation + read -p "Do you wish to continue? (y/n): " choice + case "$choice" in + y|Y ) + sensors -j > "$filepath" + msgb "Sensors data saved in $filepath" + ;; + * ) + echo "Operation cancelled by user." + ;; + esac + else + err "Sensors is not installed. No file could be saved" + fi +} + # Process the arguments using a while loop and a case statement executed=0 while [[ $# -gt 0 ]]; do @@ -785,6 +818,12 @@ while [[ $# -gt 0 ]]; do uninstall_mod echo # add a new line ;; + save-sensors-data) + executed=$(($executed + 1)) + msgb "\nSaving current sensor readings in a file for debugging..." + save_sensors_data + echo # add a new line + ;; esac shift done From 3490d3a07e63359027c8c261af31f09e8d7cb162 Mon Sep 17 00:00:00 2001 From: Meliox <5264368+Meliox@users.noreply.github.com> Date: Sun, 16 Jun 2024 11:42:31 +0200 Subject: [PATCH 3/3] Add "remote" debug option of json file (#50) --- pve-mod-gui-sensors.sh | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/pve-mod-gui-sensors.sh b/pve-mod-gui-sensors.sh index 21d504f..ba01218 100644 --- a/pve-mod-gui-sensors.sh +++ b/pve-mod-gui-sensors.sh @@ -30,7 +30,11 @@ nodespm="/usr/share/perl5/PVE/API2/Nodes.pm" DEBUG_SAVE_PATH="$SCRIPT_CWD" DEBUG_SAVE_FILENAME="sensorsdata.json" -############################################### +##################### 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. +DEBUG_REMOTE=false +JSON_FILE="/tmp/sensordata.json" # Helper functions function msg { @@ -89,7 +93,15 @@ function install_packages { function configure { sensorsDetected=false - local sensorsOutput=$(sensors -j) + local sensorsOutput + + if [ $DEBUG_REMOTE = true ]; then + warn "Remote debugging is used. Sensor readings from dump file $JSON_FILE will be used." + sensorsOutput=$(cat $JSON_FILE) + else + sensorsOutput=$(sensors -j) + fi + if [ $? -ne 0 ]; then err "Sensor output error.\n\nCommand output:\n${sensorsOutput}\n\nExiting...\n" fi @@ -249,9 +261,14 @@ function install_mod { enableSensors=true if [[ "$enableSensors" == true ]]; then - # WTF: sensors -f used for Fahrenheit breaks the fan speeds :| - #local sensorsCmd=$([[ "$TEMP_UNIT" = "F" ]] && echo "sensors -j -f" || echo "sensors -j") - local sensorsCmd="sensors -j" + local sensorsCmd + if [ $DEBUG_REMOTE = true ]; then + sensorsCmd="cat \"$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" + 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' "$nodespm" msg "Sensors' output added to \"$nodespm\"." fi