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

相关推荐
June`18 分钟前
深入解析网络层与数据链路层
linux·服务器·网络
老王熬夜敲代码23 分钟前
计算机网络--IP概念
linux·网络·笔记
Lynnxiaowen41 分钟前
今天我们继续学习devops内容基于Jenkins构建CICD环境
linux·运维·学习·jenkins·devops
用户61354114601643 分钟前
Linux 麒麟系统安装 gcc-7.3.0 rpm 包步骤
linux
小尧嵌入式44 分钟前
Linux网络介绍网络编程和数据库
linux·运维·服务器·网络·数据库·qt·php
LUCIFER1 小时前
[驱动之路(七)——Pinctrl子系统]学习总结,万字长篇,一文彻底搞懂Pinctrl子系统(含Pin Controller驱动框架解析)
linux·驱动开发
山川而川-R1 小时前
在香橙派5pro上的ubuntu22.04系统烧录镜像
linux·运维·服务器
Ancelin安心1 小时前
关于代理的一些网络知识复盘
linux·运维·网络·计算机网络·web安全·ubuntu·网络安全
chenyuhao20241 小时前
Linux系统编程:多线程互斥以及死锁问题
linux·运维·服务器·c++·后端
Norach2 小时前
Ubuntu升级opencv版本至4.9.0
linux·经验分享·opencv·yolo·ubuntu·dnn