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

相关推荐
lwx9148526 小时前
Linux-特殊权限SUID,SGID,SBIT
linux·运维·服务器
皮卡狮7 小时前
Linux权限的概念
linux
炘爚8 小时前
深入解析printf缓冲区与fork进程复制机制
linux·运维·算法
小义_8 小时前
随笔 3(Linux)
linux·运维·服务器·云原生·红帽
cccccc语言我来了9 小时前
Linux(10)进程概念
linux·运维·服务器
伐尘9 小时前
【linux】查看空间(内存、磁盘、文件目录、分区)的几个命令
linux·运维·网络
Deitymoon9 小时前
linux——PV操作
linux
原来是猿10 小时前
Linux进程信号详解(二):信号产生
linux·运维·服务器
Bert.Cai10 小时前
Linux cd命令详解
linux·运维
扑火的小飞蛾11 小时前
Kali Linux 安装 OpenClaw 详细教程
linux·运维·服务器