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
相关推荐
古城小栈27 分钟前
Python 的主流Ai框架为什么优先适配 Linux 系统?
linux·人工智能·python
小懿互联集成平台2 小时前
金蝶云星空与钉钉OA审批对接-构建一体化财务付款管理体系
钉钉·金蝶云星空·数据对接·小懿互联·构建一体化财务付款
aFakeProgramer2 小时前
S-CORE Docker 环境
linux
error:(2 小时前
Ubuntu 22.04 GNOME远程桌面配置问题排查与解决全流程
linux·运维·ubuntu
wcy100862 小时前
为 CentOS 7.6 (7.6.1810) 配置阿里云 Vault 源
linux·阿里云·centos
江华森2 小时前
Linux 运维新手入门课
linux·运维·服务器
载数而行5202 小时前
Linux 9 服务管理(进程的一种)
linux
追梦的小菜2 小时前
linux 下磁盘挂载
linux
izcll2 小时前
ubuntu系统安装软件的方法
linux·运维·ubuntu
暮云星影14 小时前
全志linux开发屏幕适配(一)屏幕参数设置说明
linux·arm开发