Shell监控主机连通性或者http服务可访问性,发现异常消息到钉钉群。

本文介绍了如何使用Shell脚本监控主机连通性、HTTP服务及Windows服务器,并通过钉钉自定义机器人发送告警信息。首先创建钉钉群与自定义机器人,然后编写监控脚本,当检测到异常时,利用curl发送消息到钉钉群。最后,通过Linux crontab设置定时任务,每15分钟执行一次监控。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:需求

使用Shell脚本监控主机连通性(通过ping的方式判断主机是否还在线)或http服务的可访问性,如果发现异常把异常消息发送到钉钉群。

(1)首先在钉钉中添加webhook自定义机器人
(2)编写sh脚本,测试发送内容 。
(3)编写触发日志报警的脚本
(4)最后一步,利用linux系统本身的 定时任务,执行这个触发日志报警的任务 crontab -e,我们暂时用每15分钟执行一次
(5)如果服务宕掉或者有异常发生,将会触发 报警,

二:自定义钉钉机器人

1.建一个钉钉群

2.创建自定义机器人

建群成功后,点击右上角的“群设置”按钮:
在这里插入图片描述
点击“智能群助手” --> 点击“添加机器人”
在这里插入图片描述点击添加自定义机器人
在这里插入图片描述
这里的安全设置选项是必须选的,可以选一个或多个
勾选“自定义关键词”,设置关键词。
在发送的消息里,必须带有设置的关键词,才能发送成功
在这里插入图片描述

其它安全设置参考:https://blog.youkuaiyun.com/HeyShHeyou/article/details/104277083
安全设置官网参考: https://open.dingtalk.com/document/robots/customize-robot-security-settings

Webhook地址复制保存好,不要公布在外部网站上,泄露后有安全风险。
在这里插入图片描述

3.shell监控主机连通信脚本:

[root@mail data]# cat ping.sh 
#!/bin/bash

#存放ip文件的位置
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
			#echo "successful"
			break
		else
			let fail_count++
		fi
		#echo $fail_count
	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]# cat http_health.sh 
#!/bin/bash
#返回状态码不是200,都视为异常
#http_url.txt存放url地址
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"`
	#time=$(date "+%Y-%m-%d %H:%M:%S")
	for ((i=0;i<=3;i++));do
		if [ $status_code -ge 200 ];then
			#echo "访问成功!$url"
			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]# cat nc.sh 
#!/bin/bash

#28 54是windows机器,通过nc探测端口的方式来判断他是否存活
#nc_ip.txt存放ip地址
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 $?`
#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

[root@mail data]# cat nc.sh 
#!/bin/bash

#nc_ip.txt存放ip地址
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

侯侯Hou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值