Use powershell for system-info CPU temp on WSL

Replace the query of CPU temp from the OpenHardwareMonitor's JSON served
via the network on the Windows host with a call to powershell to instead
read the WMI objects that OpenHardwareMonitor also emits. This is more
robust since powershell.exe is always available and does not require
Windows Defender firewall rules to allow connections from WSL2's VM to
the Windows host.
This commit is contained in:
Kenneth Benzie 2022-10-01 13:21:12 +01:00
parent d4c2af545d
commit fa263fb95e

View File

@ -8,37 +8,25 @@ if [ ! -d $cache_dir ]; then
mkdir -p $cache_dir mkdir -p $cache_dir
fi fi
while true; do if cat /proc/cpuinfo | grep -i intel > /dev/null; then
# Assumes OpenHardwareMonitor is has the Remote Web Server enabled on port cpu_temp_sensor="/intelcpu/0/temperature/0"
# 8085 of the Windows host. In WSL2 the Windows host IP is accessible using elif cat /proc/cpuinfo | grep -i amd > /dev/null; then
# $(homename).local, use this to grab sensor data but this depends on WSL2 cpu_temp_sensor="/amdcpu/0/temperature/0"
# being allowed to make connections to the Windows host: else
# return 1
# ```powershell fi
# New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow
# ```
#
# However this setting must be applied each boot, there's probably a sticky
# setting but not sure what it is.
cpu_json=`curl --silent $(hostname).local:8085/data.json | jq \
".Children[] | select(.Text | test(\"$(hostname | awk '{print toupper($0)}')\"))"\
'.Children[] | select(.ImageURL | test("images_icon/cpu.png"))'`
# cpu_load=`echo $cpu_json | jq --raw-output \ while true; do
# '.Children[] | select(.Text | test("Load"))'\ # Assumes OpenHardwareMonitor is running and emitting data to WMI so we can
# '.Children[] | select(.Text | test("CPU Core #[0-9]+")).Value'` # access it via the Windows hosts powershell.exe.
# echo cpu load: $cpu_load raw_cpu_temp=$(powershell.exe -NoProfile "(Get-WmiObject -Namespace \"root/OpenHardwareMonitor\" -Query 'select Value from Sensor WHERE Identifier LIKE \"$cpu_temp_sensor\"').Value" | sed 's/\r//')
cpu_temp=$(printf "%.1f°C" "$raw_cpu_temp")
cpu_load=`mpstat -P ALL -n 1 -u 1 -o JSON | \ cpu_load=`mpstat -P ALL -n 1 -u 1 -o JSON | \
jq '.sysstat.hosts[0].statistics[0]["cpu-load"][1:]|.[].idle' | \ jq '.sysstat.hosts[0].statistics[0]["cpu-load"][1:]|.[].idle' | \
awk '$idle ~ /[-.0-9]*/ { printf "%s", substr("█▇▆▅▄▃▂▁ ", int($idle / 11), 1) }'` awk '$idle ~ /[-.0-9]*/ { printf "%s", substr("█▇▆▅▄▃▂▁ ", int($idle / 11), 1) }'`
cpu_temp=`echo $cpu_json | jq --raw-output \
'.Children[] | select(.Text | test("Temperatures"))'\
'.Children[] | select(.Text | test("CPU Package")).Value' | \
awk '{gsub(/ /,""); print}'`
echo "$cpu_temp $cpu_load" > $cache_file echo "$cpu_temp $cpu_load" > $cache_file
# echo -e "HTTP/1.1 200 OK\n\n$cpu_temp $cpu_load" | nc -l -k -p 8080 -q 1; # echo -e "HTTP/1.1 200 OK\n\n$cpu_temp $cpu_load" | nc -l -k -p 8080 -q 1;
sleep 1 sleep 2
done done