相除 小数 内存利用率 告警

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]#

 

posted @ 2023-02-10 22:01  littlevigra  阅读(12)  评论(0编辑  收藏  举报