1. 百分比
#!/bin/bash a=38 b=99 percent_1=$(printf "%d%%" $((a*100/b))) # 或者 percent_2=`awk 'BEGIN{printf "%.1f%%\n",('$a'/'$b')*100}'` # 保留1位小数,四舍五入 echo $percent_1 echo $percent_2
2. 内存可用率低于20%的时候告警
[root@XWBizEngine04 0206]# cat /opt/0206/restart_mem.sh #!/bin/bash # author dxw cur_datetime=`date +%Y%m%d-%H:%M:%S` homeDir=`dirname $0` echo $homeDir function baojing { curl -XPOST -s -L 'https://oapi.dingtalk.com/robot/send?access_token=e73b96f7d1bfa08e8d127sba730cfd1961bfefnishibash075b09262dbd3bf56d8b79' -H 'Content-Type: application/json' -H 'charset:utf-8' -d '{"msgtype": "text", "text": { "content": " '$1' mem less then 20%!http_code " } }' } #############检查CPU情况 ip=`/usr/sbin/ifconfig |grep broadcast |awk '{print $2}'` MemAvailable=`cat /proc/meminfo |grep 'MemAvailable' |awk '{print $2}'` MemTotal=`cat /proc/meminfo |grep 'MemTotal' |awk '{print $2}'` percent_mem=`awk 'BEGIN{printf "%d\n",('$MemAvailable'/'$MemTotal')*100}'` echo $percent_mem if [ $percent_mem -lt 20 ] then cd /home/apaas/dynamic-bizengine && sh deploy.sh restart baojing $ip fi [root@XWBizEngine04 0206]#
标签:mem,percent,相除,echo,0206,awk,内存,print,小数 From: https://www.cnblogs.com/hixiaowei/p/17110400.html