- add system info log
This commit is contained in:
+16
@@ -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()
|
||||
|
||||
@@ -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}%")
|
||||
Reference in New Issue
Block a user