Linux服务监控自动巡检脚本--推送钉钉告警

1. 编写服务监控自动巡检脚本

root@localhost \~# cat /opt/port_monitor_alarm_by_dingding.sh

bash 复制代码
#!/bin/bash

# 定义需要监控服务对应的端口列表
port_list="3308  1242  35690  65500  10410"

# 定义接收通知的钉钉机器人的 Webhook URL
webhook_url="https://oapi.dingtalk.com/robot/send?access_token=888888888888888888888888888888888888888888888888"

#自定义主机名称或IP
auto_hostname="8.8.8.8"

# 获取本机IP地址输出本机的ip地址
local_ip=$(hostname -I | awk '{print $1}')

# 遍历所有端口并检查其连接状态
port_monitor()
{
    for port in $port_list; do
        # if ! nc -z localhost $port; then
        if ! netstat  -antulp |  grep $port;then
            # 如果发现不正常连接,通过钉钉机器人发送通知消息
            curl -H "Content-Type: application/json" \
                -d '{
                    "msgtype": "text",
                    "text": {
                        "content": "【重要告警】服务器【'"$auto_hostname"'】服务监控: Port 【'"$port"'】is not active on '"$local_ip"' at '"$(date +"%Y-%m-%d %H:%M:%S")"'"
                    }
                }' $webhook_url
        fi
    done
}

port_monitor

2. 配置计划任务

root@localhost \~# cat /etc/crontab

bash 复制代码
*/1  * * * *   root   /bin/bash  /opt/port_monitor_by_dingding.sh  >/dev/null 2>&1
相关推荐
爱丶狸1 小时前
Grafana_Zabbix_ImageRenderer_部署与前端操作手册
linux·前端·zabbix·grafana·kylin
k4m7v2pz2 小时前
把 Bevy 0.14 游戏移植到 R36S 掌机:一场与“无窗口系统“的搏斗
linux·rust·bevy·r36s·rk3326·开源掌机
木子欢儿4 小时前
Intel 架构的 MacBook Pro 运行 Linux 发热量高解决
linux·运维·服务器
会编程的土豆7 小时前
Go 模板初识
linux·运维·服务器
regret~9 小时前
【记录】网络路由、转发与网卡隔离
linux·服务器·网络
aFakeProgramer9 小时前
Linux版本兼容问题及RTA-VRTE环境配置笔记
linux·笔记
Zhu7589 小时前
在Ubuntu24快速配置专用SFTP
linux·sftp
码农客栈9 小时前
Linux 内核顶层 Makefile 详解
linux
Embedded-Xin10 小时前
Rust学习——Cargo工具
linux·学习·架构·rust·嵌入式
weixin_4357761110 小时前
2026年长沙托育vi设计从风格定位到元素运用的设计方法
linux·运维·服务器·python