forked from mtusi2026/mtusi2026lab7
94 lines
3.4 KiB
Python
Executable File
94 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Lab6 track 2 scenario driver: burst, sustained outage and false-positive phases.
|
|
|
|
It starts the open-loop load, injects a PostgreSQL fault, records alert state and
|
|
restores the stand. Use --quick for a short evidence run; the full 15-minute
|
|
sustained phase is the graded scenario.
|
|
"""
|
|
import argparse
|
|
import os
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from lab7_environment import Environment
|
|
from lab7_http import BASE_URL
|
|
|
|
# The lab tools always target loopback; ignore ambient corporate proxies.
|
|
for _proxy_key in ('HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'http_proxy', 'https_proxy', 'all_proxy'):
|
|
os.environ.pop(_proxy_key, None)
|
|
os.environ['NO_PROXY'] = 'localhost,127.0.0.1,::1'
|
|
os.environ['no_proxy'] = 'localhost,127.0.0.1,::1'
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def compose(*args, check=True):
|
|
return Environment().compose(*args, check=check)
|
|
|
|
|
|
def stamp():
|
|
return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='Lab6 track 2 scenarios')
|
|
parser.add_argument('--scenario', choices=['burst', 'sustained', 'false-positive'], default='burst')
|
|
parser.add_argument('--quick', action='store_true', help='short 20s phases')
|
|
parser.add_argument('--base-url', default=BASE_URL)
|
|
parser.add_argument('--load-rate', type=float, default=0.5)
|
|
parser.add_argument('--output-dir', default='evidence/lab6/track2')
|
|
args = parser.parse_args()
|
|
out = ROOT / args.output_dir
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
duration = 20 if args.quick else (120 if args.scenario == 'burst' else 900)
|
|
if args.scenario == 'false-positive':
|
|
duration = 20 if args.quick else 300
|
|
|
|
load = subprocess.Popen([
|
|
sys.executable, str(ROOT / 'scripts/lab6_load.py'),
|
|
'--base-url', args.base_url, '--rate', str(args.load_rate),
|
|
'--duration', str(duration + 20), '--upload-ratio', '0.2',
|
|
'--output', str(out / (args.scenario + '-load.json')),
|
|
], cwd=ROOT)
|
|
alerts = subprocess.Popen([
|
|
sys.executable, str(ROOT / 'scripts/lab6_alerts.py'),
|
|
'--duration', str(duration + 20), '--output', str(out / (args.scenario + '-alerts.csv')),
|
|
], cwd=ROOT)
|
|
|
|
time.sleep(5)
|
|
injection = stamp()
|
|
try:
|
|
if args.scenario == 'burst':
|
|
compose('pause', 'postgres')
|
|
elif args.scenario == 'sustained':
|
|
compose('stop', 'worker')
|
|
compose('stop', 'postgres')
|
|
time.sleep(duration)
|
|
finally:
|
|
if args.scenario == 'burst':
|
|
compose('unpause', 'postgres', check=False)
|
|
elif args.scenario == 'sustained':
|
|
compose('start', 'postgres', check=False)
|
|
time.sleep(5)
|
|
compose('up', '-d', '--no-deps', 'worker', check=False)
|
|
restore = stamp()
|
|
Path(out / (args.scenario + '-phases.json')).write_text(json.dumps({
|
|
'scenario': args.scenario,
|
|
'injection_utc': injection,
|
|
'restore_utc': restore,
|
|
'duration_seconds': duration,
|
|
}, indent=2) + '\n')
|
|
time.sleep(10)
|
|
|
|
load_rc = load.wait(timeout=duration + 60)
|
|
alerts_rc = alerts.wait(timeout=duration + 60)
|
|
print('track2 evidence:', out, 'load_rc=', load_rc, 'alerts_rc=', alerts_rc)
|
|
return 1 if alerts_rc != 0 else 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|