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

相关推荐
xyy_4963 小时前
wsl-localhost
linux
linux修理工3 小时前
linux禁用root登录
linux
小王C语言4 小时前
xshell免密登录
linux·运维·服务器
请输入蚊子5 小时前
《操作系统真象还原》 第十一章 用户进程
linux·操作系统·操作系统真像还原
Qt程序员5 小时前
网络 I/O 面试必考点:从多进程多线程到异步 I/O 与多路复用
linux·网络编程·多线程·epoll·网络io·阻塞io·io_uring
model20056 小时前
虚拟环境安装yolo26
linux·运维·服务器
Cat_Rocky6 小时前
keepalived简单配置
linux·运维·服务器
开开心心就好6 小时前
一键扫描电脑重复文件的实用工具
linux·运维·服务器·随机森林·智能手机·excel·启发式算法
charlie1145141916 小时前
AwesomeQt:最小的Qt6系列迷你版本教程发布!
linux·c++·qt·c
嵌入式×边缘AI:打怪升级日志7 小时前
Tina SDK Linux Kernel 基本使用(实战篇:为开发板添加用户按键驱动支持)
linux·运维·服务器