1.首先创建钉钉机器人获取到webhook。
2.根据脚本更改自己需求要监控的服务进程即可。
#!/bin/bash
#定义钉钉接口
dingding_webhook="这里钉钉获取的Webhook"
#监控的某个进程这里把需要监控的在grep中替换即可
process_count=$(ps aux | grep "php think queue:work --timeout 0" | grep -v grep | wc -l)
if [ $process_count -eq 0 ]; then
# message里面自己定义告警的内容
message="生产服务器进程告警 'php think queue:work --timeout 0' 不存在,请检查!"
elif [ $process_count -gt 1 ]; then
message="生产服务器存在多个进程告警 'php think queue:work --timeout 0',请检查!"
fi
#将监控的内容通过钉钉发送到群
if [ ! -z "$message" ]; then
curl $dingding_webhook -H 'Content-Type: application/json' -d "{\"msgtype\": \"text\", \"text\": {\"content\": \"$message\"}}"
fi
3.最后加入定时脚本进行监控 根据需求,例如我这个2分钟监控一次
# crontab -e
*/2 * * * * /bin/bash /root/lien_php.sh
4.看下效果