1.钉钉群添加机器人
添加自定义机器人,设置关键字,在shell脚本 发送信息中加入该关键字 保留webhook信息,添加到脚本中
2.shell脚本检查状态存活
#!/bin/bash
#主机信息
name=`hostname`
ip=`ip a show |grep eth0|grep inet |awk '{print $2}'|awk -F '/' '{print $1}'`
date=`date '+%Y-%m-%d-%H:%M:%S'`
#自定义接入钉钉函数
function SendMessageToDingding(){
#钉钉机器人接口地址
url="https://oapi.dingtalk.com/robot/send?access_token=885...8ea8"
#推送到钉钉
curl -XPOST -s -L $url -H "Content-Type:application/json" -H "charset:utf-8" -d "
{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"aws 监控主机名:$1\n主机IP: $2\n 异常服务:$3\n 巡检时间:$4\"
},
'at': {
'isAtAll': true
}
}"
}
#输入端口变量文件
cat /root/scripts/ServicePort.txt | while read PORT
do
stat=` netstat -antup | grep $PORT|grep -v grep `
#判断服务状态
if [ -z "$stat" ];then
#调用函数
SendMessageToDingding $name $ip $PORT $date
fi
done
#输入服务变量文件
cat /root/scripts/service.txt | while read service
do
stat=`systemctl status $service |grep Active|awk '{print $3}' |cut -c 2-8`
#判断服务状态
if [ $stat != running ];then
#调用函数
SendMessageToDingding $name $ip $service $date
fi
done
3.定时任务 监测服务
crontab -l
30 8 * * * /bin/bash /root/scripts/check_alive.sh > /dev/null 2&1