forked from mtusi2026/mtusi2026lab6
77 lines
3.6 KiB
Bash
Executable File
77 lines
3.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -e
|
|
mkdir -p evidence
|
|
|
|
echo "=== Демонстрация: ApiHighErrorRate firing → resolved ==="
|
|
|
|
# 1. Базовый трафик (доля 5xx = 0)
|
|
echo "=== Фаза 1: нормальный трафик (30s) ==="
|
|
python3 student-tests/generate_metrics.py &
|
|
BG_PID=$!
|
|
sleep 30
|
|
|
|
# Проверить долю 5xx (должна быть 0%)
|
|
curl -s 'http://localhost:9090/api/v1/query' \
|
|
--data-urlencode 'query=(sum(rate(http_server_request_duration_seconds_count{service_name="messenger-api",http_response_status_code=~"5..",http_route!="/health/ready"}[2m])) / clamp_min(sum(rate(http_server_request_duration_seconds_count{service_name="messenger-api",http_route!="/health/ready"}[2m])), 0.001)) * 100 or vector(0)' \
|
|
| python3 -c "import sys,json; d=json.load(sys.stdin); [print(f'5xx share: {float(r[\"value\"][1]):.2f}%') for r in d['data']['result']]"
|
|
|
|
# 2. PostgreSQL down + бизнес-трафик
|
|
echo ""
|
|
echo "=== Фаза 2: PostgreSQL DOWN (90s) ==="
|
|
docker compose stop postgres
|
|
|
|
# Ждать пока alert станет firing
|
|
FIRED=0
|
|
for i in $(seq 1 18); do
|
|
sleep 5
|
|
STATE=$(curl -s http://localhost:9090/api/v1/alerts | python3 -c "
|
|
import sys, json
|
|
d = json.load(sys.stdin)
|
|
f = [a['labels']['alertname'] for g in d.get('data',{}).get('groups',[])
|
|
for r in g.get('rules',[]) for a in r.get('alerts',[]) if a['state']=='firing']
|
|
print(','.join(f) if f else 'NONE')")
|
|
echo "[down $((i*5))s] alerts: $STATE"
|
|
if [ "$STATE" != "NONE" ] && [ $FIRED -eq 0 ]; then
|
|
FIRED=1
|
|
echo "✓ FIRING detected!"
|
|
curl -s http://localhost:9090/api/v1/alerts > evidence/alerts_firing.json
|
|
curl -s 'http://localhost:9090/api/v1/query' \
|
|
--data-urlencode 'query=(sum(rate(http_server_request_duration_seconds_count{service_name="messenger-api",http_response_status_code=~"5..",http_route!="/health/ready"}[2m])) / clamp_min(sum(rate(http_server_request_duration_seconds_count{service_name="messenger-api",http_route!="/health/ready"}[2m])), 0.001)) * 100 or vector(0)' \
|
|
> evidence/error_rate_at_firing.json
|
|
fi
|
|
done
|
|
|
|
# 3. PostgreSQL up + бизнес-трафик
|
|
echo ""
|
|
echo "=== Фаза 3: PostgreSQL UP (60s) ==="
|
|
docker compose start postgres
|
|
sleep 60
|
|
|
|
# Проверить долю 5xx (должна упасть к 0%)
|
|
curl -s 'http://localhost:9090/api/v1/query' \
|
|
--data-urlencode 'query=(sum(rate(http_server_request_duration_seconds_count{service_name="messenger-api",http_response_status_code=~"5..",http_route!="/health/ready"}[2m])) / clamp_min(sum(rate(http_server_request_duration_seconds_count{service_name="messenger-api",http_route!="/health/ready"}[2m])), 0.001)) * 100 or vector(0)' \
|
|
| python3 -c "import sys,json; d=json.load(sys.stdin); [print(f'5xx share: {float(r[\"value\"][1]):.2f}%') for r in d['data']['result']]"
|
|
|
|
# Проверить resolved
|
|
curl -s http://localhost:9090/api/v1/alerts | python3 -c "
|
|
import sys, json
|
|
d = json.load(sys.stdin)
|
|
for g in d.get('data',{}).get('groups',[]):
|
|
for r in g.get('rules',[]):
|
|
if r.get('alerts'):
|
|
for a in r['alerts']:
|
|
print(f\"{a['labels']['alertname']}: {a['state']}\")" > evidence/alerts_resolved.txt
|
|
|
|
cat evidence/alerts_resolved.txt
|
|
|
|
# 4. Завершить генератор
|
|
kill $BG_PID 2>/dev/null || true
|
|
|
|
# 5. Финальная проверка
|
|
make test
|
|
|
|
echo ""
|
|
echo "=== Доказательства ==="
|
|
echo "evidence/alerts_firing.json - alert в состоянии firing"
|
|
echo "evidence/error_rate_at_firing.json - доля 5xx в момент firing"
|
|
echo "evidence/alerts_resolved.txt - alert в состоянии resolved/inactive" |