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
相关推荐
ltl1 小时前
纠删码原理与存储效率
linux
ltl1 小时前
MinIO 原理和架构:机制、AGPL 与上游 archived 风险
linux
林浩杨_1 小时前
linux搜索命令(grep+sed)
linux·命令模式
W.W.H.2 小时前
将驱动编译成模块并安装到rootfs
linux·物联网·wifi·rootfs·驱动
ltl2 小时前
io_uring 在生产环境翻车实录:内核 bug、资源泄漏和你不知道的限制
linux
ltl2 小时前
内存分配器擂台:jemalloc vs tcmalloc vs mimalloc vs glibc
linux
ltl2 小时前
Zero Copy 的肮脏秘密:在什么时候反而更慢
linux
数据知道2 小时前
宽字节注入、二次注入、堆叠注入:高级 SQLi 专题
linux·运维·安全·网络安全
童槿顏丶3 小时前
GitLab部署到Linux
linux·运维·gitlab
空堂与归6 小时前
Linux 命令实战:AI 开发者必备的终端操作手册
linux