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
相关推荐
去码头整点薯条983 小时前
NFS服务器
linux·运维·服务器
蜗牛一步步往上爬4 小时前
第一章 初识Linux shell
linux·运维·服务器
拂拉氏4 小时前
【知识讲解】 Linux线程中锁的认识
linux·互斥锁
无足鸟ICT4 小时前
【RHCA+】系统调优配置文件
linux·服务器
时间的拾荒人6 小时前
Shell 脚本核心语法详解(下篇):流程控制、函数、重定向与文件包含
linux·运维·服务器·bash·脚本编程
蜗牛一步步往上爬7 小时前
第四章 更多的bash shell命令
linux·开发语言·bash
进击的荆棘8 小时前
Linux网络——网络基础概念
linux·运维·服务器·网络
.YM.Z8 小时前
从操作系统进入Linux进程概念及进程状态
linux·运维·服务器
忆挽篱笙歌14 小时前
通信连接,匿名管道
linux·服务器·网络
无足鸟ICT17 小时前
【RHCA+】安装Tuned
linux·服务器·centos