@xiaoxiaowang
2018-03-17T16:06:41.000000Z
字数 1966
阅读 2806
运维 微信报警
#!/bin/shexpireTime=7200dbFile="db.json"corpid=xxxcorpsecret=xxxtouser="xxx"toparty="xxx"agentid="xxx"content="服务器快崩了,你还在这里吟诗作对?"# s 为秒,m 为 分钟,h 为小时,d 为日数interval=1s## 发送报警信息sendMsg(){if [ ! -f "$dbFile" ];thentouch "$dbFile"fi# 获取tokenreq_time=`jq '.req_time' $dbFile`current_time=$(date +%s)refresh=falseif [ ! -n "$req_time" ];thenrefresh=trueelseif [ $((current_time-req_time)) -gt $expireTime ];thenrefresh=truefifiif $refresh ;thenreq_access_token_url=https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid\&corpsecret=$corpsecretaccess_res=$(curl -s -G $req_access_token_url | jq -r '.access_token')## 保存文件echo "" > $dbFileecho -e "{" > $dbFileecho -e "\t\"access_token\":\"$access_res\"," >> $dbFileecho -e "\t\"req_time\":$current_time" >> $dbFileecho -e "}" >> $dbFileecho ">>>刷新Token成功<<<"fi## 发送消息msg_body="{\"touser\":\"$touser\",\"toparty\":\"$toparty\",\"msgtype\":\"text\",\"agentid\":$agentid,\"text\":{\"content\":\"$content\"}}"access_token=`jq -r '.access_token' $dbFile`req_send_msg_url=https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_tokenreq_msg=$(curl -s -H "Content-Type: application/json" -X POST -d $msg_body $req_send_msg_url | jq -r '.errmsg')echo "触发报警发送动作,返回信息为:" $req_msg}loopMonitor(){echo 'loop'flag=`uptime | awk '{printf "%.2f\n", $11 "\n"}'`# 0.7 这个阈值可以视情况而定,如cpu核数为n,则可以设置为0.7 * n 具体视情况而定c=$(echo "$flag > 0.7" | bc)echo ">>>>>>>>>>>>>>>>>>`date`<<<<<<<<<<<<<<<<<<"free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'uptime | awk '{printf "CPU Load: %.2f\n", $11 "\n"}'echo ">>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<<<"if [ $c -eq 1 ];thensendMsgfi}while true; doloopMonitorsleep $intervaldone
让CPU达到100%的脚本
for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/dev/null & done
下周放线上试一试~
CSDN:http://blog.csdn.net/qqhjqs?viewmode=list
博客:http://vector4wang.tk/
简书:https://www.jianshu.com/u/223a1314e818
Github:https://github.com/vector4wang
Gitee:https://gitee.com/backwxc


