Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1184884
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-09 10:56:43

linux获取cpu利用率shell脚本

			

#!/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

阅读(1860) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~