8
n8n 中文网amn8n.com

网站与API健康监控系统及HTTP状态验证

中级

这是一个DevOps领域的自动化工作流,包含 13 个节点。主要使用 If, Set, Code, Webhook, HttpRequest 等节点。 网站与API健康监控系统及HTTP状态验证

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
  • 可能需要目标 API 的认证凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "6d51f49bab6b0dffc9b87e9dabbeb0e9b71c14bdaab2b96ec32b663d472dacc3"
  },
  "nodes": [
    {
      "id": "621e8816-888e-47a1-8bf4-314e68b92dff",
      "name": "触发器 (Webhook)",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -656,
        544
      ],
      "webhookId": "a92b931c-6fd7-4115-be32-40ee0ac9de1b",
      "parameters": {
        "path": "website-check",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "29d1a8bf-5967-4738-9eee-0cf00d5587d1",
      "name": "设置默认值",
      "type": "n8n-nodes-base.set",
      "position": [
        -432,
        544
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "0782f86f-5c5c-408a-995e-93f5e67c8ca5",
      "name": "HTTP (Ping)",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -208,
        544
      ],
      "parameters": {
        "url": "={{ $json.body.url }}",
        "options": {
          "timeout": "={{ $json.body.timeoutMs }}",
          "response": {}
        }
      },
      "typeVersion": 4
    },
    {
      "id": "1a6612e0-b71b-46b1-9a69-7dbde08244e9",
      "name": "IF 状态 < 期望值",
      "type": "n8n-nodes-base.if",
      "position": [
        32,
        544
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.statusCode }}",
              "value2": "={{ $json.body.expectStatusUnder }}",
              "operation": "smaller"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "7c1e04f6-7281-4fe7-8a7f-942cda1f53f5",
      "name": "健康检查 (自动)",
      "type": "n8n-nodes-base.code",
      "position": [
        272,
        480
      ],
      "parameters": {
        "jsCode": "// Always perform basic health check on JSON response\nconst body = $json.body.body;\nlet healthStatus = 'unknown';\nlet isHealthy = false;\n\n// Check common health check patterns\nif (body && typeof body === 'object') {\n  // Check for common health indicators\n  if (body.status === 'ok' || body.status === 'UP' || body.status === 'healthy') {\n    healthStatus = body.status;\n    isHealthy = true;\n  } else if (body.health === 'ok' || body.health === 'UP' || body.health === 'healthy') {\n    healthStatus = body.health;\n    isHealthy = true;\n  } else if (body.state === 'ok' || body.state === 'UP' || body.state === 'healthy') {\n    healthStatus = body.state;\n    isHealthy = true;\n  } else if (body.ok === true) {\n    healthStatus = 'ok';\n    isHealthy = true;\n  } else {\n    // If no clear health indicator, assume healthy if we got a valid JSON response\n    healthStatus = 'response_received';\n    isHealthy = true;\n  }\n} else {\n  // No JSON body or invalid JSON\n  healthStatus = 'no_json';\n  isHealthy = false;\n}\n\nreturn {\n  healthCheck: 'performed',\n  healthStatus: healthStatus,\n  pass: isHealthy,\n  responseBody: body\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "e1811eac-a0d4-4320-a961-b8114f82620b",
      "name": "IF 健康检查通过",
      "type": "n8n-nodes-base.if",
      "position": [
        496,
        480
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.pass }}",
              "operation": "true"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "dd5c7ed7-c83c-47b9-80ad-c538b6c52a56",
      "name": "Ping → 正常",
      "type": "n8n-nodes-base.code",
      "position": [
        720,
        432
      ],
      "parameters": {
        "jsCode": "return {\n  ok: true,\n  detail: 'Status and Health OK',\n  statusCode: $json.body.statusCode,\n  healthStatus: $json.body.healthStatus,\n  timestamp: new Date().toISOString()\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "704775dd-5de5-4e86-ba38-2ba4b3b472aa",
      "name": "Ping → 失败 (健康)",
      "type": "n8n-nodes-base.code",
      "position": [
        736,
        656
      ],
      "parameters": {
        "jsCode": "return {\n  ok: false,\n  error: 'Health check failed',\n  statusCode: $json.body.statusCode,\n  healthStatus: $json.body.healthStatus,\n  responseBody: $json.body.responseBody,\n  timestamp: new Date().toISOString()\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "4876e4a1-9b45-4c43-93b1-f4ddb8fd3039",
      "name": "Ping → 失败 (状态)",
      "type": "n8n-nodes-base.code",
      "position": [
        272,
        656
      ],
      "parameters": {
        "jsCode": "return {\n  ok: false,\n  error: 'HTTP status not acceptable',\n  statusCode: $json.body.statusCode,\n  timestamp: new Date().toISOString()\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "d38463bd-cfc6-4fb4-bd88-feb5c59d354f",
      "name": "响应 (JSON)",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        944,
        544
      ],
      "parameters": {
        "options": {
          "responseCode": 200
        },
        "respondWith": "json",
        "responseBody": "={{ $json }}"
      },
      "typeVersion": 1
    },
    {
      "id": "5fda4dc1-7b89-49d6-acb6-9dfec7ff7f70",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -272,
        336
      ],
      "parameters": {
        "width": 272,
        "height": 208,
        "content": "## 从这里开始"
      },
      "typeVersion": 1
    },
    {
      "id": "8a903893-2a7c-4ea4-b9ae-9f84ad812899",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        192,
        240
      ],
      "parameters": {
        "width": 256,
        "height": 224,
        "content": "## 健康验证"
      },
      "typeVersion": 1
    },
    {
      "id": "658a6a7f-16d6-4a62-ad9d-6c94eae0e07d",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        240
      ],
      "parameters": {
        "width": 256,
        "height": 192,
        "content": "## 响应路径"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "HTTP (Ping)": {
      "main": [
        [
          {
            "node": "IF Status < expect",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ping → OK": {
      "main": [
        [
          {
            "node": "Respond (JSON)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Defaults": {
      "main": [
        [
          {
            "node": "HTTP (Ping)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Health passes": {
      "main": [
        [
          {
            "node": "Ping → OK",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Ping → FAIL (Health)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trigger (Webhook)": {
      "main": [
        [
          {
            "node": "Set Defaults",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Status < expect": {
      "main": [
        [
          {
            "node": "Health Check (Auto)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Ping → FAIL (Status)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Health Check (Auto)": {
      "main": [
        [
          {
            "node": "IF Health passes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ping → FAIL (Health)": {
      "main": [
        [
          {
            "node": "Respond (JSON)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ping → FAIL (Status)": {
      "main": [
        [
          {
            "node": "Respond (JSON)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 开发运维

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量13
分类1
节点类型7
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Ibrahim Emre POLAT

Ibrahim Emre POLAT

@epolat

Senior Software Developer specializing in automation and integrations with n8n. Experienced in .NET, PostgreSQL, and cloud-native systems. Continuously learning new tools and technologies to deliver efficient, scalable workflows.

外部链接
在 n8n.io 查看

分享此工作流