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

相关推荐
_OP_CHEN8 分钟前
【Git原理与使用】(一)告别文件混乱!Git 初识:从版本灾难到高效管理的终极方案
linux·运维·git·github·运维开发·版本控制·企业级组件
装不满的克莱因瓶10 分钟前
【Java架构 搭建环境篇三】Linux安装Git详细教程
java·linux·运维·服务器·git·架构·centos
博语小屋18 分钟前
线程同步与条件变量
linux·jvm·数据结构·c++
至善迎风28 分钟前
Linux 服务器安全防护工具完全指南
linux·服务器·安全·防火墙
MC皮蛋侠客29 分钟前
Linux安装go及环境配置教程
linux·运维·golang
满天点点星辰40 分钟前
Linux命令大全-find命令
linux·运维·服务器
H_z_q24011 小时前
RHCE的条件测试
linux·运维·服务器
新青年.1 小时前
【Ubuntu】Ubuntu下解决Chrome不能输入中文
linux·chrome·ubuntu
风华同学2 小时前
【环境搭建篇】ARM+Linux环境搭建
linux·运维·arm开发
温柔如酒2 小时前
【linux调试】使用 sysrq 快照内核状态堆栈
linux·运维·服务器