import psutil
import platform
from datetime import datetime
from csengine.utility import DebugUtility
[docs]
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
[docs]
def log_sysinfo():
DebugUtility.Log("="*40 + "System Information" + "="*40)
uname = platform.uname()
DebugUtility.Log(f"System: {uname.system}")
DebugUtility.Log(f"Node Name: {uname.node}")
DebugUtility.Log(f"Release: {uname.release}")
DebugUtility.Log(f"Version: {uname.version}")
DebugUtility.Log(f"Machine: {uname.machine}")
DebugUtility.Log(f"Processor: {uname.processor}")
# Boot Time
DebugUtility.Log("="*40 + "Boot Time" + "="*40)
boot_time_timestamp = psutil.boot_time()
bt = datetime.fromtimestamp(boot_time_timestamp)
DebugUtility.Log(f"Boot Time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}")
# let's DebugUtility.Log CPU information
DebugUtility.Log("="*40 + "CPU Info" + "="*40)
# number of cores
DebugUtility.Log("Physical cores: " + str(psutil.cpu_count(logical=False)))
DebugUtility.Log("Total cores: " + str(psutil.cpu_count(logical=True)))
# CPU frequencies
try:
cpufreq = psutil.cpu_freq()
DebugUtility.Log(f"Max Frequency: {cpufreq.max:.2f}Mhz")
DebugUtility.Log(f"Min Frequency: {cpufreq.min:.2f}Mhz")
except:
DebugUtility.Log("Unable to retreive cpu_frequency from psutil")
# Memory Information
DebugUtility.Log("="*40 + "Memory Information" + "="*40)
# get the memory details
svmem = psutil.virtual_memory()
DebugUtility.Log(f"Total: {get_size(svmem.total)}")
DebugUtility.Log(f"Available: {get_size(svmem.available)}")
DebugUtility.Log(f"Used: {get_size(svmem.used)}")
DebugUtility.Log(f"Percentage: {svmem.percent}%")
DebugUtility.Log("="*20 + "SWAP" + "="*20)
# get the swap memory details (if exists)
swap = psutil.swap_memory()
DebugUtility.Log(f"Total: {get_size(swap.total)}")
DebugUtility.Log(f"Free: {get_size(swap.free)}")
DebugUtility.Log(f"Used: {get_size(swap.used)}")
DebugUtility.Log(f"Percentage: {swap.percent}%")
# Disk Information
DebugUtility.Log("="*40 + "Disk Information" + "="*40)
DebugUtility.Log("Partitions and Usage:")
# get all disk partitions
partitions = psutil.disk_partitions()
for partition in partitions:
DebugUtility.Log(f"=== Device: {partition.device} ===")
DebugUtility.Log(f" Mountpoint: {partition.mountpoint}")
DebugUtility.Log(f" File system type: {partition.fstype}")
try:
partition_usage = psutil.disk_usage(partition.mountpoint)
except PermissionError:
# this can be catched due to the disk that
# isn't ready
continue
DebugUtility.Log(f" Total Size: {get_size(partition_usage.total)}")
DebugUtility.Log(f" Used: {get_size(partition_usage.used)}")
DebugUtility.Log(f" Free: {get_size(partition_usage.free)}")
DebugUtility.Log(f" Percentage: {partition_usage.percent}%")
# get IO statistics since boot
disk_io = psutil.disk_io_counters()
DebugUtility.Log(f"Total read: {get_size(disk_io.read_bytes)}")
DebugUtility.Log(f"Total write: {get_size(disk_io.write_bytes)}")