系统内存统计曲线图
使用下面的脚本运行生成文本信息
#!/bin/sh
file=`date +"%x"`
filename=${file}.txt
while true
do
sleep 2
cat /proc/stat | sed -n 1p |awk '{print $2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8}' >> $filename
cat /proc/meminfo | sed -n 2p | awk '{print $2}' >> $filename
done
使用下面的python脚本 对获得的内存信息进行绘图
#!/usr/bin/python
import sys
import numpy as np
import matplotlib.pyplot as plt
if __name__ == "__main__":
sum = 0
count = 0
y_store =[]
y1_show = []
y2_show = []
y2_show = []
y3_show = []
y4_show = []
y5_show = []
y6_show = []
y7_show = []
y8_show = []
y9_show = []
with open(sys.argv[1], "r") as file:
while 1:
line = file.readline()
if line == '':
break
y_t = line.split()
if len(y_t) > 1:
count = count + 1
sum = 0
for num in y_t:
sum = sum + float(num)
y_store = []
for num in y_t:
y_store.append(float(num) * 100/sum)
y_store.append(100 - y_store[3])
y1_show.append(y_store[0])
y2_show.append(y_store[1])
y3_show.append(y_store[2])
y4_show.append(y_store[3])
y5_show.append(y_store[4])
y6_show.append(y_store[5])
y7_show.append(y_store[6])
y8_show.append(y_store[7])
elif len(y_t) == 1:
y9_show.append(float(y_t[0])/200)
x_aix = np.arange(0,2 * count, 2)
plt.xlabel('time:(s)')
plt.ylabel('loading:(%)/200KB')
plt.plot(x_aix, y1_show, linewidth = '2', color='r', label='usr')
plt.plot(x_aix, y2_show, linewidth = '2', color='k', label='niced')
plt.plot(x_aix, y3_show, linewidth = '2', color ='b', label='sys')
plt.plot(x_aix, y4_show, linewidth = '2', color = 'g', label='idle')
plt.plot(x_aix, y5_show, linewidth = '2', color = 'm', label='iowait')
plt.plot(x_aix, y6_show, linewidth = '2', color = 'y', label='irq')
plt.plot(x_aix, y7_show, linewidth = '2', color = 'k', label='softirq')
plt.plot(x_aix, y8_show, linewidth = '2', color = 'orange', label='loading')
plt.plot(x_aix, y9_show, linewidth = '2', color = 'black', label='free memory')
plt.legend()
plt.show()
file.close()
绘制的统计图如下图: