#!/usr/bin/env bash
# Real demos for P119 "How SNMP Network Monitoring Works". Made on the Mac (Docker Desktop).
# A craft brewery's small network: five containers from one stock Ubuntu 24.04 image with systemd as PID 1
# (lab/Dockerfile: Ubuntu's own Net-SNMP 5.9 agent, trap receiver and tools), on two private Docker networks:
#   taproom LAN 192.168.71.0/24
#     router     192.168.71.1   the brewery's router (Linux, IP forwarding on): its snmpd is the agent we watch;
#                               interfaces renamed "lan" and "wan" so the router's own interface list reads plainly
#     brewhouse  192.168.71.10  the brewhouse server (its own snmpd)
#     office     192.168.71.20  Ana's office PC: snmpget/snmpwalk, snmptrapd, the wan-rate monitor (lab/wan-rate)
#     guest      192.168.71.30  a guest laptop on the taproom Wi-Fi that streams a big download (the traffic)
#   "internet"  198.51.100.0/24 (a documentation range)
#     router     198.51.100.1   the router's wan side, limited to 25 Mbit/s with tc (the brewery's internet line)
#     website    198.51.100.80  a web server with a 2 GB file
# Every command shown is typed into a real interactive bash in a pty, one window per machine (demo_driver.py);
# every reply, timeout, trap and packet is real.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p119
cleanup() {
  for b in router brewhouse office guest website; do docker rm -f $P-$b >/dev/null 2>&1 || true; done
  for n in lan wan; do docker network rm $P-$n >/dev/null 2>&1 || true; done
}
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t $P lab >/dev/null
docker network create --subnet 192.168.71.0/24 --gateway 192.168.71.254 $P-lan >/dev/null
docker network create --subnet 198.51.100.0/24 --gateway 198.51.100.254 $P-wan >/dev/null
box() {  # box NAME NET IP [docker run options]
  local n=$1 net=$2 ip=$3; shift 3
  docker run -d --name $P-$n --hostname $n --network $P-$net --ip $ip --privileged --cgroupns=private \
    --tmpfs /run --tmpfs /run/lock "$@" $P >/dev/null
}
box router lan 192.168.71.1 --sysctl net.ipv4.ip_forward=1
docker network connect --ip 198.51.100.1 $P-wan $P-router
box brewhouse lan 192.168.71.10
box office lan 192.168.71.20
box guest lan 192.168.71.30
box website wan 198.51.100.80
for b in router brewhouse office guest website; do
  for i in $(seq 60); do docker exec $P-$b systemctl is-system-running 2>/dev/null | grep -qE 'running|degraded' && break; sleep 1; done
done
python3 demo_driver.py $P
