码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-05-10 17:50:05      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:监控系统


#!/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()

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

标签:监控系统

原文地址:http://yanruohan.blog.51cto.com/9740053/1924091

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!