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 ```

相关推荐
尹蓝锐1 小时前
在学校Linux服务器上配置go语言环境
linux·运维·服务器
用户31187945592182 小时前
Fedora 38 安装 perl-JSON RPM 包步骤(含依赖问题解决及附安装包)
linux
ARTHUR-SYS3 小时前
基于Kali linux 安装pyenv及简单使用方法及碰到的问题
linux·运维·chrome
南山鹤1663 小时前
中型规模生产架构部署详细步骤
linux
IvanCodes3 小时前
十六、Linux网络基础理论 - OSI模型、TCP/IP协议与IP地址详解
linux·网络·tcp/ip
shylyly_3 小时前
Linux-> TCP 编程2
linux·服务器·网络·tcp/ip·松耦合·command程序
upgrador3 小时前
操作系统命令:Linux与Shell(Operating System & Command Line, OS/CLI)目录导航、文件操作与日志查看命令实践
linux·ubuntu·centos
夜月yeyue4 小时前
多级流水线与指令预测
linux·网络·stm32·单片机·嵌入式硬件
IvanCodes4 小时前
十五、深入理解 SELinux
linux·运维·服务器
云博客-资源宝4 小时前
【防火墙源码】WordPress防火墙插件1.0测试版
linux·服务器·数据库