#!/bin/bash # Prometheus + Grafana for the labsim connectivity matrix. # # Grafana runs with anonymous auth as Admin — NO LOGIN. That is deliberate for # a throwaway lab on localhost; do not copy this into anything reachable. # # ./monitoring-up.sh start exporter + prometheus + grafana # ./monitoring-up.sh --down stop and remove them # # Grafana: http://localhost:3000 (dashboard "labsim — VLAN connectivity matrix") # Prometheus: http://localhost:9090 # Exporter: http://localhost:9101/metrics set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "$SCRIPT_DIR/lib.sh" GRAFANA_PORT="${GRAFANA_PORT:-3000}" PROM_PORT="${PROM_PORT:-9090}" EXPORTER_PORT="${EXPORTER_PORT:-9101}" NET="labsim-mon" if [ "${1:-}" = "--down" ]; then pkill -f "labsim-exporter.py" 2>/dev/null || true podman rm -f labsim-grafana labsim-prometheus >/dev/null 2>&1 || true podman network rm -f "$NET" >/dev/null 2>&1 || true log "monitoring stopped" exit 0 fi command -v podman >/dev/null 2>&1 || die "podman not installed" # --- exporter (on the host: it needs SSH access to the VMs) ---------------- if pgrep -f "labsim-exporter.py" >/dev/null 2>&1; then log "exporter already running on :$EXPORTER_PORT" else log "starting exporter on :$EXPORTER_PORT" nohup "$SCRIPT_DIR/labsim-exporter.py" --port "$EXPORTER_PORT" --interval 15 \ > /tmp/labsim-exporter.log 2>&1 & sleep 3 fi curl -sS --max-time 5 "http://127.0.0.1:${EXPORTER_PORT}/metrics" >/dev/null \ || die "exporter not answering on :$EXPORTER_PORT (see /tmp/labsim-exporter.log)" podman network exists "$NET" 2>/dev/null || podman network create "$NET" >/dev/null # --- prometheus ----------------------------------------------------------- podman rm -f labsim-prometheus >/dev/null 2>&1 || true log "starting prometheus on :$PROM_PORT" podman run -d --name labsim-prometheus --network "$NET" \ -p "${PROM_PORT}:9090" \ -v "$SCRIPT_DIR/monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro,Z" \ --add-host "host.containers.internal:host-gateway" \ docker.io/prom/prometheus:latest >/dev/null # --- grafana (anonymous, no login) ---------------------------------------- podman rm -f labsim-grafana >/dev/null 2>&1 || true log "starting grafana on :$GRAFANA_PORT (anonymous auth — no password)" podman run -d --name labsim-grafana --network "$NET" \ -p "${GRAFANA_PORT}:3000" \ -e GF_AUTH_ANONYMOUS_ENABLED=true \ -e GF_AUTH_ANONYMOUS_ORG_ROLE=Admin \ -e GF_AUTH_DISABLE_LOGIN_FORM=true \ -e GF_AUTH_BASIC_ENABLED=false \ -e GF_SECURITY_ALLOW_EMBEDDING=true \ -e GF_USERS_DEFAULT_THEME=dark \ -v "$SCRIPT_DIR/monitoring/grafana/provisioning:/etc/grafana/provisioning:ro,Z" \ docker.io/grafana/grafana:latest >/dev/null log "waiting for grafana..." for _ in $(seq 1 40); do if curl -sS --max-time 3 "http://127.0.0.1:${GRAFANA_PORT}/api/health" >/dev/null 2>&1; then break fi sleep 3 done echo log "Grafana: http://localhost:${GRAFANA_PORT}/d/labsim-matrix (no login)" log "Prometheus: http://localhost:${PROM_PORT}" log "Exporter: http://localhost:${EXPORTER_PORT}/metrics"