4
0

install-collector.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #!/bin/sh
  2. # =============================================================================
  3. # NetPulse Collector — one-liner installer (Linux server)
  4. #
  5. # Sidecar that probes TCP latency/availability to each router (reading the
  6. # router list from netpulse.db READ-ONLY) and stores its own time-series
  7. # SQLite (raw → 5-min buckets → daily). Companion of the NetPulse app.
  8. #
  9. # Usage:
  10. # curl -fsSL https://raw.githubusercontent.com/gnacho/netpulse/main/install-collector.sh | sh
  11. # sh install-collector.sh --version=1.0.0 --unattended
  12. # sh install-collector.sh --dry-run
  13. # sh install-collector.sh --uninstall [--purge]
  14. #
  15. # Requirements: Linux with systemd, amd64 / arm64 / armv7.
  16. # Verifies sha256 of every download (checksums.txt).
  17. #
  18. # Layout:
  19. # /usr/local/bin/netpulse-collector binary
  20. # /var/lib/netpulse-collector/data metrics.db + state.json
  21. # =============================================================================
  22. set -eu
  23. APP_NAME="netpulse-collector"
  24. GH_REPO="gnacho/netpulse"
  25. BIN_NAME="netpulse-collector"
  26. DEFAULT_PORT="9100"
  27. INSTALL_DIR="/usr/local/bin"
  28. STATE_DIR="/var/lib/$APP_NAME"
  29. SERVICE_NAME="$APP_NAME"
  30. # Where the collector looks for the app's DB (read-only) to learn the routers:
  31. NETPULSE_DB="/var/lib/netpulse/data/netpulse.db"
  32. NETPULSE_VERSION=""; UNATTENDED=0; DRY_RUN=0; UNINSTALL=0; PURGE=0
  33. # ---------------------------------------------------------------- logging ---
  34. if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
  35. C_G=$(printf '\033[32m'); C_R=$(printf '\033[31m'); C_Y=$(printf '\033[33m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m')
  36. else C_G=""; C_R=""; C_Y=""; C_B=""; C_0=""; fi
  37. info() { printf '%s--%s %s\n' "$C_B" "$C_0" "$*"; }
  38. ok() { printf '%s✓%s %s\n' "$C_G" "$C_0" "$*"; }
  39. warn() { printf '%s!%s %s\n' "$C_Y" "$C_0" "$*" >&2; }
  40. err() { printf '%s✗ %s%s\n' "$C_R" "$*" "$C_0" >&2; }
  41. fatal() { _c=$1; shift; err "$*"; exit "$_c"; }
  42. run() { if [ "$DRY_RUN" -eq 1 ]; then info "[dry-run] $*"; else "$@"; fi; }
  43. usage() {
  44. cat <<EOF
  45. NetPulse Collector — installer (TCP latency probes + time-series SQLite)
  46. Usage: sh install-collector.sh [options]
  47. --version=X.Y.Z version to install (default: latest stable release)
  48. --unattended no questions (automatic when there's no TTY)
  49. --dry-run describe each step without touching the system
  50. --uninstall remove service, binary and user (keeps $STATE_DIR)
  51. --purge with --uninstall: also remove data
  52. -h, --help this help
  53. Repo: https://github.com/$GH_REPO
  54. EOF
  55. exit 0
  56. }
  57. for arg in "$@"; do
  58. case "$arg" in
  59. --version=*) NETPULSE_VERSION="${arg#*=}" ;;
  60. --unattended) UNATTENDED=1 ;;
  61. --dry-run) DRY_RUN=1 ;;
  62. --uninstall) UNINSTALL=1 ;;
  63. --purge) PURGE=1 ;;
  64. -h|--help) usage ;;
  65. *) fatal 10 "unknown option: $arg (try --help)" ;;
  66. esac
  67. done
  68. [ -t 0 ] || UNATTENDED=1
  69. # -------------------------------------------------------------- elevation ---
  70. if [ "$(id -u)" -eq 0 ]; then SUDO=""
  71. elif command -v sudo >/dev/null 2>&1; then SUDO="sudo -E"
  72. elif command -v doas >/dev/null 2>&1; then SUDO="doas"
  73. else fatal 22 "I need root (or sudo/doas). Download the script and run it as root."
  74. fi
  75. # --------------------------------------------------------------- uninstall --
  76. if [ "$UNINSTALL" -eq 1 ]; then
  77. info "uninstalling $APP_NAME"
  78. if [ -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then
  79. run $SUDO systemctl stop "$SERVICE_NAME" 2>/dev/null || true
  80. run $SUDO systemctl disable "$SERVICE_NAME" 2>/dev/null || true
  81. run $SUDO rm -f "/etc/systemd/system/$SERVICE_NAME.service"
  82. run $SUDO systemctl daemon-reload
  83. ok "systemd unit removed"
  84. fi
  85. run $SUDO rm -f "$INSTALL_DIR/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME.bak"
  86. ok "binary removed from $INSTALL_DIR"
  87. if id "$APP_NAME" >/dev/null 2>&1; then
  88. run $SUDO userdel "$APP_NAME" 2>/dev/null || warn "could not delete user $APP_NAME"
  89. ok "system user removed"
  90. fi
  91. # Same as install.sh: a leftover login group breaks the next install (#467).
  92. if getent group "$APP_NAME" >/dev/null 2>&1; then
  93. run $SUDO groupdel "$APP_NAME" 2>/dev/null || warn "could not delete group $APP_NAME (still in use?)"
  94. ok "system group removed"
  95. fi
  96. if [ "$PURGE" -eq 1 ]; then
  97. run $SUDO rm -rf "$STATE_DIR"
  98. ok "data removed (--purge)"
  99. else
  100. info "data kept in $STATE_DIR (remove with --purge)"
  101. fi
  102. ok "$APP_NAME uninstalled"
  103. exit 0
  104. fi
  105. # --------------------------------------------------------------- detection --
  106. . /etc/os-release 2>/dev/null || true
  107. OS_PRETTY="${PRETTY_NAME:-$(uname -s)}"
  108. ARCH=$(uname -m)
  109. case "$ARCH" in
  110. x86_64|amd64) GOARCH=amd64 ;;
  111. aarch64|arm64) GOARCH=arm64 ;;
  112. armv7l|armv7) GOARCH=armv7 ;;
  113. *) fatal 20 "unsupported architecture: $ARCH (released: amd64, arm64, armv7)"
  114. esac
  115. if [ ! -d /run/systemd/system ] || ! command -v systemctl >/dev/null 2>&1; then
  116. fatal 23 "the collector needs systemd (this machine doesn't run it)"
  117. fi
  118. info "detected: $OS_PRETTY · linux/$GOARCH · systemd"
  119. if command -v curl >/dev/null 2>&1; then FETCH="curl -fsSL --retry 3 --connect-timeout 10"
  120. elif command -v wget >/dev/null 2>&1; then FETCH="wget -q -O-"
  121. else fatal 21 "I need curl or wget"
  122. fi
  123. fetch_to() { $FETCH "$1" > "$2"; }
  124. command -v sha256sum >/dev/null 2>&1 || fatal 21 "missing sha256sum (coreutils)"
  125. # ------------------------------------------------------ port pre-flight -----
  126. port_in_use() {
  127. if command -v ss >/dev/null 2>&1; then
  128. ss -tln 2>/dev/null | awk '{print $4}' | grep -qE "[:.]${1}\$"
  129. elif command -v netstat >/dev/null 2>&1; then
  130. netstat -tln 2>/dev/null | awk '{print $4}' | grep -qE "[:.]${1}\$"
  131. else
  132. return 1 # can't check: assume free
  133. fi
  134. }
  135. pick_port() {
  136. _p=$1; _end=$((_p + 20))
  137. while [ "$_p" -le "$_end" ]; do
  138. if ! port_in_use "$_p"; then printf '%s' "$_p"; return 0; fi
  139. _p=$((_p + 1))
  140. done
  141. return 1
  142. }
  143. # Fresh install only: an upgrade keeps the port from the existing unit.
  144. tty_ok() { (exec 3<>/dev/tty) 2>/dev/null; }
  145. # choose_port WANT — interactive (TTY): asks, suggesting the next free port.
  146. # Non-interactive: prints the next free one. Fails if none is free.
  147. choose_port() {
  148. _want=$1
  149. _next=$(pick_port "$((_want + 1))") || _next=""
  150. if [ "$UNATTENDED" -eq 0 ] && tty_ok; then
  151. while :; do
  152. printf 'Port %s is already in use.\nWhich port should the collector listen on? [%s] ' \
  153. "$_want" "${_next:-none free}" > /dev/tty
  154. IFS= read -r _r < /dev/tty || _r=""
  155. _r="${_r:-$_next}"
  156. case "$_r" in
  157. ''|*[!0-9]*) printf 'Please enter a port number.\n' > /dev/tty; continue ;;
  158. esac
  159. if [ "$_r" -lt 1 ] || [ "$_r" -gt 65535 ]; then
  160. printf 'Out of range (1-65535).\n' > /dev/tty; continue
  161. fi
  162. if port_in_use "$_r"; then
  163. printf 'Port %s is also in use.\n' "$_r" > /dev/tty; continue
  164. fi
  165. printf '%s' "$_r"; return 0
  166. done
  167. fi
  168. [ -n "$_next" ] || return 1
  169. printf '%s' "$_next"
  170. }
  171. PORT="$DEFAULT_PORT"
  172. if [ ! -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then
  173. if port_in_use "$DEFAULT_PORT"; then
  174. PORT=$(choose_port "$DEFAULT_PORT") \
  175. || fatal 25 "port $DEFAULT_PORT is busy and no free port found in $((DEFAULT_PORT + 1))-$((DEFAULT_PORT + 21))"
  176. warn "port $DEFAULT_PORT is already in use — the collector will listen on $PORT instead"
  177. else
  178. ok "port $DEFAULT_PORT is free"
  179. fi
  180. fi
  181. # --------------------------------------------------------- resolve version --
  182. if [ -z "$NETPULSE_VERSION" ]; then
  183. info "resolving latest stable version"
  184. NETPULSE_VERSION=$($FETCH "https://api.github.com/repos/$GH_REPO/releases/latest" \
  185. | grep -oE '"tag_name"[[:space:]]*:[[:space:]]*"[^"]*"' | head -1 | cut -d'"' -f4) \
  186. || fatal 31 "could not resolve the latest version. Use --version=X.Y.Z"
  187. [ -n "$NETPULSE_VERSION" ] || fatal 31 "no stable release found yet. Use --version=X.Y.Z"
  188. fi
  189. VERSION_NORM=$(echo "$NETPULSE_VERSION" | sed 's/^v//')
  190. case "$NETPULSE_VERSION" in
  191. v*) NETPULSE_TAG="$NETPULSE_VERSION" ;;
  192. *) NETPULSE_TAG="v$NETPULSE_VERSION" ;;
  193. esac
  194. ASSET="${BIN_NAME}_${VERSION_NORM}_linux_${GOARCH}.tar.gz"
  195. BASE_URL="https://github.com/$GH_REPO/releases/download/$NETPULSE_TAG"
  196. info "version: $NETPULSE_VERSION"
  197. $FETCH "https://github.com/$GH_REPO" >/dev/null \
  198. || fatal 30 "no access to github.com (proxy? DNS? firewall?)"
  199. UPGRADING=0
  200. if [ -x "$INSTALL_DIR/$BIN_NAME" ]; then
  201. UPGRADING=1
  202. info "previous install detected: upgrade mode (data is preserved)"
  203. fi
  204. # ---------------------------------------------------------- download+verify --
  205. TMP=$(mktemp -d) || fatal 34 "mktemp failed"
  206. cleanup() { rm -rf "$TMP"; return 0; }
  207. trap cleanup EXIT INT TERM
  208. info "downloading $ASSET"
  209. fetch_to "$BASE_URL/$ASSET" "$TMP/$ASSET" || fatal 32 "download failed: $BASE_URL/$ASSET"
  210. fetch_to "$BASE_URL/checksums.txt" "$TMP/checksums.txt" || fatal 32 "checksums.txt not found in release $NETPULSE_VERSION"
  211. [ -s "$TMP/$ASSET" ] || fatal 32 "downloaded asset is empty"
  212. SUM_FILE=$(sha256sum "$TMP/$ASSET" | awk '{print $1}')
  213. SUM_REF=$(grep " $ASSET\$" "$TMP/checksums.txt" | awk '{print $1}')
  214. [ -n "$SUM_REF" ] || fatal 33 "$ASSET not listed in checksums.txt"
  215. [ "$SUM_FILE" = "$SUM_REF" ] || fatal 33 "sha256 MISMATCH for $ASSET — corrupt or tampered download"
  216. ok "sha256 verified"
  217. tar -tzf "$TMP/$ASSET" >/dev/null 2>&1 || fatal 34 "tarball is corrupt"
  218. tar -xzf "$TMP/$ASSET" -C "$TMP"
  219. [ -s "$TMP/$BIN_NAME" ] || fatal 34 "tarball does not contain $BIN_NAME"
  220. # ------------------------------------------------------------------ install --
  221. run $SUDO install -d -m 0755 "$INSTALL_DIR"
  222. if [ "$UPGRADING" -eq 1 ]; then
  223. run $SUDO cp -a "$INSTALL_DIR/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME.bak"
  224. info "previous binary backed up: $INSTALL_DIR/$BIN_NAME.bak"
  225. fi
  226. run $SUDO install -T -m 0755 "$TMP/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME"
  227. ok "binary installed at $INSTALL_DIR/$BIN_NAME"
  228. if ! id "$APP_NAME" >/dev/null 2>&1; then
  229. # Reuse a leftover login group from a previous uninstall (#467).
  230. if getent group "$APP_NAME" >/dev/null 2>&1; then
  231. run $SUDO useradd --system -g "$APP_NAME" --home-dir "$STATE_DIR" --shell /usr/sbin/nologin "$APP_NAME"
  232. else
  233. run $SUDO useradd --system --home-dir "$STATE_DIR" --shell /usr/sbin/nologin "$APP_NAME"
  234. fi
  235. ok "system user $APP_NAME created"
  236. fi
  237. run $SUDO install -d -m 0750 -o "$APP_NAME" -g "$APP_NAME" "$STATE_DIR"
  238. # Read access to the app's DB: join the netpulse group if it exists
  239. if getent group netpulse >/dev/null 2>&1; then
  240. run $SUDO usermod -a -G netpulse "$APP_NAME" 2>/dev/null || true
  241. ok "user $APP_NAME added to group netpulse (read access to netpulse.db)"
  242. else
  243. info "no netpulse group found: set NETPULSE_DB in the unit if the app's DB lives elsewhere"
  244. fi
  245. # SELinux: context fix, degrade to warning
  246. if command -v getenforce >/dev/null 2>&1 && [ "$(getenforce)" = "Enforcing" ]; then
  247. run $SUDO chcon -t bin_t "$INSTALL_DIR/$BIN_NAME" 2>/dev/null \
  248. || warn "SELinux Enforcing: if the service fails, check 'ausearch -m avc -ts recent'"
  249. fi
  250. # ----------------------------------------------------------------- service --
  251. if [ "$DRY_RUN" -eq 1 ]; then info "[dry-run] would write systemd unit + enable --now"; else
  252. $SUDO tee "/etc/systemd/system/$SERVICE_NAME.service" >/dev/null <<EOF
  253. [Unit]
  254. Description=NetPulse Collector — TCP latency probes to routers (time-series)
  255. After=network-online.target
  256. Wants=network-online.target
  257. [Service]
  258. Type=simple
  259. User=$APP_NAME
  260. Group=$APP_NAME
  261. Environment=DATA_DIR=$STATE_DIR/data
  262. Environment=NETPULSE_DB=$NETPULSE_DB
  263. Environment=LISTEN=127.0.0.1:$PORT
  264. ExecStart=$INSTALL_DIR/$BIN_NAME
  265. Restart=on-failure
  266. RestartSec=5
  267. NoNewPrivileges=true
  268. ProtectSystem=strict
  269. ProtectHome=read-only
  270. PrivateTmp=true
  271. LockPersonality=true
  272. RemoveIPC=true
  273. StateDirectory=$APP_NAME
  274. UMask=007
  275. [Install]
  276. WantedBy=multi-user.target
  277. EOF
  278. fi
  279. run $SUDO systemctl daemon-reload
  280. if [ "$UPGRADING" -eq 1 ]; then run $SUDO systemctl restart "$SERVICE_NAME"
  281. else run $SUDO systemctl enable --now "$SERVICE_NAME"; fi
  282. if [ "$DRY_RUN" -eq 0 ]; then
  283. sleep 3
  284. if ! $SUDO systemctl is-active --quiet "$SERVICE_NAME"; then
  285. err "service failed to start; diagnostics:"
  286. $SUDO systemctl status "$SERVICE_NAME" --no-pager || true
  287. $SUDO journalctl -u "$SERVICE_NAME" -n 50 --no-pager || true
  288. exit 40
  289. fi
  290. ok "service $SERVICE_NAME active"
  291. if command -v curl >/dev/null 2>&1; then
  292. curl -fsS --max-time 5 "http://127.0.0.1:$PORT/healthz" >/dev/null 2>&1 \
  293. && ok "healthz OK on 127.0.0.1:$PORT" \
  294. || warn "service is up but healthz didn't answer yet (give it a few seconds)"
  295. fi
  296. fi
  297. # ------------------------------------------------------------------ summary --
  298. printf '\n%s================ %s installed ================%s\n' "$C_G" "$APP_NAME" "$C_0"
  299. printf 'Version: %s%s\n' "$NETPULSE_VERSION" "$( [ "$UPGRADING" -eq 1 ] && echo " (upgrade — previous binary at $INSTALL_DIR/$BIN_NAME.bak)" || true)"
  300. printf 'Binary: %s\n' "$INSTALL_DIR/$BIN_NAME"
  301. printf 'Data: %s/data (metrics.db, state.json)\n' "$STATE_DIR"
  302. printf 'Health: http://127.0.0.1:%s/healthz (localhost only)\n' "$PORT"
  303. printf 'Routers DB: %s (read-only)\n' "$NETPULSE_DB"
  304. printf '\nUseful commands:\n'
  305. printf ' systemctl status %s\n journalctl -u %s -f\n' "$SERVICE_NAME" "$SERVICE_NAME"
  306. printf ' sh install-collector.sh # update to the latest stable version\n'
  307. printf ' sh install-collector.sh --uninstall\n'
  308. printf '\nNote: the health endpoint binds to localhost only — no firewall changes.\n'
  309. printf '%s================================================%s\n\n' "$C_G" "$C_0"