钉钉告警没有额度了,替代方案使用企业微信或者是飞书,以下脚本是飞书为例
监控ping也就是活动主机
#!/bin/bash
# IP Ping 监控脚本
date=$(date "+%Y-%m-%d %H:%M:%S")
# 根据实际情况修改飞书 Webhook 地址
webhook="https://open.feishu.cn/open-apis/bot/v2/hook/xxx"
# 发送消息到飞书
send_message() {
local message="$1"
curl -s "$webhook" \
-H "Content-Type: application/json" \
-d '{
"msg_type": "text",
"content": {
"text": "'"$message idc"'"
}
}'
}
# 从 ip_list.txt 文件中读取 IP 地址列表
ip_list=$(cat ip_list.txt)
# 遍历 IP 列表并进行 Ping 测试
for ip in $ip_list
do
ping -c 1 "$ip" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "IP $ip 是存活的 $date" >/dev/null 2>&1
else
message="告警IP: $ip\n告警时间: $date\n请及时处理告警信息!!!!!"
send_message "$message"
fi
done
以下是监控端口的脚本
#!/bin/bash
#端口预警脚本
date=$(date "+%Y-%m-%d %H:%M:%S")
#根据实际情况修改webhook飞书地址
webhook="https://open.feishu.cn/open-apis/bot/v2/hook/cxxx"
send_message() {
local message="$1"
curl -s "$webhook" \
-H "Content-Type: application/json" \
-d '{
"msg_type": "text",
"content": {
"text": "'"$message idc"'"
}
}'
}
# 需要监控的主机列表及其对应服务和端口
declare -A SERVICES
SERVICES=(
["192.168.67.15"]="MySQL-Master:3306"
["192.168.199.102"]="MySQL-Slave:3306"
["192.168.67.12"]="MySQL-Master:3306"
["192.168.199.101"]="MySQL-Slave:3306"
["192.168.199.103"]="MySQL-tesdb:3306"
["192.168.1.10"]="MySQL-Archive:3306"
["192.168.1.166"]="SQLServer:1433"
["192.168.4.7"]="Oracle:1521"
["192.168.3.7"]="Oracle:1521"
)
# 当前日期
date=$(date +"%Y-%m-%d %H:%M:%S")
for host in "${!SERVICES[@]}"
do
# 获取服务名称
service_name=${SERVICES[$host]%%:*} # 提取服务名称
port=${SERVICES[$host]##*:} # 提取端口
# 检查端口
nc -zv "$host" "$port" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "服务 $service_name 在主机 $host 的端口 $port 是存活的 $date"
else
message="告警服务: $service_name\n告警IP: $host\n告警端口: $port\n告警时间: $date\n请及时处理告警信息!!!!!"
send_message "$message"
fi
done