Linux服务监控自动巡检脚本--推送钉钉告警

1. 编写服务监控自动巡检脚本

 [root@localhost ~]#    cat    /opt/port_monitor_alarm_by_dingding.sh

#!/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

*/1  * * * *   root   /bin/bash  /opt/port_monitor_by_dingding.sh  >/dev/null 2>&1

相关推荐

  1. Linux服务监控自动脚本--告警

    2024-07-09 16:58:09       31 阅读
  2. SSL证书到期自动脚本-告警

    2024-07-09 16:58:09       23 阅读
  3. 服务器脚本linux

    2024-07-09 16:58:09       34 阅读
  4. Python 自动打卡脚本

    2024-07-09 16:58:09       87 阅读
  5. Stream模式程序环境部署

    2024-07-09 16:58:09       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 16:58:09       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 16:58:09       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 16:58:09       42 阅读
  4. Python语言-面向对象

    2024-07-09 16:58:09       53 阅读

热门阅读

  1. stm32中断

    2024-07-09 16:58:09       23 阅读
  2. 使用引用 XML 文件来优化 EtherCAT ESI 文件的描述

    2024-07-09 16:58:09       27 阅读
  3. uni app for()在App上不运行

    2024-07-09 16:58:09       23 阅读
  4. OpenSNN推文:盛夏智慧之光:七月高校新闻聚焦

    2024-07-09 16:58:09       29 阅读
  5. 【计算机网络——1.2网络边缘】

    2024-07-09 16:58:09       30 阅读
  6. 音频demo:将PCM数据与g726数据的相互转换

    2024-07-09 16:58:09       24 阅读
  7. x-mind没有配置文件,可以自己创建文件修改内存

    2024-07-09 16:58:09       48 阅读
  8. Flink 窗口触发器(Trigger)(二)

    2024-07-09 16:58:09       21 阅读
  9. SpringCloud从配置中心读取git配置成功却没有效果

    2024-07-09 16:58:09       24 阅读
  10. 【python基础】—pip与conda的区别

    2024-07-09 16:58:09       30 阅读
  11. OSI模型

    2024-07-09 16:58:09       33 阅读
  12. 【Linux】GNU是什么

    2024-07-09 16:58:09       18 阅读
  13. tcp并发设计

    2024-07-09 16:58:09       15 阅读