{
  "name": "磁碟空間監控 (含自動清理)",
  "nodes": [
    {
      "parameters": {
        "rule": { "interval": [{ "field": "hours", "hoursInterval": 1 }] }
      },
      "id": "schedule-trigger",
      "name": "每小時執行",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [0, 0]
    },
    {
      "parameters": {
        "url": "http://192.168.0.110:5001/metrics",
        "options": { "timeout": 10000 }
      },
      "id": "http-metrics",
      "name": "取得系統指標",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [220, 0]
    },
    {
      "parameters": {
        "jsCode": "const metrics = $input.first().json.data || $input.first().json;\nconst text = typeof metrics === 'string' ? metrics : '';\n\nlet usedBytes = 0, totalBytes = 0;\nconst lines = text.split('\\n');\nfor (const line of lines) {\n  if (line.startsWith('momo_disk_used_bytes')) usedBytes = parseInt(line.split(' ')[1]) || 0;\n  if (line.startsWith('momo_disk_total_bytes')) totalBytes = parseInt(line.split(' ')[1]) || 0;\n}\n\nconst usagePercent = totalBytes > 0 ? ((usedBytes / totalBytes) * 100).toFixed(1) : 0;\nconst freeGB = ((totalBytes - usedBytes) / 1024 / 1024 / 1024).toFixed(1);\nconst totalGB = (totalBytes / 1024 / 1024 / 1024).toFixed(1);\nconst level = parseFloat(usagePercent) > 90 ? 'critical' : parseFloat(usagePercent) > 85 ? 'warning' : 'normal';\n\nreturn [{ json: { usagePercent: parseFloat(usagePercent), freeGB, totalGB, level, needClean: level !== 'normal' } }];"
      },
      "id": "code-parse",
      "name": "解析使用率",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [{ "id": "c1", "leftValue": "={{ $json.needClean }}", "rightValue": true, "operator": { "type": "boolean", "operation": "equals" } }],
          "combinator": "and"
        }
      },
      "id": "if-alert",
      "name": "需要清理?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [660, 0]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst emoji = data.level === 'critical' ? '🔴' : '🟠';\nconst title = data.level === 'critical' ? '嚴重不足' : '警告';\n\nconst text = emoji + ' *磁碟空間' + title + '*\\n\\n' +\n  '📊 使用率: ' + data.usagePercent + '%\\n' +\n  '💾 剩餘: ' + data.freeGB + ' GB / ' + data.totalGB + ' GB\\n\\n' +\n  '🔧 正在執行自動清理...';\n\nreturn [{ json: { ...data, telegramBody: { chat_id: '5619078117', text: text, parse_mode: 'Markdown' } } }];"
      },
      "id": "code-alert",
      "name": "組合告警訊息",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [880, 100]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.telegram.org/bot8075645931:AAH-EGKMo8ZC4QJs-Nc1_0s92xHrGdQvdpg/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.telegramBody) }}"
      },
      "id": "telegram-alert",
      "name": "告警通知",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [1100, 100]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://192.168.0.110:5001/api/system/cleanup",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ actions: ['docker_prune', 'log_rotate', 'temp_clean'], level: $json.level }) }}",
        "options": { "timeout": 120000 }
      },
      "id": "http-cleanup",
      "name": "執行自動清理",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [1320, 100]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nlet text = '';\nif (data.success) {\n  const results = Array.isArray(data.results) ? data.results.join('\\n') : (data.results || '清理完成');\n  text = '✅ *自動清理完成*\\n\\n🧹 清理結果:\\n' + results;\n} else {\n  text = '❌ *自動清理失敗*\\n\\n錯誤: ' + (data.error || '未知錯誤');\n}\n\nreturn [{ json: { telegramBody: { chat_id: '5619078117', text: text, parse_mode: 'Markdown' } } }];"
      },
      "id": "code-result",
      "name": "組合結果訊息",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1540, 100]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.telegram.org/bot8075645931:AAH-EGKMo8ZC4QJs-Nc1_0s92xHrGdQvdpg/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.telegramBody) }}"
      },
      "id": "telegram-result",
      "name": "清理結果通知",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [1760, 100]
    },
    {
      "parameters": {},
      "id": "no-op",
      "name": "空間正常",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [880, -100]
    }
  ],
  "connections": {
    "每小時執行": { "main": [[{ "node": "取得系統指標", "type": "main", "index": 0 }]] },
    "取得系統指標": { "main": [[{ "node": "解析使用率", "type": "main", "index": 0 }]] },
    "解析使用率": { "main": [[{ "node": "需要清理?", "type": "main", "index": 0 }]] },
    "需要清理?": { "main": [[{ "node": "組合告警訊息", "type": "main", "index": 0 }], [{ "node": "空間正常", "type": "main", "index": 0 }]] },
    "組合告警訊息": { "main": [[{ "node": "告警通知", "type": "main", "index": 0 }]] },
    "告警通知": { "main": [[{ "node": "執行自動清理", "type": "main", "index": 0 }]] },
    "執行自動清理": { "main": [[{ "node": "組合結果訊息", "type": "main", "index": 0 }]] },
    "組合結果訊息": { "main": [[{ "node": "清理結果通知", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" }
}
