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

相关推荐
Y淑滢潇潇1 天前
RHCE 防火墙实验
linux·运维·rhce
wadesir1 天前
当前位置:首页 > 服务器技术 > 正文Linux网络HSRP协议(实现路由器热备份与高可用性的实用指南)
linux·服务器·网络
稻谷君W1 天前
Ubuntu 远程访问 Win11 WSL2 并固定访问教程
linux·运维·ubuntu
wdfk_prog1 天前
[Linux]学习笔记系列 -- [kernel]workqueue
linux·笔记·学习
wdfk_prog1 天前
[Linux]学习笔记系列 -- [kernel]usermode_helper
linux·笔记·学习
weixin_436525071 天前
使用 idea 命令行构建 Docker 镜像并部署到云服务器
linux·docker·github
z***94841 天前
Linux下安装Nginx服务及systemctl方式管理nginx详情
linux·运维·nginx
凉晓风1 天前
Linux上TCP通信异常排查工具命令
linux·运维·tcp/ip
Xの哲學1 天前
Linux 分区表深度技术剖析
linux·网络·算法·架构·边缘计算
码龄3年 审核中1 天前
Linux record 03
java·linux·运维