From d388aa3e9002a946ff89f2efcd951d8559372131 Mon Sep 17 00:00:00 2001 From: Paccoco <99041736+Paccoco@users.noreply.github.com> Date: Sat, 9 May 2026 15:21:11 -0500 Subject: [PATCH] Add workflow 01 v1.4; update README env var docs and TODO --- docs/planning/TODO.md | 8 +- .../01-grafana-alert-ai-gotify-v1.4.json | 250 ++++++++++++++++++ n8n-workflows/README.md | 18 +- 3 files changed, 272 insertions(+), 4 deletions(-) create mode 100644 n8n-workflows/01-grafana-alert-ai-gotify-v1.4.json diff --git a/docs/planning/TODO.md b/docs/planning/TODO.md index 5c5c198..29ed736 100644 --- a/docs/planning/TODO.md +++ b/docs/planning/TODO.md @@ -36,10 +36,10 @@ ## n8n Workflows - [ ] Create Gotify apps and n8n credentials: Reminders, n8n Health, Uptime / Infra -- [ ] Add env vars to n8n: `PAPERLESS_API_TOKEN`, `PAPERLESS_TAG_TRANSCRIPTION`, `PAPERLESS_TAG_LECTURE_NOTES`, `N8N_API_KEY` +- [ ] Add remaining env vars to n8n docker-compose + .env: `PAPERLESS_API_TOKEN`, `PAPERLESS_TAG_TRANSCRIPTION`, `PAPERLESS_TAG_LECTURE_NOTES`, `N8N_API_KEY` (see n8n-workflows/README.md for docker-compose.yaml changes required) - [ ] Set up `PD SSH` credential in n8n for workflow 16 (see n8n-workflows/README.md) - [ ] Add sudoers NOPASSWD rule for n8n watchdog (see n8n-workflows/README.md) -- [ ] Import and activate upgraded workflows: 01, 04, 08 +- [ ] Import and activate upgraded workflows: 04, 08 (01 done — v1.4 active and tested) - [ ] Import and activate new workflows: 14, 15 (16 already active) ## Completed @@ -56,4 +56,6 @@ - [x] Write n8n workflow upgrade + new workflow plans (01, 04, 08, 14, 15, 16) — 2026-05-09 - [x] Write Pi-hole 3-node HA deployment plan — 2026-05-09 - [x] Write Kima-Hub deployment plan — 2026-05-09 -- [x] Create Postgres dedup tables (grafana_alert_dedup, n8n_health_dedup) on PD — 2026-05-09 +- [x] Create Postgres dedup tables (grafana_alert_dedup, n8n_health_dedup) in n8n_rss DB on PD — 2026-05-09 +- [x] Workflow 01 fully tested end-to-end (dedup, LiteLLM, Gotify) — v1.4 active — 2026-05-09 +- [x] Add LITELLM_API_KEY + N8N_BLOCK_ENV_ACCESS_IN_NODE to n8n docker-compose — 2026-05-09 diff --git a/n8n-workflows/01-grafana-alert-ai-gotify-v1.4.json b/n8n-workflows/01-grafana-alert-ai-gotify-v1.4.json new file mode 100644 index 0000000..8926c60 --- /dev/null +++ b/n8n-workflows/01-grafana-alert-ai-gotify-v1.4.json @@ -0,0 +1,250 @@ +{ + "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": {} +} diff --git a/n8n-workflows/README.md b/n8n-workflows/README.md index bfca771..3ce921c 100644 --- a/n8n-workflows/README.md +++ b/n8n-workflows/README.md @@ -61,7 +61,9 @@ CREATE TABLE IF NOT EXISTS n8n_health_dedup ( ### n8n Environment Variables -Add these to n8n's `.env` and run `sudo docker compose up -d` to apply: +Two steps required — both the `.env` file AND the `docker-compose.yaml` `environment:` section must be updated, then n8n restarted with `sudo docker compose --env-file .env down n8n && sudo docker compose --env-file .env up -d n8n`. + +**automation/.env** — add these: | Variable | Used by | How to get | |----------|---------|------------| @@ -69,6 +71,20 @@ Add these to n8n's `.env` and run `sudo docker compose up -d` to apply: | `PAPERLESS_TAG_TRANSCRIPTION` | 04 | Tag ID from Paperless API | | `PAPERLESS_TAG_LECTURE_NOTES` | 08 | Tag ID from Paperless API | | `N8N_API_KEY` | 15 | n8n → Settings → API → Create API Key | +| `LITELLM_API_KEY` | 01, 04, 08 | LiteLLM virtual key (already in .env) | + +**automation/docker-compose.yaml** — add to n8n `environment:` section: + +```yaml + N8N_BLOCK_ENV_ACCESS_IN_NODE: "false" + LITELLM_API_KEY: ${LITELLM_API_KEY} + PAPERLESS_API_TOKEN: ${PAPERLESS_API_TOKEN} + PAPERLESS_TAG_TRANSCRIPTION: ${PAPERLESS_TAG_TRANSCRIPTION} + PAPERLESS_TAG_LECTURE_NOTES: ${PAPERLESS_TAG_LECTURE_NOTES} + N8N_API_KEY: ${N8N_API_KEY} +``` + +> **Note:** `N8N_BLOCK_ENV_ACCESS_IN_NODE: "false"` is required for workflows to read `$env.*` variables. Without it, env var access is silently denied even if the variable exists in the container. ### SSH Credential (required for workflow 16)