Network monitor: hub, probes, Docker and native installers
Hub (FastAPI + no-build web UI): - Network health: live probe/target matrix, status strips, incidents - Probes: add (with install command), rename, remove, latency and throughput charts - Clients merged from AdGuard, DHCP, query log and probe ARP tables; names, categories, suspicious flag, notes, per-client DNS requests - Query log, domain detail (clients, related subdomains), blocked domains via the AdGuard Home /control API Probe: tests peers (ping, HTTP, throughput), gateway, LAN and public DNS, internet URLs and an optional VPS; buffers results while the hub is down. Gateway and ARP from /proc/net on Linux, route/arp on macOS. Install: install.sh (hub) and install-probe.sh (generated from it), each in docker mode (compose; default on Linux hosts) or native mode (venv + systemd or launchd; default on macOS and in LXC). Both modes deploy with an update command that checks the new commit and rolls back on failure. CLAUDE.md carries shared context for Claude Code sessions on each machine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
54a765510a
63 changed files with 6464 additions and 0 deletions
144
native/setup.sh
Executable file
144
native/setup.sh
Executable file
|
|
@ -0,0 +1,144 @@
|
|||
#!/usr/bin/env bash
|
||||
# Native (no Docker) setup, run by install.sh / install-probe.sh after they clone the repo
|
||||
# and write .env. Idempotent. Linux: run as root, needs systemd. macOS: run as the user
|
||||
# who will own the install (uses sudo for the launchd daemon and the update command).
|
||||
# NAME=network-probe ROLE=probe INSTALL_DIR=/opt/network-probe bash native/setup.sh
|
||||
set -euo pipefail
|
||||
: "${NAME:?}" "${ROLE:?}" "${INSTALL_DIR:?}"
|
||||
. "$INSTALL_DIR/native/lib.sh"
|
||||
SVC_USER_LINUX=netmon
|
||||
MIN_PY=11
|
||||
|
||||
as_root() { if [ "$(id -u)" = 0 ]; then "$@"; else sudo "$@"; fi; }
|
||||
|
||||
find_python() {
|
||||
local c v
|
||||
for c in python3.14 python3.13 python3.12 python3.11 python3 \
|
||||
/opt/homebrew/bin/python3 /usr/local/bin/python3 /opt/homebrew/opt/python@3.13/bin/python3.13; do
|
||||
command -v "$c" >/dev/null 2>&1 || continue
|
||||
v=$("$c" -c 'import sys; print(sys.version_info[0] == 3 and sys.version_info[1] >= '"$MIN_PY"')' 2>/dev/null) || continue
|
||||
if [ "$v" = True ] && "$c" -c 'import venv, ensurepip' >/dev/null 2>&1; then command -v "$c"; return 0; fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
ensure_python() {
|
||||
PYTHON=$(find_python) && return 0
|
||||
if [ "$PLATFORM" = launchd ]; then
|
||||
if command -v brew >/dev/null 2>&1; then
|
||||
native_log "installing Python 3.13 with Homebrew"
|
||||
brew install python@3.13 >/dev/null
|
||||
else
|
||||
native_die "needs Python 3.$MIN_PY+. Install Homebrew (https://brew.sh) and re-run, or install Python from python.org"
|
||||
fi
|
||||
elif command -v apt-get >/dev/null 2>&1; then
|
||||
native_log "installing python3 and python3-venv"
|
||||
DEBIAN_FRONTEND=noninteractive apt-get update -qq
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq python3 python3-venv >/dev/null
|
||||
elif command -v dnf >/dev/null 2>&1; then
|
||||
dnf install -y -q python3
|
||||
elif command -v apk >/dev/null 2>&1; then
|
||||
apk add -q python3
|
||||
fi
|
||||
PYTHON=$(find_python) || native_die "needs Python 3.$MIN_PY+ with venv; this system's python3 is $(python3 --version 2>&1 || echo missing). Use a newer distro (Debian 12+, Ubuntu 24.04+) or INSTALL_MODE=docker"
|
||||
}
|
||||
|
||||
if [ "$(uname -s)" = Darwin ]; then
|
||||
PLATFORM=launchd
|
||||
SERVICE="dev.neilbantoc.$NAME"
|
||||
SVC_USER=$(id -un)
|
||||
[ "$SVC_USER" != root ] || native_die "on macOS, run the installer as your own user, not with sudo"
|
||||
else
|
||||
PLATFORM=systemd
|
||||
SERVICE=$NAME
|
||||
SVC_USER=$SVC_USER_LINUX
|
||||
[ "$(id -u)" = 0 ] || native_die "native mode on Linux runs as root"
|
||||
[ -d /run/systemd/system ] || native_die "native mode on Linux needs systemd (this system has none). Use INSTALL_MODE=docker"
|
||||
fi
|
||||
|
||||
native_log "python"
|
||||
ensure_python
|
||||
native_log "using $PYTHON ($("$PYTHON" --version 2>&1))"
|
||||
sync_venv "$INSTALL_DIR" "$PYTHON" || native_die "installing requirements failed"
|
||||
|
||||
mkdir -p "$INSTALL_DIR/data" "$INSTALL_DIR/.native-state"
|
||||
if [ "$PLATFORM" = systemd ]; then
|
||||
id "$SVC_USER" >/dev/null 2>&1 || useradd --system --user-group --home-dir "$INSTALL_DIR/data" --shell /usr/sbin/nologin "$SVC_USER"
|
||||
chown -R "$SVC_USER:$SVC_USER" "$INSTALL_DIR/data"
|
||||
chown root:"$SVC_USER" "$INSTALL_DIR/.env"
|
||||
chmod 640 "$INSTALL_DIR/.env"
|
||||
else
|
||||
chmod 600 "$INSTALL_DIR/.env"
|
||||
fi
|
||||
|
||||
cat > "$INSTALL_DIR/.native" <<CONF
|
||||
NAME=$NAME
|
||||
ROLE=$ROLE
|
||||
PLATFORM=$PLATFORM
|
||||
SERVICE=$SERVICE
|
||||
SVC_USER=$SVC_USER
|
||||
PYTHON=$PYTHON
|
||||
CONF
|
||||
|
||||
# The <name>-update command runs a copy of update.sh (update replaces the checkout under it).
|
||||
cp "$INSTALL_DIR/native/update.sh" "$INSTALL_DIR/.native-update.sh"
|
||||
chmod 755 "$INSTALL_DIR/.native-update.sh"
|
||||
tmp=$(mktemp)
|
||||
printf '#!/bin/sh\n# Deploy a new commit of %s (native install). See: %s-update --help\nNETMON_DIR=%s exec /bin/bash %s/.native-update.sh "$@"\n' \
|
||||
"$NAME" "$NAME" "$INSTALL_DIR" "$INSTALL_DIR" > "$tmp"
|
||||
as_root mkdir -p /usr/local/bin
|
||||
as_root install -m 755 "$tmp" "/usr/local/bin/$NAME-update"
|
||||
rm -f "$tmp"
|
||||
|
||||
native_log "service"
|
||||
if [ "$PLATFORM" = systemd ]; then
|
||||
cat > "/etc/systemd/system/$SERVICE.service" <<UNIT
|
||||
[Unit]
|
||||
Description=Network Monitor $ROLE ($NAME)
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
User=$SVC_USER
|
||||
Group=$SVC_USER
|
||||
WorkingDirectory=$INSTALL_DIR
|
||||
ExecStart=/bin/bash $INSTALL_DIR/native/run.sh
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
NoNewPrivileges=yes
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
UNIT
|
||||
systemctl daemon-reload
|
||||
systemctl enable "$SERVICE" >/dev/null 2>&1
|
||||
systemctl restart "$SERVICE"
|
||||
else
|
||||
plist=$(mktemp)
|
||||
cat > "$plist" <<PLIST
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key><string>$SERVICE</string>
|
||||
<key>UserName</key><string>$SVC_USER</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array><string>/bin/bash</string><string>$INSTALL_DIR/native/run.sh</string></array>
|
||||
<key>WorkingDirectory</key><string>$INSTALL_DIR</string>
|
||||
<key>EnvironmentVariables</key>
|
||||
<dict><key>PATH</key><string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string></dict>
|
||||
<key>RunAtLoad</key><true/>
|
||||
<key>KeepAlive</key><true/>
|
||||
<key>ThrottleInterval</key><integer>5</integer>
|
||||
<key>StandardOutPath</key><string>$INSTALL_DIR/data/service.log</string>
|
||||
<key>StandardErrorPath</key><string>$INSTALL_DIR/data/service.log</string>
|
||||
</dict>
|
||||
</plist>
|
||||
PLIST
|
||||
as_root install -m 644 -o root -g wheel "$plist" "/Library/LaunchDaemons/$SERVICE.plist"
|
||||
rm -f "$plist"
|
||||
as_root launchctl bootout "system/$SERVICE" >/dev/null 2>&1 || true
|
||||
as_root launchctl bootstrap system "/Library/LaunchDaemons/$SERVICE.plist"
|
||||
as_root launchctl enable "system/$SERVICE" >/dev/null 2>&1 || true
|
||||
fi
|
||||
native_log "setup done ($PLATFORM service $SERVICE, user $SVC_USER)"
|
||||
Loading…
Add table
Add a link
Reference in a new issue