- add system info log

This commit is contained in:
peterlu14
2023-02-14 11:38:45 +08:00
parent e6c96e0f37
commit ffe7a95036
2 changed files with 48 additions and 0 deletions
+16
View File
@@ -1,6 +1,22 @@
import asyncio
import psycopg2
from mqtt_client import MqttClient
from utils.system_info import cpu_info, ram_info
import threading
def set_interval(func, sec):
def func_wrapper():
set_interval(func, sec)
func()
t = threading.Timer(sec, func_wrapper)
t.start()
return t
def call():
cpu_info()
ram_info()
set_interval(call, 1)
async def main():
loop = asyncio.get_event_loop()
+32
View File
@@ -0,0 +1,32 @@
import psutil
def get_size(bytes, suffix="B"):
"""
Scale bytes to its proper format
e.g:
1253656 => '1.20MB'
1253656678 => '1.17GB'
"""
factor = 1024
for unit in ["", "K", "M", "G", "T", "P"]:
if bytes < factor:
return f"{bytes:.2f}{unit}{suffix}"
bytes /= factor
def cpu_info():
# let's print CPU information
print("="*40, "CPU Info", "="*40)
print("CPU Usage Per Core:")
for i, percentage in enumerate(psutil.cpu_percent(percpu=True, interval=1)):
print(f"Core {i}: {percentage}%")
print(f"Total CPU Usage: {psutil.cpu_percent()}%")
def ram_info():
# Memory Information
print("="*40, "Memory Information", "="*40)
# get the memory details
svmem = psutil.virtual_memory()
print(f"Total: {get_size(svmem.total)}")
print(f"Available: {get_size(svmem.available)}")
print(f"Used: {get_size(svmem.used)}")
print(f"Percentage: {svmem.percent}%")