python绘图cpu/mem监控曲线

输入日志格式举例:[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
# -*- coding: cp936 -*-
import re, sys, os
import time
from pylab import *
'''
修改:2010.04.23 增加开始运行时间信息 修改参数,将进程号作为文件名一部分
修改:2010.04.25 将参数1,2呼唤,第一个参数为日志文件,第二个为进程id
修改:2010.10.31 增加core点显示,core时记录在程序运行目录
修改:2010.11.30 将两幅图绘制在一起,日志加入ps产生的内存详细信息
修改:2011.06.24 加入进程名,表示有效结果日志
'''

def getCoreLst(proclst):
        retLst = []
        curPid = proclst[0]
        for i in range(1, len(proclst)):
                if curPid != proclst[i]:
                        curPid = proclst[i]
                        retLst.append(i+1)
        return retLst
        
virt = []
res = []
cpu = []
proc = []
allrcd = []#所有记录
timeInterval = 10
processname = "empty process"
if len(sys.argv) != 5 and len(sys.argv) != 4:
        print "Usage: python %s fileName mem_cpu.png processname timeinterval[default:10]" %(__file__)
        print "Usage: python %s fileName processname timeinterval[default:10]" %(__file__)
        sys.exit()

print "VIRT RES CPU%"
if len(sys.argv) == 5:
        srcfileName = sys.argv[2]
        processname = sys.argv[3]
        timeInterval = int(sys.argv[4])
else:
        srcfileName = "cpu_mem.png"
        processname = sys.argv[2]        
        timeInterval = int(sys.argv[3])   

bFoundBeginTime = False
bFoundEndTime = False

strCurTime = ""
strBeginTime = ""
strEndTime = ""


f = open(sys.argv[1], "r")
line = f.readline()
strBeginTime = line[line.find('[')+1:line.find(']')]
f.seek(0, os.SEEK_SET)
while True:
	line = f.readline()
	if len(line)==0: break
        #if line.find("ProcessID:%s" % processId) == -1:
        #        continue
	line = line.strip()

        strEndTime = line[line.find('[')+1:line.find(']')] #获取当前时间

        try:
                if len(line.split()) != 17:
                    continue
                if not processname in line:
                    continue
                lp = line.split()[2:]
                lp0 = []
                for i in range(0, len(lp)):
                        if len(lp[i])>0: lp0.append(lp[i])

                #print lp0[]进程号
                proc.append(int(lp0[0]))
                
                #虚拟内存大小
                if lp0[4][-1]!='m' and lp0[4][-1]!='g': fRet = float(lp0[4])/(1024*1024)
                if lp0[4][-1]=='m': fRet = float(lp0[4][0:-1])/1024
                if lp0[4][-1]=='g': fRet = float(lp0[4][0:-1])
                virt.append(fRet)
                
                #实际内存大小
                if lp0[5][-1]!='m' and lp0[5][-1]!='g': fRet = float(lp0[5])/(1024*1024)
                if lp0[5][-1]=='m': fRet = float(lp0[5][0:-1])/1024
                if lp0[5][-1]=='g': fRet = float(lp0[5][0:-1])
                res.append(float(fRet))
                
                #cpu占用率
                cpu.append(float(lp0[8]))
                allrcd.append(line)

        except:
                print "Line data Error as follow..."
                print line 
	#time.sleep(1)
	#print "abc", lp0[4], lp0[5], lp0[8]
f.close()

print "Show len ",len(virt)
print "耗时%.3f小时" % (timeInterval*len(virt)/(60*60.0))
print "开始时间: %s" % strBeginTime
print "结束时间: %s" % strEndTime

print virt
xSpan = len(virt)
a = max(virt)
b = max(res)
ySpan = max(a, b)#内存最高点
cpu_mem_rate = max(cpu) / ySpan#计算cpu与内存的归一化比率

#归一化CPU损耗
for i in range(0, len(cpu)):
        cpu[i] = cpu[i] / cpu_mem_rate


corelst_x = getCoreLst(proc)

corelst_cpu_y = []
corelst_virt_y = []
corelst_res_y = []

for i in range(0,len(corelst_x)):
        corelst_cpu_y.append(cpu[corelst_x[i]])
        corelst_virt_y.append(virt[corelst_x[i]])
        corelst_res_y.append(res[corelst_x[i]])

#统计core点开始
fpCorePoint=file("error_core.log", "at")
for i in range(0, len(corelst_x)):
        fpCorePoint.write("%s\n" % allrcd[corelst_x[i]])
fpCorePoint.close()
#统计core点结束


#为了画点,将横轴坐标乘以时间间隔
for i in range(0, len(corelst_x)):
        corelst_x[i]=corelst_x[i]*timeInterval


strDuration = "StartTime:%s EndTime:%s \n Duration %d minutes %d hours" % (strBeginTime, strEndTime, len(virt)*timeInterval/60, len(virt)*timeInterval/3600)
x = []
for i in range(0, len(virt), 1):
        x.append(timeInterval*i)#每10秒获取一次信息

#先绘制CPU图
print "Cpu len: %d %d " % (len(x), len(cpu))
figure(1)
axis([0, len(cpu), 0, max(cpu)])#布置绘图区域
strTitle = "______________________________\n" + strDuration + " CPU_rate:%.2f \n--------------------------" % cpu_mem_rate
title(strTitle, fontsize=12)
xlabel("TimeEclipsed/sec", fontsize=12, ha='left')
ylabel('CPU-Rate:%.2f | MEM/g' % cpu_mem_rate , fontsize=12, va = 'top')
plot(x, cpu, 'g')  #绘制cpu
#画core点
print "CPU Core Info:"
print "Core at ", corelst_x
print "Core at ", corelst_cpu_y
#plot(corelst_x, corelst_cpu_y, "rs") #绘制cpu的core点

#title("MEM-%s \n%s" % (strBeginTime, strDuration), fontsize=12)
#xlabel("TimeEclipsed/sec", fontsize=12)
#ylabel('Memery/g', fontsize=12)
plot(x, virt, 'b')#绘制虚拟内存
plot(x, res, 'r') #绘制物理内存
#画core点
print "MEM Core Info:"
print "Core at ", corelst_x
print "Core at ", corelst_virt_y
print "Core at ", corelst_res_y

#plot(corelst_x, corelst_virt_y, "rs") #绘制虚拟内存的core点
plot(corelst_x, corelst_res_y, "cs")  #绘制物理内存的core点

grid()
savefig(srcfileName, dpi=85)

strCmd = srcfileName + "/b"
os.system(strCmd)

你可能感兴趣的:(python绘图cpu/mem监控曲线)