class CPU:
@classmethod
def _get_cpu_load(cls):
# get cpu 5 minutes load
loadavg = "/proc/loadavg"
with open(loadavg) as f:
con = f.read().split()
return con[1]
@classmethod
def _get_cpu_usage(cls):
# Return a float representing the current system-wide CPU utilization as a percentage
return psutil.cpu_percent(interval=0.1)
@classmethod
def get_cpu_info(cls):
cpu = {
"cpuUtil": cls._get_cpu_load(),
"cpuUsed": cls._get_cpu_usage()
}
return cpu
class Memory:
mem = psutil.virtual_memory()
@classmethod
def _get_mem_total(cls):
return cls.mem.total
@classmethod
def _get_mem_used(cls):
return cls.mem.used
@classmethod
def get_mem_info(cls):
mem = {
"memUsed": cls._get_mem_used(),
"memTotal": cls._get_mem_total(),
}
return mem
class NetworkCard:
@classmethod
def get_card_bytes_info(cls):
# bytes_sent
try:
card_ip = []
card_io = []
interface = []
for k, v in psutil.net_if_addrs().items():
if k not in 'lo':
card_ip.append({'name': k, 'ip':v[0].address})
for k, v in psutil.net_io_counters(pernic=True).items():
if k not in 'lo':
card_io.append({'name': k, 'out': v.bytes_sent, 'in': v.bytes_recv})
for i in range(len(card_ip)):
card = {
'intName': card_io['name'],
'ip': card_ip['ip'],
'out': card_io['out'],
'in': card_io['in']
}
interface.append(card)
return interface
except AttributeError as e:
print("Please use psutil version 3.0 or above")