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

相关推荐
KL's pig/猪头/爱心/猪头6 分钟前
写一个rv1106的led驱动1-整体架构
linux·驱动开发
叁金Coder8 分钟前
【CentOS-Stream-9 配置网卡信息】
linux·运维·centos
赵一舟14 分钟前
linux下的磁盘清理
linux·运维·服务器
会飞的小新23 分钟前
Shell 脚本中的信号与 trap:从 Ctrl+C 到优雅退出
linux·开发语言
Frdbio28 分钟前
环腺苷酸(cAMP)ELISA检测试剂盒
linux·人工智能·python
生产队队长33 分钟前
Linux:awk进行行列转换操作
android·linux·运维
linweidong44 分钟前
在Ubuntu新版本安装gcc4.8等老版本环境
linux·运维·ubuntu
石像鬼₧魂石1 小时前
80 端口(Web 服务)渗透测试完整总结(含踩坑 + 绕过 + 实战流程)
linux·运维·服务器·前端·网络·阿里云
米高梅狮子1 小时前
11. Linux 防火墙管理
linux·运维·服务器
若风的雨1 小时前
RC-EP 和 Switch-EP 的复位链路建立过程区别
linux