1
|
<br> |
#!/usr/bin/python
import psutil
import json
import time
def item_disk():
return dict(zip(
['total','used','free','percent'],psutil.disk_usage('/')
))
def item_cpu():
return dict(zip(
['user','system','nice','idle'],psutil.cpu_times()
))
def item_mem():
return dict(zip(
['total','available','percent','used','free',
'active','inactive'],psutil.virtual_memory()
))
def item_net():
return dict(zip(
['bytes_sent','bytes_recv','packets_sent','packets_recv',
'errin','errout','dropin','dropout'],psutil.net_io_counters()
))
def runner():
while True:
yield{
'disk' : item_disk(),
'cpu' : item_cpu(),
'mem' : item_mem(),
'network' : item_net()
}
def format_(data):
return json.dumps(data, indent=4, ensure_ascii=False)
def running():
for items in runner():
print(format_(items))
time.sleep(5)
def main():
print(running())
if __name__ == '__main__':
main()