圆月山庄资源网 Design By www.vgjia.com
本文以实例形式实现了python监控linux性能以及进程消耗性能的方法,具体实现代码如下:
# -*- coding: utf-8 -*- """ Created on Tue Jun 10 10:20:13 2014 @author: lifeix """ from collections import OrderedDict import time import os def cpuinfo(): lines = open('/proc/stat').readlines() for line in lines: ln = line.split() if ln[0].startswith('cpu'): return ln; return [] W = cpuinfo() one_cpuTotal=long(W[1])+long(W[2])+long(W[3])+long(W[4])+long(W[5])+long(W[6])+long(W[7]) one_cpuused=long(W[1])+long(W[2])+long(W[3]) def CPUinfo(): ''' Return the information in /proc/CPUinfo as a dictionary in the following format: CPU_info['proc0']={...} CPU_info['proc1']={...} ''' CPUinfo=OrderedDict() procinfo=OrderedDict() nprocs = 0 f = open('/proc/cpuinfo') for line in f.readlines(): if not line.strip(): # end of one processor CPUinfo['proc%s' % nprocs] = procinfo nprocs=nprocs+1 # Reset procinfo=OrderedDict() else: if len(line.split(':')) == 2: procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip() else: procinfo[line.split(':')[0].strip()] = '' return CPUinfo def meminfo(): ''' Return the information in /proc/meminfo as a dictionary ''' meminfo=OrderedDict() f = open('/proc/meminfo') for line in f.readlines(): meminfo[line.split(':')[0]] = line.split(':')[1].strip() return meminfo f = open("sysinfo.log",'a') def logSysInfo(cpu,mem,line): f.write('\ncpu:%s -------mem: %s------mongocpu:%s'%(cpu,mem,line)) f.flush(); def process_info(): #获取drm_processes 的进程号 textlist = os.popen('top -bcn 1 -p 12023').readlines() line = '' for t in textlist: if t.find('12023'): line = t line = line.split(' ') #此处的值按照自己的需求去取 return line[15] if __name__=='__main__': CPUinfo = CPUinfo() for processor in CPUinfo.keys(): print(CPUinfo[processor]['model name']) f.write("cpu:%s"%CPUinfo[processor]['model name']) #meminfo = meminfo() #print('Total memory: {0}'.format(meminfo['MemTotal'])) try: while True: line = process_info() time.sleep(2) mi = meminfo() print('Free memory: {0}'.format(mi['MemFree'])) W = cpuinfo() two_cpuTotal=long(W[1])+long(W[2])+long(W[3])+long(W[4])+long(W[5])+long(W[6])+long(W[7]) two_cpuused=long(W[1])+long(W[2])+long(W[3]) cpuused=float(two_cpuused-one_cpuused)/(two_cpuTotal-one_cpuTotal) print ('%.2f%%'%(cpuused*100)) print line cpu = '%.2f%%'%(cpuused*100) logSysInfo(cpu,format(mi['MemFree']),line) except KeyboardInterrupt, e: print ("\ncpumonit exited") f.close() f.close()
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]