demo_monitoring.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env bash
  2. # Run the synthetic multi-site monitoring demo in labs/demo-monitoring.
  3. # Every metric is invented by a generator container. Nothing here touches a
  4. # real host or cloud account.
  5. #
  6. # scripts/demo_monitoring.sh up start Prometheus, Grafana and the generator
  7. # scripts/demo_monitoring.sh silent-site [S] make site S (default site-tor2) stop answering
  8. # scripts/demo_monitoring.sh restore-site let every site answer again
  9. # scripts/demo_monitoring.sh restart-prometheus apply an edit to a rule file (only the target file is watched)
  10. # scripts/demo_monitoring.sh test promtool: check config, run the alert unit tests
  11. # scripts/demo_monitoring.sh screenshots print the capture plan (viewport, URLs, file names)
  12. # scripts/demo_monitoring.sh status show the containers and the alert state
  13. # scripts/demo_monitoring.sh down stop everything and delete the demo volumes
  14. set -euo pipefail
  15. ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  16. LAB="$ROOT/labs/demo-monitoring"
  17. PROM_IMAGE="docker.io/prom/prometheus:v3.5.0"
  18. die() { echo "demo_monitoring: $*" >&2; exit 1; }
  19. compose() {
  20. if docker compose version >/dev/null 2>&1; then
  21. docker compose --project-directory "$LAB" -f "$LAB/docker-compose.yml" "$@"
  22. elif command -v docker-compose >/dev/null 2>&1; then
  23. docker-compose --project-directory "$LAB" -f "$LAB/docker-compose.yml" "$@"
  24. else
  25. die "neither 'docker compose' nor 'docker-compose' is available"
  26. fi
  27. }
  28. need_docker() {
  29. command -v docker >/dev/null 2>&1 || die "docker is not installed"
  30. docker info >/dev/null 2>&1 || die "docker is not usable by this user (no sudo is used here)"
  31. }
  32. load_ports() {
  33. # Read the optional port overrides without sourcing the file (no code execution).
  34. GRAFANA_PORT=3300
  35. PROM_PORT=9190
  36. if [ -f "$LAB/.env" ]; then
  37. local v
  38. v="$(sed -n 's/^DEMO_GRAFANA_PORT=//p' "$LAB/.env" | tail -n 1)"
  39. if [ -n "$v" ]; then GRAFANA_PORT="$v"; fi
  40. v="$(sed -n 's/^DEMO_PROMETHEUS_PORT=//p' "$LAB/.env" | tail -n 1)"
  41. if [ -n "$v" ]; then PROM_PORT="$v"; fi
  42. fi
  43. }
  44. # The one placeholder rule lives in scripts/secret_rule.py. The value goes in on
  45. # standard input (never in a process list) and is never printed; a rejection
  46. # names the variable only.
  47. check_secret() {
  48. command -v python3 >/dev/null 2>&1 || die "python3 is needed to check $1"
  49. printf '%s' "$2" | python3 -I "$ROOT/scripts/secret_rule.py" "$1" >&2
  50. }
  51. # GRAFANA_ADMIN_PASSWORD is required. A value from the shell environment (which
  52. # compose reads first) or from the untracked .env must pass the rule or the demo
  53. # refuses to start; it is never rewritten. Only an empty or missing value gets a
  54. # random one, written to .env without touching the other lines in it.
  55. ensure_env() {
  56. local fileval=""
  57. if [ -n "${GRAFANA_ADMIN_PASSWORD:-}" ]; then
  58. check_secret GRAFANA_ADMIN_PASSWORD "$GRAFANA_ADMIN_PASSWORD" ||
  59. die "refusing to start: the GRAFANA_ADMIN_PASSWORD in your shell environment is not usable"
  60. return 0
  61. fi
  62. if [ -f "$LAB/.env" ]; then
  63. fileval="$(sed -n 's/^GRAFANA_ADMIN_PASSWORD=//p' "$LAB/.env" | tail -n 1)"
  64. fi
  65. if [ -n "$fileval" ]; then
  66. check_secret GRAFANA_ADMIN_PASSWORD "$fileval" ||
  67. die "refusing to start: the GRAFANA_ADMIN_PASSWORD in $LAB/.env is not usable (clear it to get a random one)"
  68. return 0
  69. fi
  70. local pw
  71. pw="$(head -c 24 /dev/urandom | base64 | tr -d '/+=\n' | head -c 24)"
  72. if [ -f "$LAB/.env" ]; then
  73. if grep -q '^GRAFANA_ADMIN_PASSWORD=' "$LAB/.env"; then
  74. sed -i "s/^GRAFANA_ADMIN_PASSWORD=.*/GRAFANA_ADMIN_PASSWORD=${pw}/" "$LAB/.env"
  75. else
  76. printf 'GRAFANA_ADMIN_PASSWORD=%s\n' "$pw" >>"$LAB/.env"
  77. fi
  78. else
  79. (umask 077 && cp "$LAB/env.example" "$LAB/.env")
  80. # Replace the empty value; the password is random and lives only in the untracked .env.
  81. sed -i "s/^GRAFANA_ADMIN_PASSWORD=.*/GRAFANA_ADMIN_PASSWORD=${pw}/" "$LAB/.env"
  82. fi
  83. echo "Wrote a random Grafana admin password to $LAB/.env (untracked)."
  84. }
  85. # Say where the Grafana admin password comes from, never what it is. Compose
  86. # gives the shell environment precedence over the .env file, so check it first.
  87. password_hint() {
  88. if [ -n "${GRAFANA_ADMIN_PASSWORD:-}" ]; then
  89. echo "password: your shell environment (GRAFANA_ADMIN_PASSWORD)"
  90. else
  91. echo "password: in $LAB/.env (GRAFANA_ADMIN_PASSWORD)"
  92. fi
  93. }
  94. cmd_up() {
  95. need_docker
  96. ensure_env
  97. load_ports
  98. compose up -d
  99. echo
  100. echo "Grafana http://127.0.0.1:${GRAFANA_PORT} user: admin $(password_hint)"
  101. echo "Prometheus http://127.0.0.1:${PROM_PORT}"
  102. echo "Give it two minutes to collect a few scrapes, then open the dashboard CAH: Sites overview."
  103. }
  104. cmd_down() {
  105. need_docker
  106. ensure_env
  107. compose down -v
  108. rm -f "$LAB/state/silent"
  109. }
  110. cmd_restart_prometheus() {
  111. need_docker
  112. ensure_env
  113. load_ports
  114. # Rule files are read at start; the lab has no lifecycle reload endpoint, and
  115. # only prometheus/targets.json (file service discovery) is re-read on its own.
  116. compose restart prometheus
  117. echo "Prometheus restarted; it re-read prometheus.yml and the rule files."
  118. }
  119. cmd_silent() {
  120. local site="${1:-site-tor2}"
  121. case "$site" in
  122. site-tor1 | site-tor2) ;;
  123. *) die "unknown site '$site' (the demo has site-tor1 and site-tor2)" ;;
  124. esac
  125. mkdir -p "$LAB/state"
  126. printf '%s\n' "$site" >"$LAB/state/silent"
  127. chmod 644 "$LAB/state/silent"
  128. echo "$site now answers every scrape with HTTP 503. Watch up == 0, then CloudSiteSilent after about a minute."
  129. }
  130. cmd_restore() {
  131. rm -f "$LAB/state/silent"
  132. echo "Every site answers again."
  133. }
  134. cmd_test() {
  135. need_docker
  136. docker run --rm --entrypoint promtool -v "$LAB/prometheus:/etc/prometheus:ro" "$PROM_IMAGE" \
  137. check config /etc/prometheus/prometheus.yml
  138. docker run --rm --entrypoint promtool -v "$LAB/prometheus:/etc/prometheus:ro" "$PROM_IMAGE" \
  139. test rules /etc/prometheus/tests/alerts.test.yml /etc/prometheus/tests/alerts-site-removed.test.yml
  140. }
  141. cmd_status() {
  142. need_docker
  143. ensure_env
  144. load_ports
  145. compose ps
  146. echo
  147. curl -fsS "http://127.0.0.1:${PROM_PORT}/api/v1/alerts" || echo "Prometheus is not answering on ${PROM_PORT}."
  148. echo
  149. }
  150. cmd_screenshots() {
  151. load_ports
  152. cat <<EOF
  153. Screenshot plan (headless browser, fixed viewport 1440x900, strip PNG metadata, keep each file <= 600 KB).
  154. Take the Grafana ones after logging in as admin. Never put the password in a screenshot or a file.
  155. 1 http://127.0.0.1:${GRAFANA_PORT}/d/cah-sites-overview?from=now-15m&to=now demo-sites-overview-healthy.png
  156. 2 http://127.0.0.1:${GRAFANA_PORT}/d/cah-droplet-health?from=now-15m&to=now&var-site=site-tor1&var-instance=192.0.2.11 demo-droplet-health.png
  157. 3 http://127.0.0.1:${PROM_PORT}/targets demo-prometheus-targets.png
  158. then: $0 silent-site site-tor2 (wait about 2 minutes)
  159. 4 http://127.0.0.1:${GRAFANA_PORT}/d/cah-sites-overview?from=now-15m&to=now demo-sites-overview-silent.png
  160. 5 http://127.0.0.1:${PROM_PORT}/alerts demo-prometheus-alerts-firing.png
  161. finally: $0 restore-site
  162. EOF
  163. }
  164. main() {
  165. local sub="${1:-}"
  166. if [ "$#" -gt 0 ]; then shift; fi
  167. case "$sub" in
  168. up) cmd_up ;;
  169. down) cmd_down ;;
  170. silent-site) cmd_silent "$@" ;;
  171. restore-site) cmd_restore ;;
  172. restart-prometheus) cmd_restart_prometheus ;;
  173. test) cmd_test ;;
  174. status) cmd_status ;;
  175. screenshots) cmd_screenshots ;;
  176. *)
  177. sed -n '2,14p' "${BASH_SOURCE[0]}"
  178. [ -z "$sub" ] || die "unknown subcommand '$sub'"
  179. ;;
  180. esac
  181. }
  182. # Run only when executed, so the tests can source the functions.
  183. if [ "${BASH_SOURCE[0]}" = "$0" ]; then
  184. main "$@"
  185. fi