查看当前cpu总利用率
# method 1
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
# method 2
top -bn1 | grep "Cpu(s)"| sed "s/.*, *\([0-9.]*\)%* id.*/\1/"| awk '{print 100 - $1"%"}'
# method 3 更贴近于top统计
ps -A -o pcpu | tail -n+2 | paste -sd+ | bc
cpu_eff=0
time_stamp=`date +%y%m%d%H%M%S`
while :
do
cpu_eff=`ps -A -o pcpu | tail -n+2 | paste -sd+ | bc`
echo $cpu_eff | tee -a cpu_eff_${time_stamp}.log
sleep 1
done
画图
- 读文件,使用matplotlib 画出折现图
- 去掉“\n”
s1 = [l.strip('\n') for l in input_1.readlines()]
import numpy as np
import matplotlib.pyplot as plt
import sys
# read data
print(sys.argv)
input_1 = open(sys.argv[1], 'r')
# deal with data
s1 = [l.strip('\n') for l in input_1.readlines()]
s1 = [float(i) for i in s1]
# print s1
t1 = np.arange(0, len(s1))
# print t1
# draw pic
fig, axs = plt.subplots()
axs.plot(t1, s1, "b--",linewidth=1)
# set axis
axs.set_xlabel('time / second')
plt.yticks(np.linspace(0,4000,num=10))
axs.set_ylabel('cpu utilization / %')
# set title
axs.set_title(r'Original CPU Utilization')
# set grid
axs.grid(True)
# save pic
plt.savefig("hhh.jpg")
# show pic
plt.show()