• 自动监控网站可用性并发送通知的 Bash 脚本


    在现代的互联网世界中,网站的可用性对于业务的成功至关重要。如果您是一个网站管理员或负责监控网站运行状况的工程师,那么您知道及时发现并解决问题对于确保用户满意度至关重要。如何使用 Bash 脚本来监控多个网站的可用性,并在网站出现问题时自动发送通知

    背景

    在项目中,我们使用了 Rails 框架,Nginx 作为代理服务器,实现了前后端分离的架构,共有 6 个网站。为了及时发现这些网站的可用性问题,曾考虑使用 UptimeRobot,这是一个免费且简单易用的监控工具。然而,由于 UptimeRobot 的报警通知功能限制,只能通知注册账号的人,无法邀请团队成员,这让我们感到不便。因此,决定自己编写一个监控脚本

    基本功能

    监控脚本需要实现以下基本功能:

    1. 当网站出现故障时,发送通知。
    2. 当网站首次成功启动并处于正常运行状态时,发送通知。
    3. 在连续检查中,如果服务一直保持正常状态,不发送重复通知。
    #!/bin/bash
    
    # 定义要监视的网站列表
    websites=("https://dev.1.com" "https://dev.2.com" "https://dev.1.com")
    
    # 定义Feishu机器人的Webhook URL
    webhook_url="xxxxxxxxx"
    
    # 函数:发送消息到 Feishu 机器人
    send_message_to_feishu() {
      local website="$1"
      local status="$2"
    
      # 构建要发送的消息
       message="Monitor is $status: $website  监控时间: $(date "+%Y-%m-%d %H:%M:%S")"
      echo $message
      # 发送消息到 Feishu 机器人
      curl -X POST \
        $webhook_url \
        -H 'Content-Type: application/json' \
        -d '{
        "msg_type": "post",
        "content": {
            "post": {
                "zh_cn": {
                    "title": "Monitor - 服务告警",
                    "content": [
                        [
                            {
                                "tag": "text",
                                "text":  "'"$message"'"
                            },
                            {
                              "tag": "at",
                              "user_id": "all"
                            }
                        ]
                    ]
                }
            }
        }
    }'
    }
    
    
    # 文件夹用于存储状态文件
    status_folder="status_files"
    
    # 创建状态文件夹(如果不存在)
    mkdir -p "$status_folder"
    
    # 遍历网站列表并检查
    for website in "${websites[@]}"; do
      # 从网站URL中提取名称,用作状态文件名
      website_name=$(echo "$website" | sed 's/[^a-zA-Z0-9]/_/g')
      status_file="$status_folder/$website_name.status"
    
      status_code=$(curl -s -o /dev/null -w "%{http_code}" "$website")
      echo "status_code $status_code"
    
    if [[ ! "$status_code" =~ ^4[0-9][0-9] && "$status_code" != "000" ]]; then
        if [ ! -e "$status_file" ] || [ "$(cat "$status_file")" == "down" ]; then
          send_message_to_feishu "$website" "Up and Running"
        fi
        echo "up" > "$status_file"  # 更新状态为 "up"
      else
          send_message_to_feishu "$website" "Down"
          echo "down" > "$status_file"  # 更新状态为 "down"
      fi
    done
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    上述脚本是一个用于监控多个网站可用性的 Bash 脚本。它会定期检查这些网站的状态,并在网站出现问题时发送通知。脚本中使用了 Feishu 机器人 来发送通知,根据自己的需求选择其他通知方式。

    总结

    通过这个简单的 Bash 脚本,能够自动监控多个网站的可用性,并在必要时发送通知,确保我们的网站始终保持良好的运行状态。这种自动化的监控方式可以大大减轻管理员和工程师的负担,提高了网站的可用性和用户体验。

    希望这个示例对您有所帮助,可以根据您的实际需求进行修改和定制。如果您有任何问题或建议,欢迎在评论中分享!

  • 相关阅读:
    ctf工具之:mitmproxy实践测试
    使用brainconn工具绘制的大脑连接数据,比BrainNet更方便和灵活
    ubunut搭建aarch64 cuda交叉编译环境记录
    钉钉群内自定义机器人发送消息功能实现
    昨天阅读量900
    使用vmware ESXi安装和复制虚拟机
    Linux内核 -- ARM指定CPU运行逻辑之smp_call_function_single函数
    错误码设计思考
    什么台灯最好学生晚上用?开学适合学生用的护眼台灯推荐
    中国大陆已有IB学校243所
  • 原文地址:https://blog.csdn.net/noob_cherry/article/details/133753634