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

77
awinlib/utilities.py Normal file
View File

@@ -0,0 +1,77 @@
import asyncio
import datetime
import inspect
import json
import logging
import subprocess
from rich import print as pp
def runCmd(cmdString):
result = subprocess.run(cmdString, stdout=subprocess.PIPE, shell=True).stdout.decode('utf-8')
return result
def syncRun(coroutine):
loop = asyncio.get_event_loop()
# coroutine = funcInstace()
return loop.run_until_complete(coroutine)
def datetimeNow():
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def loadJson(fileanme):
try:
with open(fileanme, "r") as f:
secrets = json.load(f)
return secrets
except FileNotFoundError:
log(f"File not found: {fileanme}")
return None
def log2(message, *args):
frame = inspect.currentframe().f_back
class_name = frame.f_locals.get('self', '').__class__.__name__
func_name = frame.f_code.co_name
line_no = frame.f_lineno
caller = f"{class_name}::{func_name},{line_no}"
prefix = f"[{datetimeNow()}|{caller}]"
pp(f"{prefix} {message}", end='')
for arg in args:
pp(arg, end='')
pp("")
def log(message, *args):
log = logging.getLogger()
log.info(message)
def logD(message, *args):
log = logging.getLogger()
log.debug(message)
def logI(message, *args):
log = logging.getLogger()
log.info(message)
def logW(message, *args):
log = logging.getLogger()
log.warning(message)
def logE(message, *args):
log = logging.getLogger()
log.error(message)
def logC(message, *args):
log = logging.getLogger()
log.critical(message)