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

相关推荐
宁波阿成10 分钟前
在ubuntu22.04源码级安装sub2api
linux·运维·ubuntu·ai·api·token·中转站
charlie11451419140 分钟前
嵌入式Linux驱动开发(7) 从虚拟设备到真实硬件 —— LED驱动硬件基础
linux·开发语言·驱动开发·内核·c
Mortalbreeze1 小时前
软件包管理器yum和编辑器vim详解 —— 附带vim配置链接
linux·服务器
李日灐1 小时前
< 7 > Linux 开发工具:git 版本控制器 和 cgdb/gdb 调试器
linux·运维·服务器·开发语言·git·调试器·gdb/cgdb
青木9602 小时前
前后端开发调试运行技巧
linux·服务器·前端·后端·npm·uv
c++之路2 小时前
C++ 模板
linux·开发语言·c++
云动课堂2 小时前
【运维实战】MySQL 8.0 数据库 · 一键自动化部署方案 (适配银河麒麟 V10 / 龙蜥 8 / Rocky Linux 8 / CentOS 8)
linux·运维·数据库
cui_ruicheng2 小时前
Linux进程间通信(一):管道与IPC基础
linux·运维·服务器
Lumos_7772 小时前
Linux -- 互斥锁
linux
一叶龙洲2 小时前
Ubuntu开机无法用向日葵远程控制
linux·运维·ubuntu