Files
mtusi2026lab6/student-tests/firing_alert_s3.sh
T
2026-11-19 18:40:32 +03:00

109 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
mkdir -p evidence
echo "=== Сценарий: AttachmentQueueLagHigh через остановку S3 ==="
# 1. Остановить S3
docker compose stop s3
sleep 3
# 2. Создать 5 attachments (jobs попадут в pending)
python3 -c "
import requests, uuid, base64, time
base = 'http://localhost:8080'
# Один пользователь, один чат
u = requests.post(f'{base}/api/v1/users',
json={'username':f'user{uuid.uuid4().hex[:8]}','display_name':'T','password':'correcthorsebatterystaple'}).json()
token = requests.post(f'{base}/api/v1/auth/sessions',
auth=(u['username'],'correcthorsebatterystaple')).json()['token']
auth_h = {'Authorization': f'Bearer {token}'}
chat = requests.post(f'{base}/api/v1/chats',
json={'title':'t','member_ids':[u['id']]},
headers={**auth_h, 'Content-Type': 'application/json'}).json()
png = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==')
print('Creating 5 attachments...')
for i in range(5):
r = requests.post(f'{base}/api/v1/chats/{chat[\"id\"]}/attachments?filename=t{i}.png',
headers={**auth_h, 'Content-Type': 'image/png'},
data=png)
print(f'{i+1}/5: status={r.status_code}')
time.sleep(0.5)
"
# 3. Проверить, что jobs в pending
echo ""
echo "=== Jobs в БД ==="
docker compose exec -T postgres sh -c 'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT status,count(*) FROM attachment_jobs GROUP BY status;"'
# 4. Polling alerts каждые 5 секунд
echo ""
echo "Polling alerts каждые 5 секунд..."
FIRED=0
for i in $(seq 1 36); do
sleep 5
STATE=$(curl -s http://localhost:9090/api/v1/alerts | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
firing = []
for group in data.get('data',{}).get('groups',[]):
for rule in group.get('rules',[]):
if rule.get('alerts'):
for alert in rule['alerts']:
if alert['state'] == 'firing':
firing.append(alert['labels']['alertname'])
if firing:
print('FIRING:' + ','.join(firing))
else:
print('NONE')
except:
print('ERROR')
")
echo "[$((i*5))s] alerts: $STATE"
if [[ "$STATE" == FIRING:* ]]; then
echo ""
echo "✓ ALERT 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=attachment_jobs_oldest_age_seconds' \
> evidence/queue_lag_metric.json
FIRED=1
break
fi
done
# 5. Восстановить S3
docker compose start s3
sleep 15
if [ $FIRED -eq 1 ]; then
echo ""
echo "✓✓✓ FIRING ALERT ЗАФИКСИРОВАН ✓✓✓"
python3 -c "
import json
with open('evidence/alerts_firing.json') as f:
data = json.load(f)
for group in data.get('data',{}).get('groups',[]):
for rule in group.get('rules',[]):
if rule.get('alerts'):
for alert in rule['alerts']:
if alert['state'] == 'firing':
print(f\"✓ {alert['labels']['alertname']}: FIRING\")
print(f\" Summary: {alert.get('annotations',{}).get('summary','')}\")
"
else
echo ""
echo "✗ Alert не сработал"
fi
make test