The shell script obtains a method for each CPU usage of the host.
#!/bin/Bashinterval=3Cpu_num=`Cat/proc/Stat|grepcpu[0-9] -C ' Start_idle=() Start_total=() cpu_rate=() Cpu_rate_file=./`hostname' _cpu_rate.csvif[-F ${cpu_rate_file}]; Then MV${cpu_rate_file} ${cpu_rate_file}. 'Date+%m_%d-%h_%m_%S '. Bakfi for((i=0; i<${cpu_num};i++)){ Echo-N"cpu$i,">>${cpu_rate_file}}Echo-N"Cpu_avg">>${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 $ "" $ "" $4 "" $ "" $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 $ "" $ "" $4 "" $ "" $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 $ "" $ "" $4 "" $ "" $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} \* -' Cpu_usage=`Expr${idle_normal}/${total} ' cpu_rate[$i]=`Expr --${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 $ "" $ "" $4 "" $ "" $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} \* -' Cpu_usage=`Expr${idle_normal}/${total} ' Cpu_rate[${cpu_num}]=`Expr --${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
Shell gets CPU usage