#!/bin/bash #disk_used_rate Location=/dev/xvdb Disk_Used_Rate=$(df -h | grep $Location | awk ‘{print $5}‘) echo "Disk Used Rate : $Disk_Used_Rate" #memory_used_rate LoadMemory=$(cat /proc/meminfo | awk ‘{print $2}‘) Total=$(echo $LoadMemory | awk ‘{print $1}‘) Free1=$(echo $LoadMemory | awk ‘{print $2}‘) Free2=$(echo $LoadMemory | awk ‘{print $3}‘) Free3=$(echo $LoadMemory | awk ‘{print $4}‘) Used=`expr $Total - $Free1 - $Free2 - $Free3` Used_Rate=`expr $Used/$Total*100` Memory_Used_Rate=`expr $Used_Rate/1` echo "Memory Used Rate : $Memory_Used_Rate%" interval=3 cpu_num=`cat /proc/stat | grep cpu[0-9] -c` start_idle=() start_total=() cpu_rate=() 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 "CPU$i Rate : ${cpu_rate[$i]}%" } 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 "Average CPU Rate : ${cpu_rate[${cpu_num}]}%" echo -e "\nProccess of CPU used rate > 100%" ps -aux | awk ‘$3 > 1{print $2,$3,$11}‘
时间: 2024-10-06 16:09:42