Implement Prometheus data exporters for CPU fan temperature and HDD temperature, along with utility functions for logging and command execution.

This commit is contained in:
2025-02-05 10:47:08 +08:00
parent d34588467b
commit 665a8ea05a
8 changed files with 546 additions and 0 deletions

54
awinlib/cpufantemp.py Normal file
View File

@@ -0,0 +1,54 @@
#!/usr/bin/python3
import json
import os
from .utilities import runCmd
def getCpuTempAndFanSpeed():
if os.name == "nt":
return {}
cmd = "sensors"
ret = runCmd(cmd).split("\n")
fanTempDict = {
"cpu": {},
"fan": {}
}
for line in ret:
if line.startswith("Core"):
line = line.split(":")
if len(line) > 1:
cpuIndex, tempString = line[0], line[1]
tempString = tempString.split(" ")
tempString = [x for x in tempString if x != ""]
if len(tempString) > 1:
temp = float(tempString[0].replace("+", "").replace("°C", ""))
else:
temp = -999.9
fanTempDict["cpu"][cpuIndex] = temp
elif line.startswith("fan"):
line = line.split(":")
if len(line) > 1:
fanId, fanNumber = line[0], line[1]
fanNumber = fanNumber.split(" ")
fanNumber = [x for x in fanNumber if x != ""]
if len(fanNumber) > 1:
fanSpeed = int(fanNumber[0])
else:
fanSpeed = 0
if fanSpeed > 0:
fanTempDict["fan"][fanId] = fanSpeed
return fanTempDict
def main():
fanTempDict = getCpuTempAndFanSpeed()
print(f"{json.dumps(fanTempDict, indent=4)}")
if __name__ == "__main__":
main()