Files
truenas-stacks/n8n-workflows/15-n8n-health-monitor-v1.2.json

303 lines
10 KiB
JSON

{
"name": "n8n Self-Health Monitor v1.2",
"active": true,
"isArchived": false,
"nodes": [
{
"parameters": {
"rule": {
"interval": [
{
"field": "minutes",
"minutesInterval": 15
}
]
}
},
"id": "dabcb0ae-d513-410e-b779-4570df5c4a35",
"name": "Every 15 Minutes",
"type": "n8n-nodes-base.scheduleTrigger",
"typeVersion": 1.2,
"position": [0, 0]
},
{
"parameters": {
"method": "GET",
"url": "http://10.5.30.6:5678/api/v1/workflows",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "X-N8N-API-KEY",
"value": "={{ $env.N8N_API_KEY }}"
}
]
},
"sendQuery": true,
"queryParameters": {
"parameters": [
{
"name": "limit",
"value": "250"
}
]
},
"options": {}
},
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"name": "Fetch All Workflows",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [224, 0]
},
{
"parameters": {
"method": "GET",
"url": "http://10.5.30.6:5678/api/v1/executions",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "X-N8N-API-KEY",
"value": "={{ $env.N8N_API_KEY }}"
}
]
},
"sendQuery": true,
"queryParameters": {
"parameters": [
{
"name": "status",
"value": "error"
},
{
"name": "includeData",
"value": "false"
},
{
"name": "limit",
"value": "50"
}
]
},
"options": {}
},
"id": "806d75e9-b95a-499a-b997-a7f7dc046775",
"name": "Fetch Failed Executions",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [448, 0]
},
{
"parameters": {
"jsCode": "const data = $input.first().json;\nconst executions = data.data || [];\nconst oneHourAgo = Date.now() - (60 * 60 * 1000);\n\n// Build workflow ID → name lookup from previously fetched workflows\nconst wfResponse = $('Fetch All Workflows').first().json;\nconst nameMap = {};\n(wfResponse.data || []).forEach(wf => { nameMap[wf.id] = wf.name; });\n\n// Filter to failures in the past hour\nconst recent = executions.filter(e => {\n const ts = new Date(e.startedAt || e.stoppedAt || 0).getTime();\n return ts >= oneHourAgo;\n});\n\n// Map executions to failure objects, resolving names from the lookup\nconst failures = recent.map(e => ({\n executionId: e.id,\n workflowId: e.workflowId,\n workflowName: nameMap[e.workflowId] || e.workflowData?.name || `Workflow ${e.workflowId}`,\n startedAt: e.startedAt,\n stoppedAt: e.stoppedAt,\n errorMessage: e.data?.resultData?.error?.message || 'Unknown error'\n}));\n\n// Deduplicate by workflowName (report once per workflow per check)\nconst seen = new Set();\nconst unique = failures.filter(f => {\n if (seen.has(f.workflowName)) return false;\n seen.add(f.workflowName);\n return true;\n});\n\nreturn [{ json: { hasFailures: unique.length > 0, hasFailuresStr: unique.length > 0 ? 'yes' : 'no', count: unique.length, failures: unique } }];"
},
"id": "f5d47910-5047-4c4f-aa84-a6907aa38551",
"name": "Filter Recent Failures",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [672, 0]
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"id": "02ff82df-a31d-491b-9cd1-96874c8ae2fe",
"leftValue": "={{ $json.hasFailuresStr }}",
"rightValue": "yes",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
}
},
"id": "7c298433-ab89-435e-8955-73da6b986f46",
"name": "Has Failures?",
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [896, 0]
},
{
"parameters": {
"jsCode": "const d = $input.first().json;\n// Fingerprint = sorted workflow names joined — stable across retriggers\nconst fingerprint = (d.failures || [])\n .map(f => f.workflowName)\n .sort()\n .join('|') || 'unknown';\nreturn [{ json: { ...d, fingerprint } }];"
},
"id": "d3609d45-ba9f-41d3-a9bd-8c558a719f10",
"name": "Build Fingerprint",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [1120, -120]
},
{
"parameters": {
"operation": "executeQuery",
"query": "SELECT fingerprint, last_seen FROM n8n_health_dedup WHERE fingerprint = '{{ $json.fingerprint }}' AND last_seen > NOW() - INTERVAL '1 hour' LIMIT 1;",
"options": {}
},
"id": "15a3dd71-bc5b-4396-9534-63518e097438",
"name": "Lookup Dedup",
"type": "n8n-nodes-base.postgres",
"typeVersion": 2.5,
"position": [1344, -120],
"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 alreadyNotified = rows.length > 0;\nreturn [{ json: { ...alertData, alreadyNotified, shouldNotify: !alreadyNotified, shouldNotifyStr: !alreadyNotified ? 'yes' : 'no' } }];"
},
"id": "a92590e3-7044-4ba3-9b39-f6a9457c5fbe",
"name": "Check Already Notified",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [1568, -120]
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"id": "aa5db0c3-75a7-4ea4-b29e-c12568ccf320",
"leftValue": "={{ $json.shouldNotifyStr }}",
"rightValue": "yes",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
}
},
"id": "39ac0eaf-73ce-4fe1-9c4b-d09ac920291d",
"name": "Should Notify?",
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [1792, -120]
},
{
"parameters": {
"operation": "executeQuery",
"query": "INSERT INTO n8n_health_dedup (fingerprint, last_seen, workflow_names) VALUES ('{{ $json.fingerprint }}', NOW(), '{{ $json.failures.map(f => f.workflowName).join(\", \") }}') ON CONFLICT (fingerprint) DO UPDATE SET last_seen = NOW(), workflow_names = EXCLUDED.workflow_names;",
"options": {}
},
"id": "0c3be155-0c3a-4354-b863-73a3f5bbf598",
"name": "Upsert Dedup Record",
"type": "n8n-nodes-base.postgres",
"typeVersion": 2.5,
"position": [2016, -120],
"credentials": {
"postgres": {
"id": "n9svoXemqSZoNNUB",
"name": "Postgres account"
}
}
},
{
"parameters": {
"jsCode": "const d = $('Build Fingerprint').first().json;\nconst lines = (d.failures || []).map(f =>\n ` • ${f.workflowName}\\n ${f.errorMessage.substring(0, 120)}`\n);\nconst body = `${d.count} workflow${d.count !== 1 ? 's' : ''} failed in the past hour:\\n\\n${lines.join('\\n')}\\n\\nhttps://n8n.paccoco.com`;\nreturn [{ json: { title: `⚠️ n8n: ${d.count} Workflow Failure${d.count !== 1 ? 's' : ''}`, message: body, priority: 7 } }];"
},
"id": "1c6b42bf-a38e-47b6-82ac-50c43ee9e583",
"name": "Format Notification",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [2240, -120]
},
{
"parameters": {
"message": "={{ $json.message }}",
"additionalFields": {
"priority": "={{ $json.priority }}",
"title": "={{ $json.title }}"
},
"options": {
"contentType": "text/plain"
}
},
"id": "09435c2f-9044-432a-8ca8-dd3ce3262c76",
"name": "Push to Gotify",
"type": "n8n-nodes-base.gotify",
"typeVersion": 1,
"position": [2464, -120],
"credentials": {
"gotifyApi": {
"id": "SETUP_REQUIRED",
"name": "n8n Health"
}
}
}
],
"connections": {
"Every 15 Minutes": {
"main": [[{ "node": "Fetch All Workflows", "type": "main", "index": 0 }]]
},
"Fetch All Workflows": {
"main": [[{ "node": "Fetch Failed Executions", "type": "main", "index": 0 }]]
},
"Fetch Failed Executions": {
"main": [[{ "node": "Filter Recent Failures", "type": "main", "index": 0 }]]
},
"Filter Recent Failures": {
"main": [[{ "node": "Has Failures?", "type": "main", "index": 0 }]]
},
"Has Failures?": {
"main": [
[{ "node": "Build Fingerprint", "type": "main", "index": 0 }],
[]
]
},
"Build Fingerprint": {
"main": [[{ "node": "Lookup Dedup", "type": "main", "index": 0 }]]
},
"Lookup Dedup": {
"main": [[{ "node": "Check Already Notified", "type": "main", "index": 0 }]]
},
"Check Already Notified": {
"main": [[{ "node": "Should Notify?", "type": "main", "index": 0 }]]
},
"Should Notify?": {
"main": [
[{ "node": "Upsert Dedup Record", "type": "main", "index": 0 }],
[]
]
},
"Upsert Dedup Record": {
"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": null,
"pinData": {},
"tags": [
{ "name": "monitoring" },
{ "name": "scheduled" },
{ "name": "n8n" }
],
"id": "febfda9b-0f4e-40b5-ab84-82ba9b7fcf29"
}