python绘图cpu/mem监控曲线
程序员文章站
2023-11-19 12:25:04
1
输入日志格式举例:[2012-09-26 12:55:31] 16070 sosotest 20 0 8302m 41m 11m s 0 0.1 0:00.93 java 4...
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
except:
095
print "line data error as follow..."
096
print line
097
#time.sleep(1)
098
#print "abc", lp0[4], lp0[5], lp0[8]
099
f.close()
100
101
print "show len ",len(virt)
102
print "耗时%.3f小时" % (timeinterval*len(virt)/(60*60.0))
103
print "开始时间: %s" % strbegintime
104
print "结束时间: %s" % strendtime
105
106
print virt
107
xspan = len(virt)
108
a = max(virt)
109
b = max(res)
110
yspan = max(a, b)#内存最高点
111
cpu_mem_rate = max(cpu) / yspan#计算cpu与内存的归一化比率
112
113
#归一化cpu损耗
114
for i in range(0, len(cpu)):
115
cpu[i] = cpu[i] / cpu_mem_rate
116
117
118
corelst_x = getcorelst(proc)
119
120
corelst_cpu_y = []
121
corelst_virt_y = []
122
corelst_res_y = []
123
124
for i in range(0,len(corelst_x)):
125
corelst_cpu_y.append(cpu[corelst_x[i]])
126
corelst_virt_y.append(virt[corelst_x[i]])
127
corelst_res_y.append(res[corelst_x[i]])
128
129
#统计core点开始
130
fpcorepoint=file("error_core.log", "at")
131
for i in range(0, len(corelst_x)):
132
fpcorepoint.write("%s\n" % allrcd[corelst_x[i]])
133
fpcorepoint.close()
134
#统计core点结束
135
136
137
#为了画点,将横轴坐标乘以时间间隔
138
for i in range(0, len(corelst_x)):
139
corelst_x[i]=corelst_x[i]*timeinterval
140
141
142
strduration = "starttime:%s endtime:%s \n duration %d minutes %d hours" % (strbegintime, strendtime, len(virt)*timeinterval/60, len(virt)*timeinterval/3600)
143
x = []
144
for i in range(0, len(virt), 1):
145
x.append(timeinterval*i)#每10秒获取一次信息
146
147
#先绘制cpu图
148
print "cpu len: %d %d " % (len(x), len(cpu))
149
figure(1)
150
axis([0, len(cpu), 0, max(cpu)])#布置绘图区域
151
strtitle = "______________________________\n" + strduration + " cpu_rate:%.2f \n--------------------------" % cpu_mem_rate
152
title(strtitle, fontsize=12)
153
xlabel("timeeclipsed/sec", fontsize=12, ha='left')
154
ylabel('cpu-rate:%.2f | mem/g' % cpu_mem_rate , fontsize=12, va = 'top')
155
plot(x, cpu, 'g') #绘制cpu
156
#画core点
157
print "cpu core info:"
158
print "core at ", corelst_x
159
print "core at ", corelst_cpu_y
160
#plot(corelst_x, corelst_cpu_y, "rs") #绘制cpu的core点
161
162
#title("mem-%s \n%s" % (strbegintime, strduration), fontsize=12)
163
#xlabel("timeeclipsed/sec", fontsize=12)
164
#ylabel('memery/g', fontsize=12)
165
plot(x, virt, 'b')#绘制虚拟内存
166
plot(x, res, 'r') #绘制物理内存
167
#画core点
168
print "mem core info:"
169
print "core at ", corelst_x
170
print "core at ", corelst_virt_y
171
print "core at ", corelst_res_y
172
173
#plot(corelst_x, corelst_virt_y, "rs") #绘制虚拟内存的core点
174
plot(corelst_x, corelst_res_y, "cs") #绘制物理内存的core点
175
176
grid()
177
savefig(srcfilename, dpi=85)
178
179
strcmd = srcfilename + "/b"
180
os.system(strcmd)
输入日志格式举例:[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
except:
095
print "line data error as follow..."
096
print line
097
#time.sleep(1)
098
#print "abc", lp0[4], lp0[5], lp0[8]
099
f.close()
100
101
print "show len ",len(virt)
102
print "耗时%.3f小时" % (timeinterval*len(virt)/(60*60.0))
103
print "开始时间: %s" % strbegintime
104
print "结束时间: %s" % strendtime
105
106
print virt
107
xspan = len(virt)
108
a = max(virt)
109
b = max(res)
110
yspan = max(a, b)#内存最高点
111
cpu_mem_rate = max(cpu) / yspan#计算cpu与内存的归一化比率
112
113
#归一化cpu损耗
114
for i in range(0, len(cpu)):
115
cpu[i] = cpu[i] / cpu_mem_rate
116
117
118
corelst_x = getcorelst(proc)
119
120
corelst_cpu_y = []
121
corelst_virt_y = []
122
corelst_res_y = []
123
124
for i in range(0,len(corelst_x)):
125
corelst_cpu_y.append(cpu[corelst_x[i]])
126
corelst_virt_y.append(virt[corelst_x[i]])
127
corelst_res_y.append(res[corelst_x[i]])
128
129
#统计core点开始
130
fpcorepoint=file("error_core.log", "at")
131
for i in range(0, len(corelst_x)):
132
fpcorepoint.write("%s\n" % allrcd[corelst_x[i]])
133
fpcorepoint.close()
134
#统计core点结束
135
136
137
#为了画点,将横轴坐标乘以时间间隔
138
for i in range(0, len(corelst_x)):
139
corelst_x[i]=corelst_x[i]*timeinterval
140
141
142
strduration = "starttime:%s endtime:%s \n duration %d minutes %d hours" % (strbegintime, strendtime, len(virt)*timeinterval/60, len(virt)*timeinterval/3600)
143
x = []
144
for i in range(0, len(virt), 1):
145
x.append(timeinterval*i)#每10秒获取一次信息
146
147
#先绘制cpu图
148
print "cpu len: %d %d " % (len(x), len(cpu))
149
figure(1)
150
axis([0, len(cpu), 0, max(cpu)])#布置绘图区域
151
strtitle = "______________________________\n" + strduration + " cpu_rate:%.2f \n--------------------------" % cpu_mem_rate
152
title(strtitle, fontsize=12)
153
xlabel("timeeclipsed/sec", fontsize=12, ha='left')
154
ylabel('cpu-rate:%.2f | mem/g' % cpu_mem_rate , fontsize=12, va = 'top')
155
plot(x, cpu, 'g') #绘制cpu
156
#画core点
157
print "cpu core info:"
158
print "core at ", corelst_x
159
print "core at ", corelst_cpu_y
160
#plot(corelst_x, corelst_cpu_y, "rs") #绘制cpu的core点
161
162
#title("mem-%s \n%s" % (strbegintime, strduration), fontsize=12)
163
#xlabel("timeeclipsed/sec", fontsize=12)
164
#ylabel('memery/g', fontsize=12)
165
plot(x, virt, 'b')#绘制虚拟内存
166
plot(x, res, 'r') #绘制物理内存
167
#画core点
168
print "mem core info:"
169
print "core at ", corelst_x
170
print "core at ", corelst_virt_y
171
print "core at ", corelst_res_y
172
173
#plot(corelst_x, corelst_virt_y, "rs") #绘制虚拟内存的core点
174
plot(corelst_x, corelst_res_y, "cs") #绘制物理内存的core点
175
176
grid()
177
savefig(srcfilename, dpi=85)
178
179
strcmd = srcfilename + "/b"
180
os.system(strcmd)