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
相关推荐
月巴月巴白勺合鸟月半17 分钟前
在Linux下开发桌面程序
linux·运维·服务器
zh路西法17 分钟前
【tmux入门】终端分屏、SSH远程守护与一键启动脚本
linux·运维·ssh·bash
qq_1631357523 分钟前
Linux 【03-pwd命令超详细教程】
linux
学途路漫漫24 分钟前
Ubuntu 24.04 国内网络环境全面优化指南
linux·网络·ubuntu
c2385625 分钟前
GDB 进程概念详解(下篇)—— 多进程与进阶调试能力
linux·服务器·数据库
RisunJan27 分钟前
Linux命令-php(PHP语言的命令行接口)
linux·php
A_humble_scholar28 分钟前
Linux(八) 进程内存全景:环境变量、main 函数参数与虚拟地址空间全链路深度解析
linux·运维·服务器
longforus33 分钟前
linux上播放音乐的终极解决方案
linux·音频·折腾
xcLeigh35 分钟前
鸿蒙PC平台 Shotwell 照片管理器适配实战:从 Linux GNOME 到 鸿蒙PC 的 Electron 迁移
linux·electron·harmonyos·鸿蒙·shotwell·照片管理器
Lang-12101 小时前
CentOS Linux服务器完整迁移方案
linux·服务器·centos