#!/bin/bash # Alert Resolution Testing - Wave 75 Agent 8 # Demonstrates how to test alert firing and resolution PROMETHEUS_URL="http://localhost:9099" ALERTMANAGER_URL="http://localhost:9093" echo "=======================================================================" echo " Alert Resolution Testing Framework" echo " Demonstrates alert lifecycle: inactive → pending → firing → resolved" echo "=======================================================================" echo "" # Check if amtool is available if ! command -v amtool &> /dev/null; then echo "⚠️ amtool not installed - cannot simulate alerts" echo "" echo "To install amtool:" echo " wget https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz" echo " tar xzf alertmanager-0.26.0.linux-amd64.tar.gz" echo " sudo cp alertmanager-0.26.0.linux-amd64/amtool /usr/local/bin/" echo "" exit 1 fi echo "=== Test 1: Simulate Critical Auth Latency Alert ===" echo "Injecting alert into AlertManager..." amtool alert add AuthLatencySLAViolation \ --annotation=summary="P99 auth latency exceeded 10μs SLA" \ --annotation=description="p99 auth latency is 15.3μs (target: <10μs)" \ --label=severity=critical \ --label=component=auth \ --label=alertname=AuthLatencySLAViolation \ --alertmanager.url="$ALERTMANAGER_URL" \ 2>/dev/null sleep 2 echo "Checking if alert is active..." ACTIVE=$(curl -s "$ALERTMANAGER_URL/api/v2/alerts" 2>/dev/null | \ jq '[.[] | select(.labels.alertname=="AuthLatencySLAViolation")] | length') if [ "$ACTIVE" -gt 0 ]; then echo "✅ Alert fired successfully!" curl -s "$ALERTMANAGER_URL/api/v2/alerts" 2>/dev/null | \ jq -r '.[] | select(.labels.alertname=="AuthLatencySLAViolation") | " Status: \(.status.state)\n Receiver: \(.receivers[0].name)\n Started: \(.startsAt)"' else echo "❌ Alert not found in AlertManager" fi echo "" echo "=== Test 2: Verify Alert Routing ===" RECEIVER=$(curl -s "$ALERTMANAGER_URL/api/v2/alerts" 2>/dev/null | \ jq -r '.[] | select(.labels.alertname=="AuthLatencySLAViolation") | .receivers[0].name' 2>/dev/null) if [ "$RECEIVER" == "critical-alerts" ] || [ "$RECEIVER" == "auth-alerts" ]; then echo "✅ Alert routed to correct receiver: $RECEIVER" else echo "⚠️ Alert routing: $RECEIVER (expected: critical-alerts or auth-alerts)" fi echo "" echo "=== Test 3: Test Alert Inhibition ===" echo "Simulating CircuitBreakerOpen alert (should inhibit HighBackendLatency)..." amtool alert add CircuitBreakerOpen \ --annotation=summary="Circuit breaker open for trading service" \ --annotation=description="Backend service trading circuit breaker is open" \ --label=severity=critical \ --label=component=proxy \ --label=service=trading \ --label=alertname=CircuitBreakerOpen \ --alertmanager.url="$ALERTMANAGER_URL" \ 2>/dev/null sleep 2 amtool alert add HighBackendLatency \ --annotation=summary="High latency to trading service" \ --annotation=description="p99 latency to trading is 150ms (threshold: 100ms)" \ --label=severity=warning \ --label=component=proxy \ --label=service=trading \ --label=alertname=HighBackendLatency \ --alertmanager.url="$ALERTMANAGER_URL" \ 2>/dev/null sleep 2 INHIBITED=$(curl -s "$ALERTMANAGER_URL/api/v2/alerts" 2>/dev/null | \ jq '[.[] | select(.labels.alertname=="HighBackendLatency" and .status.state=="suppressed")] | length') if [ "$INHIBITED" -gt 0 ]; then echo "✅ Inhibition working: HighBackendLatency suppressed by CircuitBreakerOpen" else echo "⚠️ Inhibition may not be working (HighBackendLatency not suppressed)" fi echo "" echo "=== Test 4: List All Active Alerts ===" curl -s "$ALERTMANAGER_URL/api/v2/alerts" 2>/dev/null | \ jq -r '.[] | "- \(.labels.alertname) (\(.status.state)) → \(.receivers[0].name)"' echo "" echo "=== Test 5: Silence All Test Alerts ===" echo "Creating silence for all test alerts..." amtool silence add \ alertname=~"AuthLatencySLAViolation|CircuitBreakerOpen|HighBackendLatency" \ --alertmanager.url="$ALERTMANAGER_URL" \ --comment="Wave 75 Agent 8 testing complete" \ --duration=1h \ 2>/dev/null sleep 2 SILENCED=$(curl -s "$ALERTMANAGER_URL/api/v2/alerts" 2>/dev/null | \ jq '[.[] | select(.status.state=="suppressed")] | length') echo "✅ Silenced $SILENCED alerts" echo "" echo "=== Test 6: Clean Up Test Alerts ===" echo "Note: amtool cannot remove alerts - they will expire naturally" echo "Alerts will auto-resolve when they reach their timeout" echo "" echo "=======================================================================" echo " Test Summary" echo "=======================================================================" echo "✅ Alert injection tested (AuthLatencySLAViolation)" echo "✅ Alert routing verified" echo "✅ Alert inhibition tested (CircuitBreakerOpen → HighBackendLatency)" echo "✅ Alert silencing tested" echo "" echo "To view active alerts:" echo " curl -s http://localhost:9093/api/v2/alerts | jq ." echo "" echo "To view silences:" echo " amtool --alertmanager.url=http://localhost:9093 silence query" echo "" echo "To expire silences:" echo " amtool --alertmanager.url=http://localhost:9093 silence expire " echo ""