AIOps in Practice: Using LLMs for Prometheus Alert Analysis and RCA

AI tutorial - IT technology blog
AI tutorial - IT technology blog

Beyond the 3 AM Pager: Why Manual Monitoring Fails

On-call shifts shouldn’t feel like a guessing game. Most DevOps engineers spend the first 20 minutes of an incident just gathering context. You’re usually hunting for the specific pod that’s failing, scanning logs for stack traces, or checking if a recent deployment caused a 15% spike in latency. It is an exhausting, manual process that drains productivity.

Prometheus is excellent at identifying what is broken, but it lacks the logic to explain why. This gap leads to alert fatigue, where teams are buried under hundreds of notifications that require human eyes to filter. By integrating Large Language Models (LLMs) as a first-responder, we can automate the initial investigation phase.

Implementing this in a production environment typically yields impressive results. In my recent projects, this workflow reduced the Mean Time to Resolution (MTTR) by 40% simply by providing engineers with a pre-analyzed report before they even logged into the terminal.

Connecting Prometheus to Large Language Models

The goal is to build a bridge between your monitoring data and an LLM like GPT-4o or a local Llama 3 instance. We aren’t just asking the AI to summarize an alert. Instead, we want it to perform a deep Root Cause Analysis (RCA) by evaluating metadata, historical patterns, and runbook instructions.

The Workflow Architecture

Our automated pipeline consists of five clear stages:

  1. Detection: Prometheus identifies a threshold breach, such as a memory leak.
  2. Grouping: Alertmanager bundles related alerts and pushes a JSON payload to a custom webhook.
  3. Enrichment: A Python-based webhook fetches extra context, including recent logs or pod status.
  4. Analysis: The LLM processes the data and generates a structured RCA report.
  5. Delivery: The final analysis appears in Slack with a suggested fix and an execution button.

Implementation Guide

Let’s build a functional Python receiver to handle the heavy lifting. This service will act as the intelligence layer of your monitoring stack.

Step 1: Defining the Prometheus Alert

We need a high-quality alert to trigger the process. The following rule monitors memory usage. If consumption exceeds 85% for more than five minutes, it fires an alert. Save this in your alert_rules.yml.

groups:
- name: host_alerts
  rules:
  - alert: HighMemoryUsage
    expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 85
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "Host high memory usage on {{ $labels.instance }}"
      description: "Memory usage is above 85% for more than 5 minutes."

Step 2: Building the LLM Webhook Receiver

FastAPI is the ideal tool for this because it is lightweight and handles asynchronous JSON requests efficiently. This script captures the Prometheus alert and forwards it to the OpenAI API.

Install the necessary dependencies first:

pip install fastapi uvicorn openai

Here is the core logic for the receiver:

from fastapi import FastAPI, Request
from openai import OpenAI
import os

app = FastAPI()
client = OpenAI(api_key="your_api_key_here")

@app.post("/webhook")
async def prometheus_webhook(request: Request):
    data = await request.json()
    
    for alert in data.get('alerts', []):
        alert_name = alert['labels'].get('alertname')
        instance = alert['labels'].get('instance')
        description = alert['annotations'].get('description')
        
        # Instruct the LLM to act as an SRE
        prompt = f"""
        Analyze this Kubernetes alert:
        - Alert: {alert_name}
        - Instance: {instance}
        - Description: {description}
        
        Provide a concise report including:
        1. Likely Root Causes.
        2. Commands to diagnose (e.g., kubectl top pods).
        3. Immediate remediation steps.
        """
        
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}]
        )
        
        analysis = response.choices[0].message.content
        print(f"--- RCA for {alert_name} ---\n{analysis}")
        
    return {"status": "processed"}

Step 3: Engineering the Prompt for Production RCA

The quality of your AIOps system depends entirely on context. If you only provide the alert name, the AI will offer generic advice. To get specific answers, you must include your internal runbooks in the prompt template.

By feeding the LLM snippets of your documentation, you transform it from a general assistant into an expert on your specific infrastructure. For example, tell the model to check for OOM kills specifically in the dmesg logs if it detects memory pressure.

# Enhanced context for better accuracy
system_context = """
You are an expert SRE. Follow these internal rules:
- For memory issues, identify the top 3 processes using 'top -o %MEM'.
- Check if the Java Heap is exceeding 80% of the container limit.
- Search for 'Out of memory' patterns in the last 100 lines of logs.
"""

Step 4: Activating the Alertmanager Webhook

Finally, configure Prometheus Alertmanager to route notifications to your Python service. Update your alertmanager.yml configuration:

receivers:
- name: 'aiops-bridge'
  webhook_configs:
  - url: 'http://your-webhook-server:8000/webhook'

route:
  receiver: 'aiops-bridge'

The Path to Self-Healing Systems

Analysis is only the first half of the battle. The logical next step is “Human-in-the-loop” automation. While I don’t recommend letting an LLM execute commands on production servers autonomously, you can provide a bridge.

A common pattern involves adding an interactive button to the Slack alert. If the LLM suggests clearing a /tmp directory that is at 95% capacity, the Slack message can include an [Execute Cleanup] button. Clicking this triggers a pre-defined Ansible playbook.

This approach maintains security while eliminating the need for manual CLI work. This setup evolves with your infrastructure. As you refine your prompts, the RCA becomes so accurate that you’ll have the solution ready before your first cup of coffee is finished.

Share: