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

相关推荐
CCPC不拿奖不改名5 小时前
虚拟机基础:在VMware WorkStation上安装Linux为容器化部署打基础
linux·运维·服务器·人工智能·milvus·知识库搭建·容器化部署
一只自律的鸡7 小时前
【Linux系统编程】文件IO 函数篇
linux·linux系统编程
dinga198510268 小时前
linux上redis升级
linux·运维·redis
hzc09876543219 小时前
Linux系统下安装配置 Nginx 超详细图文教程_linux安装nginx
linux·服务器·nginx
RisunJan9 小时前
Linux命令-ltrace(用来跟踪进程调用库函数的情况)
linux·运维·服务器
阿乐艾官9 小时前
【 LVM 创建逻辑卷】
linux
予枫的编程笔记9 小时前
【Linux高级篇】搞定文件句柄+TIME_WAIT,Linux内核初步调优实操指南
linux·linux运维·ulimit·time_wait·sysctl.conf·内核调优·服务器优化
c***03239 小时前
linux centos8 安装redis 卸载redis
linux·运维·redis
柏木乃一10 小时前
Linux进程信号(2):信号产生part2
linux·运维·服务器·c++·信号处理·信号·异常
小义_11 小时前
【RH134知识点问答题】第13章 运行容器
linux·云原生