shell中计算除法,基本的方法有几种
2.MAILX=/usr/bin/mailx 3.NOTIFY="194915592@qq.com" 4.FROM="***@****.com" 5.DATE=`date %Y%m%d%H` 6.DIR="/root/monitor/" 7.HOSTNAME=`hostname` 8.NUM="100" 10.cd $DIR 11.rm report_cpu_mem.`date -d -1day %Y%m%d`* 12.rm report_cpu_mem.$DATE 13.########################### cpu ########################## 14.#system used percent of cpu and user used percent of cpu e.g 3.2%usr , 4.5%sys , user_cpu_percent=3.2 ,sys_cpu_percent=4.5 15.user_cpu_percent=`top -n 1 |grep Cpu|awk '{print $2}'|cut -d "%" -f 1` 16.sys_cpu_percent=`top -n 1 |grep Cpu |awk '{print $3 }' |cut -d "%" -f 1` 18.total_cpu_percent=`echo "$user_cpu_percent $sys_cpu_percent"|bc` 19.#cpu number e.g 60% used , then usage_cpu=0.6 20.usage_cpu=`echo "scale=2;$total_cpu_percent/$NUM"|bc` 22.########################### mem ########################## 24.#total memory and used memory , e.g total 25620K , used 2222K, then total_mem=25620, used_mem=2222k 25.total_mem=`top -n 1 |grep Mem |awk '{print $3}'|cut -d "k" -f 1` 26.used_mem=`top -n 1 |grep Mem |awk '{print $5}'|cut -d "k" -f 1` 28.#memory number e.g 30% used, then usage_mem=0.3 29.usage_mem=`echo "scale=2;$used_mem/$total_mem"|bc` 31.#the percent of memory used , e.g 63.3% , then usage_mem_percent=63.6 32.usage_mem_percent=`echo "0"$usage_mem \* 100|bc` 36.flag=`expr "0"$usage_cpu \> "0.95"` 37.flag1=`expr "0"$usage_mem \> "0.95"` 39.if [ "$flag" == "1" ];then 40.echo "The cpu Utilization on $HOSTNAME is over 95%. " >>report_cpu_mem.$DATE 41.echo "user had used "$total_cpu_percent"% cpu" >>report_cpu_mem.$DATE 42.echo "system had used "$sys_cpu"% cpu">>report_cpu_mem.$DATE 43.echo "Please check below processes:" >>report_cpu_mem.$DATE 45.ps aux|head -1;ps aux|grep -v PID|sort -rn -k 3|head >>report_cpu_mem.$DATE 46.$MAILX -s "the cpu of $hostname used too much , please check it" $NOTIFY<report_cpu_mem.$DATE 47.fi 48.if [ "$flag1" == "1" ] ;then 50.echo "The mem Utilization on $HOSTNAME is over 95%. " >>report_cpu_mem.$DATE 51.echo "this host had used $usage_mem_percent"%" memory" >>report_cpu_mem.$DATE 52.echo "Please check below processes:" >>report_cpu_mem.$DATE 54.ps aux|head -1;ps aux|grep -v PID|sort -rn -k 4|head >>report_cpu_mem.$DATE 55.$MAILX -s "the host $HOSTNAME used too much memory or cpu , please check it" $NOTIFY<report_cpu_mem.$DATE 57.fi (责任编辑:IT) |