一:需求
使用Shell脚本监控主机连通性(通过ping的方式判断主机是否还在线)或http服务的可访问性,如果发现异常把异常消息发送到钉钉群。
(1)首先在钉钉中添加webhook自定义机器人 (2)编写sh脚本,测试发送内容 。 (3)编写触发日志报警的脚本 (4)最后一步,利用linux系统本身的 定时任务,执行这个触发日志报警的任务 crontab -e,我们暂时用每15分钟执行一次 (5)如果服务宕掉或者有异常发生,将会触发 报警,
二:自定义钉钉机器人
1.建一个钉钉群
2.创建自定义机器人
建群成功后,点击右上角的“群设置”按钮: 点击“智能群助手” --> 点击“添加机器人” 点击添加自定义机器人 这里的安全设置选项是必须选的,可以选一个或多个 勾选“自定义关键词”,设置关键词。 在发送的消息里,必须带有设置的关键词,才能发送成功
其它安全设置参考:https://blog.csdn.net/HeyShHeyou/article/details/104277083 安全设置官网参考: https://open.dingtalk.com/document/robots/customize-robot-security-settings
Webhook地址复制保存好,不要公布在外部网站上,泄露后有安全风险。
3.shell监控主机连通信脚本:
[root@mail data]
pwd=/data
ip_list=`cat $pwd/ip.txt`
for ip in $ip_list;do
fail_count=0
for ((i=1;i<=3;i++)); do
if ping -c 1 $ip > /dev/null;then
break
else
let fail_count++
fi
done
if [ $fail_count -eq 3 ];then
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx' -H 'Content-Type: application/json' -d "{'msgtype': 'text','text': {'content':'监控告警!!!\n网络连通性告警\n源IP地址: 192.168.120.22\n目标IP地址: $ip\n'},'at': {'isAtAll': true}}"
fi
done
4.shell监控http服务可访问性脚本:
[root@mail data]
pwd=/data
http_url=`cat $pwd/http_url.txt`
for url in $http_url;do
fail_count=0
status_code=`curl -s -o /dev/null $url -w "%{http_code}\n"`
for ((i=0;i<=3;i++));do
if [ $status_code -ge 200 ];then
break
else
let fail_count++
fi
done
time=$(date "+%Y-%m-%d %H:%M:%S")
if [ $fail_count -ge 3 ];then
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxx' -H 'Content-Type: application/json' -d "{'msgtype': 'text','text': {'content':'监控告警!!!\nhttp地址: $url 无法正常访问,请及时查看!\n访问时间: $time \n'},'at': {'isAtAll':true}}"
fi
done
5.shell监控windows(以监控端口连通性方式实现)
[root@manager-1 dingding]
pwd=/data/dingding
nc_ip=`cat $pwd/nc_ip.txt`
for ip in $nc_ip;do
nc=`nc -z -w 3 $ip 3389`
result_code=`echo $?`
time=$(date "+%Y-%m-%d %H:%M:%S")
if [ $result_code -ne 0 ];then
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxx' -H 'Content-Type: application/json' -d "{'msgtype': 'text','text': {'content':'监控告警!!!\n网络连通性告警 \n源ip: 10.161.54.32\n目标ip: $ip(windows) 3389\n无法正常访问,请及时查看!\n访问时间: $time \n'},'at': {'isAtAll':true}}"
fi
done
nc=`nc -z -w 3 10.160.93.16 9200`
result_code2=`echo $?`
if [ $result_code2 -ne 0 ];then
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxx' -H 'Content-Type: application/json' -d "{'msgtype': 'text','text': {'content':'监控告警!!!\n源IP地址: 10.161.54.32\nhttp地址: https://10.160.93.16:9200(es访问url) \n无法正常访问,请及时查看!\n访问时间: $time \n'},'at': {'isAtAll':true}}"
fi
[root@mail data]
pwd=/data
nc_ip=`cat $pwd/nc_ip.txt`
for ip in $nc_ip;do
if [ $ip == '192.168.120.22' ];then
nc=`nc -z -w 5 $ip 82`
result_code=`echo $?`
elif [ $ip == '192.168.120.24' ];then
nc=`nc -z -w 5 $ip 80`
result_code=`echo $?`
fi
time=$(date "+%Y-%m-%d %H:%M:%S")
if [ $result_code -ne 0 ];then
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxx' -H 'Content-Type: application/json' -d "{'msgtype': 'text','text': {'content':'监控告警!!!\n网络连通性告警 \n源ip: 10.161.54.32\n目标ip: $ip(windows) 3389\n无法正常访问,请及时查看!\n访问时间: $time \n'},'at': {'isAtAll':true}}"
fi
done
nc=`nc -z -w 5 10.160.93.16 9092`
result_code2=`echo $?`
echo $result_code2
if [ $result_code2 -ne 0 ];then
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxx' -H 'Content-Type: application/json' -d "{'msgtype': 'text','text': {'content':'监控告警!!!\n源IP地址: 10.161.54.32\nhttp地址: https://10.160.93.16:9200(es访问url) \n无法正常访问,请及时查看!\n访问时间: $time \n'},'at': {'isAtAll':true}}"
fi
|