要进行的操作:
1.shell 脚本获取top命令下的结果,存入文件
2.用python 的matplotlib 画图
shell 部分
top -d 1 | grep 'test' | grep -v 'grep' >> /tmp/tmp_c.log
awk '{print $9}' /tmp/tmp_cpu.log >> /tmp/test_cpu.log
python部分:
# -*- coding:utf-8 -*-
import matplotlib.pyplot as pl
import numpy as np
import os
import string
import sys
def set_plot_attr(plt, title, xlabel, ylabel):
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.grid()
plt.legend()
pl.subplots_adjust(wspace=0.5, hspace=0.5)
def draw_cpu():
pro = 'node'
rootdir = './tmp/memdir'
mean_data = []
files = os.listdir(rootdir)
for file in files:
data = []
f = open(rootdir + '/' + file, 'r')
for line in f.readlines():
# import pudb; pudccccb.set_trace()
data.append(string.atof(line.strip()))
# import pudb; pudb.set_trace()
f.close()
pl.subplot(211)
pl.plot(data, label=file)
mean_data.append(np.mean(data))
set_plot_attr(pl, pro + ' mem Usage', 'Time (s)', ' mem Usage (%)')
pl.subplot(212)
pl.plot(mean_data)
set_plot_attr(pl, pro + ' Mean mem Usage', 'Time (s)', ' mem Usage (%)')
pl.show()
def usage():
print 'Usage: draw.py pro rootdir'
print ' pro: progress name'
print ' rootdir: log file directory'
if __name__ == '__main__':
draw_cpu()
上为画cpu曲线图的部分,如果要画内存,则截取$9,另外将文件中的cpu改为mem即可
脚本如上