linux利用/proc/stat计算CPU利用率

#!/bin/sh

##echo user nice system idle iowait irq softirq
CPULOG_1=$(cat /proc/stat | grep ‘cpu ‘ | awk ‘{print $2″ “$3” “$4” “$5” “$6” “$7” “$8}’)
SYS_IDLE_1=$(echo $CPULOG_1 | awk ‘{print $4}’)
Total_1=$(echo $CPULOG_1 | awk ‘{print $1+$2+$3+$4+$5+$6+$7}’)

sleep 1

CPULOG_2=$(cat /proc/stat | grep ‘cpu ‘ | awk ‘{print $2” “$3” “$4” “$5” “$6” “$7” “$8}’)
SYS_IDLE_2=$(echo $CPULOG_2 | awk ‘{print $4}’)
Total_2=$(echo $CPULOG_2 | awk ‘{print $1+$2+$3+$4+$5+$6+$7}’)

SYS_IDLE=`expr $SYS_IDLE_2 – $SYS_IDLE_1`

Total=`expr $Total_2 – $Total_1`
SYS_USAGE=`expr $SYS_IDLE/$Total*100 |bc -l`

SYS_Rate=`expr 100-$SYS_USAGE |bc -l`

Disp_SYS_Rate=`expr “scale=3; $SYS_Rate/1” |bc`
echo $Disp_SYS_Rate%

发表评论

电子邮件地址不会被公开。 必填项已用*标注