{
  "name": "Tdarr GPU Monitor",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 5
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Every 5 Minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "command": "ssh -i /root/.ssh/n8n_to_claude -o BatchMode=yes -o ConnectTimeout=10 cal@10.10.0.226 'docker exec tdarr-node nvidia-smi --query-gpu=name --format=csv,noheader 2>&1'"
      },
      "id": "check-gpu",
      "name": "Check GPU Access",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [220, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "gpu-error-check",
              "leftValue": "={{ $json.stdout + $json.stderr }}",
              "rightValue": "NVML|CUDA_ERROR|Unknown Error|no CUDA-capable|Failed to initialize",
              "operator": {
                "type": "string",
                "operation": "regex"
              }
            }
          ],
          "combinator": "or"
        }
      },
      "id": "check-error",
      "name": "GPU Error Detected?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "command": "ssh -i /root/.ssh/n8n_to_claude -o BatchMode=yes -o ConnectTimeout=10 cal@10.10.0.226 'cd /home/cal/docker/tdarr && docker compose restart tdarr-node 2>&1'"
      },
      "id": "restart-container",
      "name": "Restart tdarr-node",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [660, -100]
    },
    {
      "parameters": {
        "command": "ssh -i /root/.ssh/n8n_to_claude -o BatchMode=yes -o ConnectTimeout=30 cal@10.10.0.226 'sleep 5 && docker exec tdarr-node nvidia-smi --query-gpu=name --format=csv,noheader 2>&1'"
      },
      "id": "verify-gpu",
      "name": "Verify GPU Restored",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [880, -100]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://discord.com/api/webhooks/1451783909409816763/O9PMDiNt6ZIWRf8HKocIZ_E4vMGV_lEwq50aAiZ9HVFR2UGwO6J1N9_wOm82p0MetIqT",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"embeds\": [{\n    \"title\": \"Tdarr GPU Recovery\",\n    \"description\": \"GPU access was lost and container was automatically restarted.\",\n    \"color\": {{ $('Verify GPU Restored').item.json.stdout.includes('GTX') || $('Verify GPU Restored').item.json.stdout.includes('NVIDIA') ? 3066993 : 15158332 }},\n    \"fields\": [\n      {\n        \"name\": \"Original Error\",\n        \"value\": \"```{{ $('Check GPU Access').item.json.stdout.substring(0, 200) }}{{ $('Check GPU Access').item.json.stderr.substring(0, 200) }}```\",\n        \"inline\": false\n      },\n      {\n        \"name\": \"Recovery Status\",\n        \"value\": \"{{ $('Verify GPU Restored').item.json.stdout.includes('GTX') || $('Verify GPU Restored').item.json.stdout.includes('NVIDIA') ? '✅ GPU access restored' : '❌ GPU still not accessible - manual intervention needed' }}\",\n        \"inline\": false\n      },\n      {\n        \"name\": \"Post-Restart GPU\",\n        \"value\": \"```{{ $('Verify GPU Restored').item.json.stdout.substring(0, 200) }}```\",\n        \"inline\": false\n      }\n    ],\n    \"timestamp\": \"{{ new Date().toISOString() }}\",\n    \"footer\": {\n      \"text\": \"Tdarr GPU Monitor | ubuntu-manticore\"\n    }\n  }]\n}",
        "options": {}
      },
      "id": "discord-notify",
      "name": "Discord Notification",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1100, -100]
    },
    {
      "parameters": {},
      "id": "no-action",
      "name": "GPU OK - No Action",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [660, 100]
    }
  ],
  "connections": {
    "Every 5 Minutes": {
      "main": [
        [
          {
            "node": "Check GPU Access",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check GPU Access": {
      "main": [
        [
          {
            "node": "GPU Error Detected?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GPU Error Detected?": {
      "main": [
        [
          {
            "node": "Restart tdarr-node",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "GPU OK - No Action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Restart tdarr-node": {
      "main": [
        [
          {
            "node": "Verify GPU Restored",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify GPU Restored": {
      "main": [
        [
          {
            "node": "Discord Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "homelab"
    },
    {
      "name": "monitoring"
    },
    {
      "name": "tdarr"
    }
  ],
  "triggerCount": 1,
  "pinData": {}
}
