{ "name": "Grafana Alert → AI → Gotify v1.4", "nodes": [ { "parameters": { "httpMethod": "POST", "path": "grafana-alert", "options": {} }, "id": "a6e7dfb1-8e9b-4048-b028-4383c30a7e0d", "name": "Grafana Webhook", "type": "n8n-nodes-base.webhook", "typeVersion": 2, "position": [0, 0], "webhookId": "0492788e-db4e-4d15-9572-9d6c3efc80c3" }, { "parameters": { "jsCode": "// Parse Grafana alert payload into a clean summary for the LLM\nconst alerts = $input.first().json.body?.alerts || $input.first().json.alerts || [];\n\nconst parsed = alerts.map(a => ({\n status: a.status || 'unknown',\n alertName: a.labels?.alertname || 'Unnamed Alert',\n host: a.labels?.host || a.labels?.instance || 'unknown host',\n severity: a.labels?.severity || 'warning',\n summary: a.annotations?.summary || '',\n description: a.annotations?.description || '',\n value: a.valueString || '',\n startsAt: a.startsAt || '',\n endsAt: a.endsAt || ''\n}));\n\nconst alertText = parsed.map(a =>\n `[${a.status.toUpperCase()}] ${a.alertName} on ${a.host}\\nSeverity: ${a.severity}\\nSummary: ${a.summary}\\nDescription: ${a.description}\\nValue: ${a.value}\\nStarted: ${a.startsAt}`\n).join('\\n---\\n');\n\nreturn [{\n json: {\n alertText,\n alertCount: parsed.length,\n hasResolved: parsed.some(a => a.status === 'resolved'),\n hasFiring: parsed.some(a => a.status === 'firing'),\n parsed\n }\n}];" }, "id": "ea216058-69db-42cc-88d0-8f67e1b8951b", "name": "Parse Alert Payload", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [224, 0] }, { "parameters": { "jsCode": "const parsed = $('Parse Alert Payload').first().json;\nconst now = new Date();\nconst hour = now.getHours();\nconst fingerprint = (parsed.parsed || [])\n .map(a => `${a.alertName}:${a.host}`)\n .sort().join('|') || 'unknown';\nreturn [{ json: { ...parsed, fingerprint, isNightTime: hour >= 0 && hour < 7 } }];\n" }, "id": "df26d9ca-c72c-4dee-9327-14a0f5fcf3a0", "name": "Build Fingerprint", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [448, 0] }, { "parameters": { "operation": "executeQuery", "query": "={{ 'SELECT fingerprint, last_seen FROM grafana_alert_dedup WHERE fingerprint = \\'' + $json.fingerprint + '\\' AND last_seen > NOW() - INTERVAL \\'30 minutes\\' LIMIT 1;' }}", "options": {} }, "id": "3fd78a53-7885-4739-a4b7-b441a27e993e", "name": "Lookup Dedup", "type": "n8n-nodes-base.postgres", "typeVersion": 2.5, "position": [672, 0], "credentials": { "postgres": { "id": "n9svoXemqSZoNNUB", "name": "Postgres account" } }, "continueOnFail": true }, { "parameters": { "jsCode": "const alertData = $('Build Fingerprint').first().json;\nconst rows = $input.all().filter(r => r.json && r.json.fingerprint);\nconst alreadySeen = rows.length > 0;\nconst isLowPriority = !alertData.hasFiring;\nconst suppressNight = alertData.isNightTime && isLowPriority;\nconst suppress = alreadySeen || suppressNight;\nreturn [{ json: { ...alertData, suppress, shouldNotify: suppress ? 'no' : 'yes', suppressReason: alreadySeen ? 'dedup' : suppressNight ? 'night_suppress' : null } }];\n" }, "id": "c34b4b0e-1a47-4624-b1c4-9ef6b5cdeb6c", "name": "Check Should Notify", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [896, 0] }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" }, "conditions": [ { "id": "cc41d77c-b659-4c6f-8e71-96a13dced242", "leftValue": "={{ $json.shouldNotify }}", "rightValue": "yes", "operator": { "type": "string", "operation": "equals" } } ], "combinator": "and" } }, "id": "29929dc8-17f6-4cc8-90c2-c501eaba8be5", "name": "Should Notify?", "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [1120, 0] }, { "parameters": { "jsCode": "const d = $input.first().json;\nconst first = (d.parsed || [])[0] || {};\nreturn [{ json: {\n fingerprint: d.fingerprint,\n dedupAlertName: first.alertName || 'unknown',\n dedupHost: first.host || 'unknown'\n} }];" }, "id": "54705c76-512d-4479-889f-dc0a3c24d1b3", "name": "Prepare Dedup Data", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [1344, 0] }, { "parameters": { "operation": "executeQuery", "query": "={{ 'INSERT INTO grafana_alert_dedup (fingerprint, last_seen, alert_name, host) VALUES (\\'' + $json.fingerprint + '\\', NOW(), \\'' + $json.dedupAlertName + '\\', \\'' + $json.dedupHost + '\\') ON CONFLICT (fingerprint) DO UPDATE SET last_seen = NOW();' }}", "options": {} }, "id": "25a14a6a-db17-49ca-a6d2-6ed2f97d2889", "name": "Upsert Dedup Record", "type": "n8n-nodes-base.postgres", "typeVersion": 2.5, "position": [1568, 0], "credentials": { "postgres": { "id": "n9svoXemqSZoNNUB", "name": "Postgres account" } }, "continueOnFail": true }, { "parameters": { "jsCode": "// Build the LiteLLM request body from Parse Alert Payload data.\n// Doing this in a Code node (instead of inline in the HTTP node) ensures\n// the expression resolves cleanly — same pattern as RSS Digest which is stable.\nconst alertData = $('Parse Alert Payload').first().json;\nconst alertText = alertData.alertText || '(no alert text received)';\n\nreturn [{\n json: {\n aiBody: {\n model: 'medium',\n messages: [\n {\n role: 'system',\n content: 'You are a concise homelab monitoring assistant. Summarize infrastructure alerts in 2-3 sentences. Include: what happened, which host, severity, and a suggested action. Keep it brief — this goes to a phone notification.'\n },\n {\n role: 'user',\n content: 'Summarize this Grafana alert:\\n\\n' + alertText\n }\n ],\n max_tokens: 200,\n temperature: 0.3\n }\n }\n}];" }, "id": "b1c2d3e4-f5a6-7890-abcd-ef1234567890", "name": "Prepare AI Request", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [1792, 0] }, { "parameters": { "method": "POST", "url": "http://10.5.1.6:4000/v1/chat/completions", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "Content-Type", "value": "application/json" }, { "name": "Authorization", "value": "={{'Bearer ' + $env.LITELLM_API_KEY}}" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ JSON.stringify($json.aiBody) }}", "options": { "response": { "response": { "responseFormat": "json" } }, "timeout": 120000 } }, "id": "8ef1aa9d-d5b1-41ea-abc6-703b6491ea33", "name": "LiteLLM Summarize", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [2016, 0], "continueOnFail": true }, { "parameters": { "jsCode": "const response = $input.first().json;\nconst alertData = $('Parse Alert Payload').first().json;\n\nlet summary;\nif (response.error || response.errorMessage) {\n summary = 'AI summary unavailable — check LiteLLM.';\n} else {\n summary = response.choices?.[0]?.message?.content || 'Could not generate summary';\n}\n\nconst title = alertData.hasFiring\n ? `🔥 Alert Firing (${alertData.alertCount})`\n : `✅ Alert Resolved (${alertData.alertCount})`;\n\nconst priority = alertData.hasFiring ? 8 : 2;\n\nreturn [{\n json: {\n title,\n message: summary,\n priority\n }\n}];" }, "id": "2e969bca-8458-4477-a358-664fd1c50c4f", "name": "Format Notification", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [2240, 0] }, { "parameters": { "message": "={{ $json.message }}", "additionalFields": { "priority": "={{ $json.priority }}", "title": "={{ $json.title }}" }, "options": { "contentType": "text/plain" } }, "id": "65c2b53a-6e03-4460-9aa6-5d967d244de0", "name": "Push to Gotify", "type": "n8n-nodes-base.gotify", "typeVersion": 1, "position": [2464, 0], "credentials": { "gotifyApi": { "id": "DXdYZDfVZecDTaNU", "name": "Gotify account" } } } ], "connections": { "Grafana Webhook": { "main": [[{ "node": "Parse Alert Payload", "type": "main", "index": 0 }]] }, "Parse Alert Payload": { "main": [[{ "node": "Build Fingerprint", "type": "main", "index": 0 }]] }, "Build Fingerprint": { "main": [[{ "node": "Lookup Dedup", "type": "main", "index": 0 }]] }, "Lookup Dedup": { "main": [[{ "node": "Check Should Notify", "type": "main", "index": 0 }]] }, "Check Should Notify": { "main": [[{ "node": "Should Notify?", "type": "main", "index": 0 }]] }, "Should Notify?": { "main": [ [{ "node": "Prepare Dedup Data", "type": "main", "index": 0 }], [] ] }, "Prepare Dedup Data": { "main": [[{ "node": "Upsert Dedup Record", "type": "main", "index": 0 }]] }, "Upsert Dedup Record": { "main": [[{ "node": "Prepare AI Request", "type": "main", "index": 0 }]] }, "Prepare AI Request": { "main": [[{ "node": "LiteLLM Summarize", "type": "main", "index": 0 }]] }, "LiteLLM Summarize": { "main": [[{ "node": "Format Notification", "type": "main", "index": 0 }]] }, "Format Notification": { "main": [[{ "node": "Push to Gotify", "type": "main", "index": 0 }]] } }, "settings": { "executionOrder": "v1", "binaryMode": "separate" }, "staticData": null, "meta": { "templateCredsSetupCompleted": true }, "pinData": {} }