#!/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 ()
Script to monitor system performance (Cpu,disk,memory,network)