当前位置:编程学习 > python >>

python绘图cpu/mem监控曲线

1
输入日志格式举例:[2012-09-26 12:55:31] 16070 sosotest 20 0 8302m 41m 11m S 0 0.1 0:00.93 java 42836 8501676 HandleNum:28
view sourceprint?
001
# -*- coding: cp936 -*-
002
import re, sys, os
003
import time
004
from pylab import *
005
'''
006
修改:2010.04.23 增加开始运行时间信息 修改参数,将进程号作为文件名一部分
007
修改:2010.04.25 将参数1,2呼唤,第一个参数为日志文件,第二个为进程id
008
修改:2010.10.31 增加core点显示,core时记录在程序运行目录
009
修改:2010.11.30 将两幅图绘制在一起,日志加入ps产生的内存详细信息
010
修改:2011.06.24 加入进程名,表示有效结果日志
011
'''
012
 
013
def getCoreLst(proclst):
014
        retLst = []
015
        curPid = proclst[0]
016
        for i in range(1, len(proclst)):
017
                if curPid != proclst[i]:
018
                        curPid = proclst[i]
019
                        retLst.append(i+1)
020
        return retLst
021
        
022
virt = []
023
res = []
024
cpu = []
025
proc = []
026
allrcd = []#所有记录
027
timeInterval = 10
028
processname = "empty process"
029
if len(sys.argv) != 5 and len(sys.argv) != 4:
030
        print "Usage: python %s fileName mem_cpu.png processname timeinterval[default:10]" %(__file__)
031
        print "Usage: python %s fileName processname timeinterval[default:10]" %(__file__)
032
        sys.exit()
033
 
034
print "VIRT RES CPU%"
035
if len(sys.argv) == 5:
036
        srcfileName = sys.argv[2]
037
        processname = sys.argv[3]
038
        timeInterval = int(sys.argv[4])
039
else:
040
        srcfileName = "cpu_mem.png"
041
        processname = sys.argv[2]      
042
        timeInterval = int(sys.argv[3]) 
043
 
044
bFoundBeginTime = False
045
bFoundEndTime = False
046
 
047
strCurTime = ""
048
strBeginTime = ""
049
strEndTime = ""
050
 
051
 
052
f = open(sys.argv[1], "r")
053
line = f.readline()
054
strBeginTime = line[line.find('[')+1:line.find(']')]
055
f.seek(0, os.SEEK_SET)
056
while True:
057
    line = f.readline()
058
    if len(line)==0: break
059
        #if line.find("ProcessID:%s" % processId) == -1:
060
        #        continue
061
    line = line.strip()
062
 
063
        strEndTime = line[line.find('[')+1:line.find(']')] #获取当前时间
064
 
065
        try:
066
                if len(line.split()) != 17:
067
                    continue
068
                if not processname in line:
069
                    continue
070
                lp = line.split()[2:]
071
                lp0 = []
072
                for i in range(0, len(lp)):
073
                        if len(lp[i])>0: lp0.append(lp[i])
074
 
075
                #print lp0[]进程号
076
                proc.append(int(lp0[0]))
077
                
078
                #虚拟内存大小
079
                if lp0[4][-1]!='m' and lp0[4][-1]!='g': fRet = float(lp0[4])/(1024*1024)
080
                if lp0[4][-1]=='m': fRet = float(lp0[4][0:-1])/1024
081
                if lp0[4][-1]=='g': fRet = float(lp0[4][0:-1])
082
                virt.append(fRet)
083
                
084
                #实际内存大小
085
                if lp0[5][-1]!='m' and lp0[5][-1]!='g': fRet = float(lp0[5])/(1024*1024)
086
                if lp0[5][-1]=='m': fRet = float(lp0[5][0:-1])/1024
087
                if lp0[5][-1]=='g': fRet = float(lp0[5][0:-1])
088
                res.append(float(fRet))
089
                
090
                #cpu占用率
091
                cpu.append(float(lp0[8]))
092
                allrcd.append(line)
093
 
094
       
补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,