AI/ML
Automate Incident Alerts and Logging with n8n
Overview
n8n monitors server health via Prometheus, logs incidents in a database, and sends instant alerts to Slack when issues like high CPU usage are detected.
Prerequisites
n8n installed.
Prometheus instance with server metrics.
PostgreSQL database for logging.
Slack workspace with incoming webhook.
Workflow Setup
1. Prepare n8n Environment
- Navigate to http://localhost:5678 and log in.
- Click “Workflows,” then “+ New” and name it (e.g., “Incident Logger”).
- Click “+” to add nodes.
2. Trigger Node: Schedule
Node: Schedule
Settings:
- Interval: Every 5 minutes
Output: Triggers workflow.
3. Monitor Node: HTTP Request
Node: HTTP Request
Settings:
- URL: http://<prometheus-host>:9090/api/v1/query?query=avg(rate(node_cpu_seconds_total[5m]))
- Method: GET
- Response Format: JSON
Output: CPU usage data (e.g., {{ $json.data.result[0].value[1] }}).
4. Log Node: PostgreSQL
Node: PostgreSQL
Settings:
- Credentials: Database credentials
- Query: INSERT INTO incidents (timestamp, cpu_usage) VALUES (NOW(), {{ $node["HTTP Request"].json.data.result[0].value[1] }})
Output: Incident logged.
5. Alert Node: Slack
Node: Slack
Settings:
- Credentials: Slack webhook URL
- Channel: #incident-alerts
- Text: High CPU usage detected: {{ $node["HTTP Request"].json.data.result[0].value[1] }}%
- Condition: Execute if {{ $node["HTTP Request"].json.data.result[0].value[1] > 80 }}
Output: Team notified.
Workflow Summary
Flow: Schedule → HTTP Request (Prometheus) → PostgreSQL (log) → Slack (alert if high CPU).
Execution: ~10 seconds per run.
Benefits
Minimizes response time to critical failures (~5 minutes).
Enhances reliability with logged incident history.
Saves ~15 hours weekly on manual monitoring.
Troubleshooting
Prometheus Fail: Check query syntax and endpoint.
Slack Issues: Verify webhook permissions.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our AI/ML Expertise.
Comment