{
  "name": "Clawdbot 健康監控 (自動修復)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 5
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "每 5 分鐘檢查",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [250, 300]
    },
    {
      "parameters": {
        "command": "ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ollama@192.168.0.188 'pgrep -f \"clawdbot gateway\" && clawdbot gateway probe 2>&1 | grep -q \"RPC: ok\" && echo \"HEALTHY\" || echo \"UNHEALTHY\"'"
      },
      "id": "check-health",
      "name": "檢查 Clawdbot 狀態",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [470, 300]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true
          },
          "conditions": [
            {
              "id": "is-healthy",
              "leftValue": "={{ $json.stdout }}",
              "rightValue": "HEALTHY",
              "operator": {
                "type": "string",
                "operation": "contains"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "if-healthy",
      "name": "是否健康?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [690, 300]
    },
    {
      "parameters": {},
      "id": "healthy-noop",
      "name": "正常 (不做任何事)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [910, 200]
    },
    {
      "parameters": {
        "command": "ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ollama@192.168.0.188 'pkill -f \"clawdbot gateway\" 2>/dev/null; sleep 3; systemctl --user restart clawdbot-gateway.service 2>/dev/null || (setsid clawdbot gateway </dev/null >/dev/null 2>&1 &); sleep 5; pgrep -f \"clawdbot gateway\" && echo \"RESTART_SUCCESS\" || echo \"RESTART_FAILED\"'"
      },
      "id": "restart-gateway",
      "name": "重啟 Gateway",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [910, 400]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true
          },
          "conditions": [
            {
              "id": "restart-success",
              "leftValue": "={{ $json.stdout }}",
              "rightValue": "RESTART_SUCCESS",
              "operator": {
                "type": "string",
                "operation": "contains"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "if-restart-success",
      "name": "重啟成功?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1130, 400]
    },
    {
      "parameters": {
        "chatId": "5619078117",
        "text": "🔄 <b>Clawdbot 自動修復成功</b>\n\nGateway 曾經停止回應，已自動重啟。\n\n⏰ 時間: {{ $now.format('yyyy-MM-dd HH:mm:ss') }}\n🖥️ 主機: 192.168.0.188",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "id": "notify-success",
      "name": "通知修復成功",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1350, 300],
      "credentials": {
        "telegramApi": {
          "id": "telegram-bot",
          "name": "WOOO Telegram Bot"
        }
      }
    },
    {
      "parameters": {
        "chatId": "5619078117",
        "text": "🚨 <b>Clawdbot 修復失敗</b>\n\nGateway 無法自動重啟，需要手動檢查！\n\n⏰ 時間: {{ $now.format('yyyy-MM-dd HH:mm:ss') }}\n🖥️ 主機: 192.168.0.188\n\n🔧 手動修復:\n<code>ssh ollama@192.168.0.188 \"clawdbot gateway\"</code>",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "id": "notify-failure",
      "name": "通知修復失敗",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1350, 500],
      "credentials": {
        "telegramApi": {
          "id": "telegram-bot",
          "name": "WOOO Telegram Bot"
        }
      }
    }
  ],
  "connections": {
    "每 5 分鐘檢查": {
      "main": [
        [
          {
            "node": "檢查 Clawdbot 狀態",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "檢查 Clawdbot 狀態": {
      "main": [
        [
          {
            "node": "是否健康?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "是否健康?": {
      "main": [
        [
          {
            "node": "正常 (不做任何事)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "重啟 Gateway",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "重啟 Gateway": {
      "main": [
        [
          {
            "node": "重啟成功?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "重啟成功?": {
      "main": [
        [
          {
            "node": "通知修復成功",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "通知修復失敗",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "監控",
      "id": "monitoring"
    },
    {
      "name": "Clawdbot",
      "id": "clawdbot"
    }
  ],
  "triggerCount": 1,
  "meta": {
    "instanceId": "momo-n8n"
  }
}
