监控系统性能的脚本(cpu,disk,memory,network)

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()










本文转自 妙曼  51CTO博客,原文链接:http://blog.51cto.com/yanruohan/1924091,如需转载请自行联系原作者
上一篇:Java基础 day03-2


下一篇:面试官刁难:Java字符串可以引用传递吗?(1)