时间:2021-05-22
本文以实例形式实现了python监控linux性能以及进程消耗性能的方法,具体实现代码如下:
# -*- coding: utf-8 -*-"""Created on Tue Jun 10 10:20:13 2014@author: lifeix"""from collections import OrderedDictimport timeimport osdef 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 CPUinfodef 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 meminfof = 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()声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Linux系统操作中,除了可以使用命令监控系统性能外(详解监控Linux系统性能的命令有哪些),还可使用KSysguard工具,KSysguard是系统性能
psutil是一个跨平台进程和系统监控的Python库,而gopsutil是其Go语言版本的实现。本文介绍了它的基本使用。Go语言部署简单、性能好的特点非常适合
本文实例讲述了PHP+swoole+linux实现系统监控和性能优化操作。分享给大家供大家参考,具体如下:服务器监控端口监控php运行shell脚本classS
本篇内容:Sentinel对性能的消耗如何Sentinel工作流程源码分析Sentinel熔断降级实现源码分析Sentinel对性能的消耗如何Sentinel统
Linux内核有个机制叫OOMkiller(Out-Of-Memorykiller),该机制会监控那些占用内存过大,尤其是瞬间很快消耗大量内存的进程,为了防止内