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

相关推荐
DN金猿12 小时前
使用ubuntu安装nginx时报错
linux·nginx·ubuntu
小赵还有头发12 小时前
安装Ceres与glog
linux·学习·无人机·ceres·glog
负二代0.013 小时前
Linux下的网络管理
linux·网络
s_daqing13 小时前
ubuntu(arm)安装redis
linux·redis·ubuntu
林鸿群13 小时前
ubuntu 26.04 安装mysql-server
linux·mysql·ubuntu
betazhou13 小时前
rsync使用案例分析
linux·运维·服务器·rsync·同步数据
安静的技术开发者14 小时前
Linux Ubuntu学习笔记
linux·ubuntu
geshifei14 小时前
Sched ext回调1——init_task (linux 6.15.7)
linux·ebpf
晚风予卿云月15 小时前
【Linux】自动化构建—make/Makefile
linux·自动化·make/makefile
脑子进水养啥鱼?15 小时前
Linux find 命令
linux·运维