From 1b136c31d77f84ac3dfac8d7b6d06a93720db480 Mon Sep 17 00:00:00 2001 From: Awin Huang Date: Mon, 25 Mar 2024 14:38:01 +0000 Subject: [PATCH] Add cpufantemp.py, hddtemp.py, sysFanTemp.py --- cpufantemp.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ ddns.sh | 2 +- hddtemp.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ sysFanTemp.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100755 cpufantemp.py create mode 100755 hddtemp.py create mode 100755 sysFanTemp.py diff --git a/cpufantemp.py b/cpufantemp.py new file mode 100755 index 0000000..c63765c --- /dev/null +++ b/cpufantemp.py @@ -0,0 +1,55 @@ +#!/usr/bin/python3 + +import subprocess +import json + + +def runCmd(cmdString): + result = subprocess.run(cmdString, stdout=subprocess.PIPE, shell=True).stdout.decode('utf-8') + return result + + +def getCpuTempAndFanSpeed(): + 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() diff --git a/ddns.sh b/ddns.sh index d4bc97b..3f7ff16 100755 --- a/ddns.sh +++ b/ddns.sh @@ -24,7 +24,7 @@ INTERNET_IP=`curl -s http://ipv4.icanhazip.com` DNS_RECORD_IP=`dig +short "${URLS[0]}" | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | awk 'NR==1{print}'` NOW_TIME=`date` -if [ "$INTERNET_IP" != "$DNS_RECORD_IP" ] +if [ "$INTERNET_IP" != "$DNS_RECORD_IP" ] || [ "$1" = "-f" ] then printf "[${NOW_TIME}]: Renew IP: ${DNS_RECORD_IP} to ${INTERNET_IP}\n" for ((i = 0; i < ${#URLS[@]}; i++)); do diff --git a/hddtemp.py b/hddtemp.py new file mode 100755 index 0000000..4a7b800 --- /dev/null +++ b/hddtemp.py @@ -0,0 +1,52 @@ +#!/usr/bin/python3 + +import subprocess +import json + + +def runCmd(cmdString): + result = subprocess.run(cmdString, stdout=subprocess.PIPE, shell=True).stdout.decode('utf-8') + return result + + +def findHddId(): + hddDict = {} + results = runCmd("blkid").split("\n") + for line in results: + if line == "": + continue + + hddInfoList = line.split(" ") + if len(hddInfoList) > 1 and hddInfoList[0].startswith("/dev/sd"): + hddName = hddInfoList[0].replace(":", "") + hddDict[hddName] = {} + for hddInfoline in hddInfoList[1:]: + kvList = hddInfoline.split("=") + if len(kvList) > 1: + hddDict[hddName][kvList[0].replace("\"", "")] = kvList[1].replace("\"", "") + return hddDict + + +def findHddTemp(hddDict): + for hddName in sorted(hddDict.keys()): + cmd = f"smartctl -A {hddName} | grep Temperature_Celsius" + hddTempString = runCmd(cmd) + hddTempList = hddTempString.split(" ") + hddTempList = [item for item in hddTempList if item != ""] + if len(hddTempList) > 9: + hddDict[hddName]["temp"] = int(hddTempList[9]) + + +def getHddTemp(): + hddDict = findHddId() + findHddTemp(hddDict) + return hddDict + + +def main(): + hddDict = getHddTemp() + print(f"{json.dumps(hddDict, indent=4)}") + + +if __name__ == "__main__": + main() diff --git a/sysFanTemp.py b/sysFanTemp.py new file mode 100755 index 0000000..d680ef3 --- /dev/null +++ b/sysFanTemp.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 +import hddtemp +import cpufantemp +from rich import print +from rich.layout import Layout +from rich.panel import Panel + + +def main(): + hddtempDict = hddtemp.getHddTemp() + fanTempDict = cpufantemp.getCpuTempAndFanSpeed() + + layout = Layout() + layout.split_row( + Layout(name="left"), + Layout(name="center"), + Layout(name="right"), + ) + + ## Fan speed on left + fanSpeedString = "" + for fanKey in sorted(fanTempDict["fan"].keys()): + fanSpeed = fanTempDict["fan"][fanKey] + fanSpeedString += f"{fanKey}: {fanSpeed:<4} RPM\n" + p = Panel(fanSpeedString, title="Fan Speed") + p.height = 15 + layout["left"].update(p) + + ## CPU temp on center + cpuTempString = "" + for cpuKey in sorted(fanTempDict["cpu"].keys()): + cpuTemp = fanTempDict["cpu"][cpuKey] + cpuTempString += f"{cpuKey:<8}: {cpuTemp}°C\n" + p = Panel(cpuTempString, title="CPU Temperature") + p.height = 15 + layout["center"].update(p) + + ## HDD temp on right + hddTempString = "" + for hddname in sorted(hddtempDict.keys()): + temp = hddtempDict[hddname]["temp"] + hddTempString += f"{hddname}: {temp}°C\n" + p = Panel(hddTempString, title="HDD Temperature") + p.height = 15 + layout["right"].update(p) + print(layout) + + +if __name__ == "__main__": + main()