注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Aspirer's blog

停止维护,新博客地址:http://aspirer.wang/

 
 
 

日志

 
 

linux获取cpu利用率shell脚本  

2013-04-23 13:09:21|  分类: 学习心得 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#!/bin/bash
interval=3
cpu_num=`cat /proc/stat | grep cpu[0-9] -c`

start_idle=()
start_total=()
cpu_rate=()

cpu_rate_file=./`hostname`_cpu_rate.csv
if [ -f ${cpu_rate_file} ]; then
mv ${cpu_rate_file} ${cpu_rate_file}.`date +%m_%d-%H_%M_%S`.bak
fi
for((i=0;i<${cpu_num};i++))
{
echo -n "cpu$i," >> ${cpu_rate_file}
}
echo -n "cpu" >> ${cpu_rate_file}
echo "" >> ${cpu_rate_file}

while(true)
do
for((i=0;i<${cpu_num};i++))
{
start=$(cat /proc/stat | grep "cpu$i" | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
start_idle[$i]=$(echo ${start} | awk '{print $4}')
start_total[$i]=$(echo ${start} | awk '{printf "%.f",$1+$2+$3+$4+$5+$6+$7}')
}
start=$(cat /proc/stat | grep "cpu " | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
start_idle[${cpu_num}]=$(echo ${start} | awk '{print $4}')
start_total[${cpu_num}]=$(echo ${start} | awk '{printf "%.f",$1+$2+$3+$4+$5+$6+$7}')
sleep ${interval}
for((i=0;i<${cpu_num};i++))
{
end=$(cat /proc/stat | grep "cpu$i" | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
end_idle=$(echo ${end} | awk '{print $4}')
end_total=$(echo ${end} | awk '{printf "%.f",$1+$2+$3+$4+$5+$6+$7}')
idle=`expr ${end_idle} - ${start_idle[$i]}`
total=`expr ${end_total} - ${start_total[$i]}`
idle_normal=`expr ${idle} \* 100`
cpu_usage=`expr ${idle_normal} / ${total}`
cpu_rate[$i]=`expr 100 - ${cpu_usage}`
echo "The CPU$i Rate : ${cpu_rate[$i]}%"
echo -n "${cpu_rate[$i]}," >> ${cpu_rate_file}
}
end=$(cat /proc/stat | grep "cpu " | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
end_idle=$(echo ${end} | awk '{print $4}')
end_total=$(echo ${end} | awk '{printf "%.f",$1+$2+$3+$4+$5+$6+$7}')
idle=`expr ${end_idle} - ${start_idle[$i]}`
total=`expr ${end_total} - ${start_total[$i]}`
idle_normal=`expr ${idle} \* 100`
cpu_usage=`expr ${idle_normal} / ${total}`
cpu_rate[${cpu_num}]=`expr 100 - ${cpu_usage}`
echo "The average CPU Rate : ${cpu_rate[${cpu_num}]}%"
echo -n "${cpu_rate[${cpu_num}]}" >> ${cpu_rate_file}
echo "------------------"
echo "" >> ${cpu_rate_file}
done

输出是逗号分隔符文件(.csv),可以用excel直接打开,方便数据分析和绘图等。
cpu0,cpu1,cpu2,cpu3,cpu4,cpu5,cpu6,cpu7,cpu8,cpu9,cpu10,cpu11,cpu12,cpu13,cpu14,cpu15,cpu16,cpu17,cpu18,cpu19,cpu20,cpu21,cpu22,cpu23,cpu_avg
4,1,2,13,11,5,2,4,1,1,3,4,7,5,12,16,3,1,4,5,1,2,2,1,5
4,1,2,13,12,1,8,4,4,5,15,7,2,5,3,1,1,1,1,4,3,2,1,0,4
4,2,1,15,11,2,5,6,4,6,7,4,3,4,2,4,1,8,1,3,3,2,1,1,4
6,3,3,15,11,3,5,8,6,4,11,4,8,18,7,4,1,1,5,11,4,6,4,5,7
22,1,2,14,10,2,5,6,6,6,7,8,1,5,10,8,1,2,1,3,3,1,1,1,5


cpu0 cpu1 cpu2 cpu3 cpu4 cpu5 cpu6 cpu7 cpu8 cpu9 cpu10 cpu11 cpu12 cpu13 cpu14 cpu15 cpu16 cpu17 cpu18 cpu19 cpu20 cpu21 cpu22 cpu23 cpu_avg
4 1 2 13 11 5 2 4 1 1 3 4 7 5 12 16 3 1 4 5 1 2 2 1 5
4 1 2 13 12 1 8 4 4 5 15 7 2 5 3 1 1 1 1 4 3 2 1 0 4
4 2 1 15 11 2 5 6 4 6 7 4 3 4 2 4 1 8 1 3 3 2 1 1 4
6 3 3 15 11 3 5 8 6 4 11 4 8 18 7 4 1 1 5 11 4 6 4 5 7
22 1 2 14 10 2 5 6 6 6 7 8 1 5 10 8 1 2 1 3 3 1 1 1 5
  评论这张
 
阅读(2983)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018