{
  "name": "System Health Check",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/5 * * * *"
            }
          ]
        }
      },
      "id": "cron-trigger",
      "name": "Every 5 Minutes",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "url": "http://prometheus:9090/api/v1/query",
        "options": {
          "qs": {
            "query": "up"
          }
        }
      },
      "id": "check-prometheus",
      "name": "Check Prometheus",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        460,
        200
      ]
    },
    {
      "parameters": {
        "url": "http://redis:6379/ping"
      },
      "id": "check-redis",
      "name": "Check Redis",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "url": "http://postgres:5432"
      },
      "id": "check-postgres",
      "name": "Check PostgreSQL",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        460,
        400
      ]
    },
    {
      "parameters": {
        "url": "http://mongodb:27017"
      },
      "id": "check-mongodb",
      "name": "Check MongoDB",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        460,
        500
      ]
    },
    {
      "parameters": {
        "jsCode": "// Aggregate health check results\nconst results = [];\n\n// Process each service check\nfor (const item of $input.all()) {\n  const service = item.json.service || 'unknown';\n  const status = item.json.status === 200 ? 'healthy' : 'unhealthy';\n  const responseTime = item.json.responseTime || 0;\n  \n  results.push({\n    service,\n    status,\n    responseTime,\n    timestamp: new Date().toISOString(),\n    healthy: status === 'healthy'\n  });\n}\n\n// Calculate overall health\nconst healthyServices = results.filter(r => r.healthy).length;\nconst totalServices = results.length;\nconst overallHealth = healthyServices / totalServices;\n\nreturn {\n  overallHealth,\n  healthyServices,\n  totalServices,\n  services: results,\n  timestamp: new Date().toISOString()\n};"
      },
      "id": "aggregate-results",
      "name": "Aggregate Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "table": "health_checks",
        "columns": "timestamp, service, status, response_time, healthy",
        "values": "={{ $json.timestamp }}, {{ $json.service }}, {{ $json.status }}, {{ $json.responseTime }}, {{ $json.healthy }}"
      },
      "id": "save-health-data",
      "name": "Save Health Data",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        900,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "condition-1",
              "leftValue": "={{ $json.overallHealth }}",
              "rightValue": 1,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-overall-health",
      "name": "Check Overall Health",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "n8n",
          "mode": "name"
        },
        "text": "\u2705 All services are healthy ({{ $json.healthyServices }}/{{ $json.totalServices }})",
        "otherOptions": {
          "attachments": [
            {
              "color": "good",
              "fields": [
                {
                  "title": "Healthy Services",
                  "value": "{{ $json.healthyServices }}/{{ $json.totalServices }}",
                  "short": true
                },
                {
                  "title": "Overall Health",
                  "value": "{{ Math.round($json.overallHealth * 100) }}%",
                  "short": true
                }
              ]
            }
          ]
        }
      },
      "id": "success-notification",
      "name": "Success Notification",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1340,
        200
      ],
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "n8n",
          "mode": "name"
        },
        "text": "\u26a0\ufe0f System health degraded ({{ $json.healthyServices }}/{{ $json.totalServices }} services healthy)",
        "otherOptions": {
          "attachments": [
            {
              "color": "warning",
              "fields": [
                {
                  "title": "Healthy Services",
                  "value": "{{ $json.healthyServices }}/{{ $json.totalServices }}",
                  "short": true
                },
                {
                  "title": "Overall Health",
                  "value": "{{ Math.round($json.overallHealth * 100) }}%",
                  "short": true
                }
              ]
            }
          ]
        }
      },
      "id": "warning-notification",
      "name": "Warning Notification",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1340,
        400
      ],
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every 5 Minutes": {
      "main": [
        [
          {
            "node": "Check Prometheus",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check Redis",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check PostgreSQL",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check MongoDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Prometheus": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Redis": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check PostgreSQL": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check MongoDB": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Results": {
      "main": [
        [
          {
            "node": "Save Health Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Health Data": {
      "main": [
        [
          {
            "node": "Check Overall Health",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Overall Health": {
      "main": [
        [
          {
            "node": "Success Notification",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Warning Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "createdAt": "2024-01-01T00:00:00.000Z",
      "updatedAt": "2024-01-01T00:00:00.000Z",
      "id": "monitoring",
      "name": "monitoring"
    }
  ],
  "triggerCount": 1,
  "updatedAt": "2024-01-01T00:00:00.000Z",
  "versionId": "1"
}