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

相关推荐
tan77º1 小时前
【Linux网络编程】Socket - UDP
linux·服务器·网络·c++·udp
czhc11400756631 小时前
Linux 76 rsync
linux·运维·python
蓝易云2 小时前
Qt框架中connect()方法的ConnectionType参数使用说明 点击改变文章字体大小
linux·前端·后端
花落已飘3 小时前
多线程 vs 异步
linux·网络·系统架构
PanZonghui3 小时前
Centos项目部署之Nginx部署项目
linux·nginx
码出钞能力4 小时前
linux内核模块的查看
linux·运维·服务器
星辰云-4 小时前
# Linux Centos系统硬盘分区扩容
linux·运维·centos·磁盘扩容
聽雨2375 小时前
02每日简报20250704
linux·科技·金融·生活·社交电子·娱乐·媒体
Maki Winster5 小时前
Peek-Ubuntu上Gif录制工具-24.04LTS可装
linux·ubuntu·peek
Maki Winster6 小时前
在 Ubuntu 下配置 oh-my-posh —— 普通用户 + root 各自使用独立主题(共享可执行)
linux·运维·ubuntu