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
相关推荐
vortex56 分钟前
Linux日志轮转管理:logrotate 完全指南
linux·运维·服务器
ggaofeng25 分钟前
如何通过uboot加载硬盘
linux·qemu·uboot
尔染君子30 分钟前
嵌入式Linux驱动开发(按键驱动)
linux·驱动开发
kebidaixu43 分钟前
BCU 平台 Modbus 主机功能开发:液冷机组 & 消防传感器
linux
vsropy1 小时前
安装虚拟机VMware
linux·windows
Jason_chen1 小时前
Linux 3.0 串口机制深度解析:传统8250驱动与基础RS-232/485支持
linux·前端
Jason_chen1 小时前
Linux 5.10 串口机制深度解析:serial_core重构与RS-485自动方向控制革命
linux
无敌的牛1 小时前
自省。。。。
linux
lqjun08271 小时前
Linux 下 Hermes Agent 代理配置不生效问题的解决
linux·服务器
Gary Studio1 小时前
复杂 SoC(RK3568)PCB 布局的五步
android·linux·硬件