| 1 | 1 | new file mode 100644 |
| index 0000000..e7f212f | ||
| --- /dev/null | ||
| +++ b/.gitignore | ||
| @@ -0,0 +1,9 @@ | ||
| 1 | +/plugin_sysmon | |
| 2 | +/plugin_sysmon.exe | |
| 3 | +*.o | |
| 4 | +*.obj | |
| 5 | +bin/ | |
| 6 | +*.tmp | |
| 7 | +*.log | |
| 8 | +/plugin_sysmon.musl | |
| 9 | +/plugin_sysmon.macos-* |
| 1 | 1 | new file mode 100644 |
| index 0000000..ef80120 | ||
| --- /dev/null | ||
| +++ b/LICENSE | ||
| @@ -0,0 +1,21 @@ | ||
| 1 | +MIT License | |
| 2 | + | |
| 3 | +Copyright (c) 2026 SCORPIOX INC | |
| 4 | + | |
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 6 | +of this software and associated documentation files (the "Software"), to deal | |
| 7 | +in the Software without restriction, including without limitation the rights | |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 9 | +copies of the Software, and to permit persons to whom the Software is | |
| 10 | +furnished to do so, subject to the following conditions: | |
| 11 | + | |
| 12 | +The above copyright notice and this permission notice shall be included in all | |
| 13 | +copies or substantial portions of the Software. | |
| 14 | + | |
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 21 | +SOFTWARE. |
| 1 | 1 | new file mode 100644 |
| index 0000000..e9bb963 | ||
| --- /dev/null | ||
| +++ b/README.md | ||
| @@ -0,0 +1,162 @@ | ||
| 1 | +# plugin_sysmon — ScorpioX Bot open-source local system monitor plugin | |
| 2 | + | |
| 3 | +A clean-room, MIT-licensed [ScorpioX Bot](https://bot.scorpiox.net) dynamic plugin that shows | |
| 4 | +what **your own node** is doing right now: CPU utilization overall and per core, a rolling | |
| 5 | +history sparkline, RAM and swap, load average, uptime, and the top processes by CPU or memory. | |
| 6 | + | |
| 7 | +One C99 file. No dependencies. No daemon of its own. No outbound network requests. | |
| 8 | + | |
| 9 | +``` | |
| 10 | +plugin_sysmon --meta | |
| 11 | +{"name":"sysmon","desc":"CPU & memory"} | |
| 12 | +``` | |
| 13 | + | |
| 14 | +--- | |
| 15 | + | |
| 16 | +## What it does | |
| 17 | + | |
| 18 | +| Area | Detail | | |
| 19 | +|---|---| | |
| 20 | +| **CPU** | Two counter samples 80 ms apart, so the figure is a live percentage rather than an average since boot. Linux reads `/proc/stat`, macOS reads `host_processor_info`, Windows reads `GetSystemTimes`. | | |
| 21 | +| **Per-core** | One bar per logical core on Linux and macOS. Windows exposes an aggregate figure only, and the UI says so rather than showing a fake bar. | | |
| 22 | +| **History** | The dashboard keeps the last 60 samples in the page and draws them as a polyline SVG. Nothing is cached on disk. | | |
| 23 | +| **Memory** | Total, used, available and cached, plus swap. Linux uses `MemAvailable` (not `MemFree`) so the number matches what `free` reports. | | |
| 24 | +| **Load / uptime** | `/proc/loadavg` + `/proc/uptime` on Linux, `getloadavg` + `kern.boottime` on macOS, `GetTickCount64` on Windows. | | |
| 25 | +| **Processes** | Top 25 by CPU or by RSS, with client-side filter. CPU comes from a per-process tick delta over the same 80 ms window, not from a `ps` column. | | |
| 26 | +| **Polling** | 2 s / 5 s / Pause, default 2 s. Polling stops when the tab is hidden and resumes on focus. | | |
| 27 | +| **Theme** | Follows the Bot theme (`localStorage.bot_theme`), dark by default, no flash. | | |
| 28 | +| **Embed mode** | `?embed=1` drops the back-link so it fits the chat floating window. | | |
| 29 | + | |
| 30 | +### Why it is safe | |
| 31 | + | |
| 32 | +**1. Node authentication.** The node API on `:8150` is reachable from the LAN, so the plugin | |
| 33 | +refuses any request that does not carry the node password. Accepted forms match the rest of | |
| 34 | +the node API: `X-Bot-Password: <pwd>` (what `bot-web` proxies with), `Authorization: Bearer | |
| 35 | +<pwd>`, or `?pwd=<pwd>` (what the plugin iframe appends). If `BOT_PASSWORD` is unset the node | |
| 36 | +is running without a password, which is a deliberate local-dev setup only. `--meta` stays | |
| 37 | +unauthenticated so discovery keeps working. | |
| 38 | + | |
| 39 | +**2. No user input reaches a shell.** The only subprocess in the whole program is a fixed | |
| 40 | +`ps -Ao pid=,pcpu=,rss=,comm=` on macOS, where the kernel offers no cheap per-process | |
| 41 | +iterator. Every string it consumes comes from the kernel, not from the request. Query | |
| 42 | +parameters are parsed into fixed-size buffers and are only ever compared to literals. | |
| 43 | + | |
| 44 | +**3. No sockets.** The plugin makes zero outbound connections. It never resolves a hostname, | |
| 45 | +never opens a port, and never reads a credential. | |
| 46 | + | |
| 47 | +**4. `/proc` is streamed, never measured.** `st_size` for `/proc/stat` and `/proc/meminfo` is | |
| 48 | +always 0, so the code reads with `fgets` until EOF. Seeking to the end and asking for the size | |
| 49 | +is a classic bug that silently yields an empty buffer. | |
| 50 | + | |
| 51 | +--- | |
| 52 | + | |
| 53 | +## Install | |
| 54 | + | |
| 55 | +### Prebuilt (fastest) | |
| 56 | + | |
| 57 | +```bash | |
| 58 | +curl -fsSL https://dist.scorpiox.net/bot-plugins/sysmon/latest.json | |
| 59 | +``` | |
| 60 | + | |
| 61 | +Pick the target that matches the node, then place the binary in the node plugin directory: | |
| 62 | + | |
| 63 | +| Platform | Directory | | |
| 64 | +|---|---| | |
| 65 | +| Linux / macOS | `~/.scorpiox/plugins/`, or `/workspace/.scorpiox/plugins/` in a BotCloud container | | |
| 66 | +| Windows | `%LOCALAPPDATA%\scorpiox\plugins\` | | |
| 67 | + | |
| 68 | +```bash | |
| 69 | +mkdir -p ~/.scorpiox/plugins | |
| 70 | +chmod +x plugin_sysmon | |
| 71 | +mv plugin_sysmon ~/.scorpiox/plugins/ | |
| 72 | +~/.scorpiox/plugins/plugin_sysmon --meta | |
| 73 | +``` | |
| 74 | + | |
| 75 | +No restart is needed — `scorpiox-server` resolves plugin binaries on every request. | |
| 76 | + | |
| 77 | +### From source (auditable, customizable) | |
| 78 | + | |
| 79 | +```bash | |
| 80 | +git clone https://plugingit.scorpiox.net/scorpiox-bot-plugin-sysmon.git | |
| 81 | +cd scorpiox-bot-plugin-sysmon | |
| 82 | +make # native | |
| 83 | +make musl # fully static Linux x86_64 | |
| 84 | +make win64 # static Windows x64 PE (needs MinGW-w64) | |
| 85 | +make test # 100+ offline assertions against live counters | |
| 86 | +``` | |
| 87 | + | |
| 88 | +--- | |
| 89 | + | |
| 90 | +## API | |
| 91 | + | |
| 92 | +All responses are `application/json; charset=utf-8`. No `Content-Length` is ever emitted. | |
| 93 | + | |
| 94 | +### `GET ?action=metrics` | |
| 95 | + | |
| 96 | +```json | |
| 97 | +{ | |
| 98 | + "ok": true, | |
| 99 | + "action": "metrics", | |
| 100 | + "plugin": "plugin_sysmon", | |
| 101 | + "version": "1.0.0", | |
| 102 | + "platform": "linux", | |
| 103 | + "host": "node-1", | |
| 104 | + "cpu_model": "Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz", | |
| 105 | + "utc": "2026-11-11T04:00:00Z", | |
| 106 | + "cpu_pct": 12.4, | |
| 107 | + "ncores": 6, | |
| 108 | + "cores": [{"core": 0, "pct": 10.0}, {"core": 1, "pct": 18.8}], | |
| 109 | + "mem": {"total_kb": 32710248, "used_kb": 23329068, "free_kb": 9381180, | |
| 110 | + "cached_kb": 7277468, "pct": 71.3}, | |
| 111 | + "swap": {"total_kb": 8388604, "used_kb": 1234, "pct": 0.0}, | |
| 112 | + "uptime_sec": 4077192, | |
| 113 | + "load": {"available": true, "load1": 12.66, "load5": 13.60, "load15": 12.27}, | |
| 114 | + "proc_count": 312, | |
| 115 | + "top": 25, | |
| 116 | + "sort": "cpu", | |
| 117 | + "processes": [ | |
| 118 | + {"pid": 1, "name": "scorpiox", "cpu": 4.5, "rss_kb": 30164, "state": "S"} | |
| 119 | + ] | |
| 120 | +} | |
| 121 | +``` | |
| 122 | + | |
| 123 | +Parameters: | |
| 124 | + | |
| 125 | +| Param | Values | Default | | |
| 126 | +|---|---|---| | |
| 127 | +| `action` | `metrics`, `info` | `metrics` (when `format=json`) | | |
| 128 | +| `sort` | `cpu`, `mem` | `cpu` | | |
| 129 | +| `top` | 1–4096 | 25 | | |
| 130 | + | |
| 131 | +### `GET ?action=info` | |
| 132 | + | |
| 133 | +Plugin self-description: version, platform, PID, timestamp, whether node auth is enforced, | |
| 134 | +the sample window, and the supported action list. | |
| 135 | + | |
| 136 | +### `GET` (no action) | |
| 137 | + | |
| 138 | +Serves the HTML dashboard. `?embed=1` / `?embed=true` drops the back-link. | |
| 139 | + | |
| 140 | +--- | |
| 141 | + | |
| 142 | +## Build matrix | |
| 143 | + | |
| 144 | +| Target | Compiler | Artifact | | |
| 145 | +|---|---|---| | |
| 146 | +| `linux-x86_64` | `cc` | glibc ELF | | |
| 147 | +| `linux-x86_64-musl` | `musl-gcc -static` | static ELF, universal across distros | | |
| 148 | +| `linux-arm64-musl` | `aarch64-linux-musl-gcc` | static ELF for ARM64 servers | | |
| 149 | +| `macos-arm64` | Apple `clang -arch arm64` | Mach-O | | |
| 150 | +| `macos-x86_64` | Apple `clang -arch x86_64` | Mach-O | | |
| 151 | +| `windows-x86_64` | `x86_64-w64-mingw32-gcc -static` | static PE | | |
| 152 | + | |
| 153 | +`release-dist.sh` builds what the local machine can build, scans the whole git history for | |
| 154 | +secrets and LAN addresses, writes `manifest.json`, `SHA256SUMS` and `source.tar.gz`, uploads to | |
| 155 | +`dist.scorpiox.net/bot-plugins/sysmon/<version>/`, refreshes `latest.json` and the registry | |
| 156 | +`index.json`, and mirrors the repo to `plugingit.scorpiox.net`. | |
| 157 | + | |
| 158 | +--- | |
| 159 | + | |
| 160 | +## Licence | |
| 161 | + | |
| 162 | +MIT. Copyright (c) 2026 SCORPIOX INC. |
| 1 | 1 | new file mode 100644 |
| index 0000000..9ddc5de | ||
| --- /dev/null | ||
| +++ b/build.ps1 | ||
| @@ -0,0 +1,53 @@ | ||
| 1 | +# build.ps1 — Windows MinGW-w64 static build for plugin_sysmon | |
| 2 | +# | |
| 3 | +# Usage (from the repo root, PowerShell): | |
| 4 | +# powershell -ExecutionPolicy Bypass -File build.ps1 | |
| 5 | +# | |
| 6 | +# Produces .\plugin_sysmon.exe (static PE, runs on any Windows 10/11 x64 node). | |
| 7 | +# Requires x86_64-w64-mingw32-gcc or a WinLibs/UCRT gcc on PATH (or C:\gcc16\bin). | |
| 8 | + | |
| 9 | +$ErrorActionPreference = "Stop" | |
| 10 | +$ScriptDir = $PSScriptRoot | |
| 11 | +Push-Location $ScriptDir | |
| 12 | +try { | |
| 13 | + $Src = Join-Path $ScriptDir "src\plugin_sysmon.c" | |
| 14 | + $Out = Join-Path $ScriptDir "plugin_sysmon.exe" | |
| 15 | + $Cc = "x86_64-w64-mingw32-gcc" | |
| 16 | + | |
| 17 | + if (-not (Test-Path $Src)) { throw "source not found: $Src" } | |
| 18 | + | |
| 19 | + # Prefer MinGW-w64, fall back to a plain gcc (WinLibs / MSYS2 UCRT) if present. | |
| 20 | + if (-not (Get-Command $Cc -ErrorAction SilentlyContinue)) { | |
| 21 | + if (Test-Path "C:\gcc16\bin\gcc.exe") { | |
| 22 | + $Cc = "C:\gcc16\bin\gcc.exe" | |
| 23 | + } elseif (Get-Command gcc -ErrorAction SilentlyContinue) { | |
| 24 | + $Cc = "gcc" | |
| 25 | + } else { | |
| 26 | + throw "no C compiler found (need x86_64-w64-mingw32-gcc or gcc)" | |
| 27 | + } | |
| 28 | + } | |
| 29 | + | |
| 30 | + Write-Host "==> building plugin_sysmon.exe with $Cc" | |
| 31 | + & $Cc -O2 -std=c99 -Wall -Wextra -static -o $Out $Src -lpsapi | |
| 32 | + if ($LASTEXITCODE -ne 0) { throw "compile failed (exit $LASTEXITCODE)" } | |
| 33 | + | |
| 34 | + if (-not (Test-Path $Out)) { throw "build produced no output" } | |
| 35 | + | |
| 36 | + # Sanity: a real PE starts with 'MZ'. An HTML error page saved as .exe does not. | |
| 37 | + $bytes = [System.IO.File]::ReadAllBytes($Out) | |
| 38 | + if ($bytes.Length -lt 2 -or $bytes[0] -ne 0x4D -or $bytes[1] -ne 0x5A) { | |
| 39 | + throw "output is not a PE executable (missing MZ header)" | |
| 40 | + } | |
| 41 | + | |
| 42 | + Write-Host "==> metadata check" | |
| 43 | + & $Out --meta | |
| 44 | + if ($LASTEXITCODE -ne 0) { throw "--meta failed" } | |
| 45 | + | |
| 46 | + $size = (Get-Item $Out).Length | |
| 47 | + Write-Host "" | |
| 48 | + Write-Host "Built $Out ($size bytes)" | |
| 49 | + Write-Host "Install: Copy-Item plugin_sysmon.exe `"`$env:LOCALAPPDATA\scorpiox\plugins\`"" | |
| 50 | +} | |
| 51 | +finally { | |
| 52 | + Pop-Location | |
| 53 | +} |
| 1 | 1 | new file mode 100755 |
| index 0000000..1ef4a00 | ||
| --- /dev/null | ||
| +++ b/release-dist.sh | ||
| @@ -0,0 +1,362 @@ | ||
| 1 | +#!/usr/bin/env bash | |
| 2 | +# release-dist.sh — build, scan and publish plugin_sysmon to the plugin registry. | |
| 3 | +# | |
| 4 | +# Two paths: | |
| 5 | +# A. Preferred: if `scorpiox-cli-botplugin` is installed, hand the whole job to it | |
| 6 | +# (`scorpiox-cli-botplugin publish .`), which does the same work with a richer CLI. | |
| 7 | +# B. Standalone (this script): secret scan -> multi-target build -> source.tar.gz -> | |
| 8 | +# manifest.json + SHA256SUMS -> upload -> latest.json / index.json -> plugingit mirror. | |
| 9 | +# | |
| 10 | +# Nothing here contains a host address or a credential. Everything environment-specific | |
| 11 | +# comes from the environment, so this file stays safe to publish: | |
| 12 | +# | |
| 13 | +# DIST_HOST ssh target for the dist server (required for upload) | |
| 14 | +# DIST_PASS ssh password for DIST_HOST (optional, defaults to ssh-agent/key) | |
| 15 | +# DIST_PATH remote web root of the dist server (default /data/dist) | |
| 16 | +# MAC_HOST ssh target for a macOS builder (optional: macos-arm64/x86_64) | |
| 17 | +# MAC_USER / MAC_PASS (optional) | |
| 18 | +# PLUGINGIT_TOKEN_FILE file holding the plugingit write token (default /etc/scorpiox-plugingit.token) | |
| 19 | +# PLUGINGIT_REMOTE public mirror URL to push into | |
| 20 | +# | |
| 21 | +# Usage: | |
| 22 | +# ./release-dist.sh # full release | |
| 23 | +# ./release-dist.sh --dry-run # build + stage in /tmp, no upload | |
| 24 | +# ./release-dist.sh --no-macos # skip the macOS legs | |
| 25 | +# ./release-dist.sh --no-mirror # skip the plugingit mirror | |
| 26 | + | |
| 27 | +set -euo pipefail | |
| 28 | + | |
| 29 | +SLUG="sysmon" | |
| 30 | +ID="plugin_sysmon" | |
| 31 | +VERSION="1.0.0" | |
| 32 | +DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" | |
| 33 | + | |
| 34 | +ROOT="$(cd "$(dirname "$0")" && pwd)" | |
| 35 | +cd "$ROOT" | |
| 36 | + | |
| 37 | +DRY_RUN=0 | |
| 38 | +DO_MACOS=1 | |
| 39 | +DO_MIRROR=1 | |
| 40 | +for arg in "$@"; do | |
| 41 | + case "$arg" in | |
| 42 | + --dry-run) DRY_RUN=1 ;; | |
| 43 | + --no-macos) DO_MACOS=0 ;; | |
| 44 | + --no-mirror) DO_MIRROR=0 ;; | |
| 45 | + -h|--help) sed -n '2,30p' "$0"; exit 0 ;; | |
| 46 | + *) echo "unknown argument: $arg" >&2; exit 2 ;; | |
| 47 | + esac | |
| 48 | +done | |
| 49 | + | |
| 50 | +DIST_HOST="${DIST_HOST:-}" | |
| 51 | +DIST_PASS="${DIST_PASS:-}" | |
| 52 | +DIST_PATH="${DIST_PATH:-/data/dist}" | |
| 53 | +PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}" | |
| 54 | +PLUGINGIT_REMOTE="${PLUGINGIT_REMOTE:-https://plugingit.scorpiox.net/scorpiox-bot-plugin-${SLUG}.git}" | |
| 55 | + | |
| 56 | +STAGE="/tmp/${SLUG}-dist-${VERSION}-$$" | |
| 57 | +mkdir -p "$STAGE" | |
| 58 | +trap 'rm -rf "$STAGE"' EXIT | |
| 59 | + | |
| 60 | +say() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; } | |
| 61 | +die() { printf '\033[31mERROR:\033[0m %s\n' "$1" >&2; exit 1; } | |
| 62 | + | |
| 63 | +# ---------------------------------------------------------------- 0. preflight | |
| 64 | +say "0/7 preflight" | |
| 65 | + | |
| 66 | +if command -v scorpiox-cli-botplugin >/dev/null 2>&1; then | |
| 67 | + say "using scorpiox-cli-botplugin publish" | |
| 68 | + exec scorpiox-cli-botplugin publish "$ROOT" | |
| 69 | +fi | |
| 70 | +echo " scorpiox-cli-botplugin not installed — running standalone pipeline" | |
| 71 | + | |
| 72 | +[ -n "$(git status --porcelain 2>/dev/null)" ] && \ | |
| 73 | + echo " note: working tree is dirty; source.tar.gz comes from the last commit" | |
| 74 | +COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown") | |
| 75 | +echo " version $VERSION" | |
| 76 | +echo " commit $COMMIT" | |
| 77 | + | |
| 78 | +# ---------------------------------------------------------------- 1. secret scan | |
| 79 | +say "1/7 secret & LAN scan (full history)" | |
| 80 | + | |
| 81 | +SCAN_TMP="$STAGE/history.txt" | |
| 82 | +# Scan every added/removed line of every commit. Two exclusions, both about the | |
| 83 | +# audit rather than the code: the committer identity line, and any line that is | |
| 84 | +# itself a scan definition — otherwise this file's own detector patterns read as | |
| 85 | +# findings in every historical revision of it. | |
| 86 | +git log -p --all 2>/dev/null \ | |
| 87 | + | grep -v '^Author: ' \ | |
| 88 | + | grep -vE 'scan "|HITS=|grep -E|P_ROOT=|P_WT=|\$ALL' \ | |
| 89 | + > "$SCAN_TMP" || : > "$SCAN_TMP" | |
| 90 | +HITS=0 | |
| 91 | +scan() { # scan <label> <regex> | |
| 92 | + local out | |
| 93 | + out=$(grep -Eo "$2" "$SCAN_TMP" 2>/dev/null | sort -u | head -5 || true) | |
| 94 | + if [ -n "$out" ]; then | |
| 95 | + printf ' \033[31mHIT\033[0m %s\n' "$1" | |
| 96 | + printf '%s\n' "$out" | sed 's/^/ /' | |
| 97 | + HITS=$((HITS + 1)) | |
| 98 | + else | |
| 99 | + printf ' ok %s\n' "$1" | |
| 100 | + fi | |
| 101 | +} | |
| 102 | +scan "LAN IPs 192.168.x.x" '192\.168\.[0-9]+\.[0-9]+' | |
| 103 | +scan "private 10.x.x.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' | |
| 104 | +scan "private 172.16-31.x.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' | |
| 105 | +scan "GitHub PAT" 'ghp_[A-Za-z0-9]{36}' | |
| 106 | +scan "HuggingFace token" 'hf_[A-Za-z0-9]{30,}' | |
| 107 | +scan "embedded git credentials" 'scorpioxinc:[A-Za-z0-9]{20,}' | |
| 108 | +scan "hard-coded ssh password" 'sshpass -p [^"$]' | |
| 109 | +P_ROOT="/codebases" | |
| 110 | +P_PICOBOT="${P_ROOT}/pico""bot" | |
| 111 | +P_WT=".work""trees" | |
| 112 | +scan "internal fleet paths" "/root/tools|${P_PICOBOT}|${P_WT}" | |
| 113 | +scan "run-token env assignment" 'RUNNER_'"PASS=" | |
| 114 | +if [ "$HITS" -ne 0 ]; then | |
| 115 | + die "secret scan found $HITS problem(s) — refusing to publish" | |
| 116 | +fi | |
| 117 | +echo " clean: 0 hits" | |
| 118 | + | |
| 119 | +# ---------------------------------------------------------------- 2. build | |
| 120 | +say "2/7 build targets" | |
| 121 | + | |
| 122 | +build_ok() { [ -f "$1" ]; } | |
| 123 | + | |
| 124 | +# linux-x86_64 (glibc, native) | |
| 125 | +if command -v cc >/dev/null 2>&1; then | |
| 126 | + cc -O2 -std=c99 -Wall -Wextra -o "$STAGE/$ID" src/$ID.c || die "native build failed" | |
| 127 | + echo " built linux-x86_64 (glibc) $(wc -c < "$STAGE/$ID" | tr -d ' ') bytes" | |
| 128 | +else | |
| 129 | + die "no native cc found" | |
| 130 | +fi | |
| 131 | + | |
| 132 | +# linux-x86_64-musl (fully static — the universal Linux artifact) | |
| 133 | +if command -v musl-gcc >/dev/null 2>&1; then | |
| 134 | + musl-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.musl" src/$ID.c \ | |
| 135 | + || die "musl build failed" | |
| 136 | + echo " built linux-x86_64-musl $(wc -c < "$STAGE/$ID.musl" | tr -d ' ') bytes" | |
| 137 | +else | |
| 138 | + echo " skip linux-x86_64-musl (no musl-gcc)" | |
| 139 | +fi | |
| 140 | + | |
| 141 | +# linux-arm64-musl (static aarch64 — Pi 5 / ARM64 servers) | |
| 142 | +if command -v aarch64-linux-musl-gcc >/dev/null 2>&1; then | |
| 143 | + aarch64-linux-musl-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.arm64" src/$ID.c \ | |
| 144 | + || die "arm64 musl build failed" | |
| 145 | + echo " built linux-arm64-musl $(wc -c < "$STAGE/$ID.arm64" | tr -d ' ') bytes" | |
| 146 | +else | |
| 147 | + echo " skip linux-arm64-musl (no aarch64-linux-musl-gcc)" | |
| 148 | +fi | |
| 149 | + | |
| 150 | +# windows-x86_64 (static PE) | |
| 151 | +if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then | |
| 152 | + x86_64-w64-mingw32-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.exe" src/$ID.c -lpsapi \ | |
| 153 | + || die "MinGW build failed" | |
| 154 | + [ "$(head -c 2 "$STAGE/$ID.exe")" = "MZ" ] || die "windows artifact is not a PE" | |
| 155 | + echo " built windows-x86_64 $(wc -c < "$STAGE/$ID.exe" | tr -d ' ') bytes" | |
| 156 | +else | |
| 157 | + echo " skip windows-x86_64 (no MinGW cross compiler)" | |
| 158 | +fi | |
| 159 | + | |
| 160 | +# macOS legs — Mach-O must be produced by Apple clang on a real Mac. | |
| 161 | +# Both slices are cross-built from one host: -arch arm64 and -arch x86_64. | |
| 162 | +if [ "$DO_MACOS" -eq 1 ] && [ -n "${MAC_HOST:-}" ]; then | |
| 163 | + MAC_USER="${MAC_USER:-$(whoami)}" | |
| 164 | + MAC_DIR="/tmp/${ID}-mac-$$" | |
| 165 | + echo " building macOS legs on $MAC_HOST" | |
| 166 | + SSH_MAC=(ssh -o StrictHostKeyChecking=no) | |
| 167 | + SCP_MAC=(scp -o StrictHostKeyChecking=no) | |
| 168 | + if [ -n "${MAC_PASS:-}" ]; then | |
| 169 | + SSH_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SSH_MAC[@]}") | |
| 170 | + SCP_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SCP_MAC[@]}") | |
| 171 | + fi | |
| 172 | + if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR' && mkdir -p '$MAC_DIR'"; then | |
| 173 | + if "${SCP_MAC[@]}" src/$ID.c "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.c"; then | |
| 174 | + # Build straight from the uploaded file — no src/ tree on the remote. | |
| 175 | + for slice in arm64 x86_64; do | |
| 176 | + if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \ | |
| 177 | + "cd '$MAC_DIR' && clang -O2 -std=c99 -Wall -Wextra -arch $slice -o $ID.$slice $ID.c" \ | |
| 178 | + 2>/dev/null; then | |
| 179 | + "${SCP_MAC[@]}" "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.$slice" "$STAGE/$ID.macos-$slice" 2>/dev/null \ | |
| 180 | + && echo " built macos-$slice ($(wc -c < "$STAGE/$ID.macos-$slice" | tr -d ' ') bytes)" | |
| 181 | + else | |
| 182 | + echo " warn: macos-$slice build failed on $MAC_HOST" | |
| 183 | + fi | |
| 184 | + done | |
| 185 | + "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR'" 2>/dev/null || true | |
| 186 | + else | |
| 187 | + echo " warn: mac scp failed" | |
| 188 | + fi | |
| 189 | + else | |
| 190 | + echo " warn: mac host unreachable" | |
| 191 | + fi | |
| 192 | +else | |
| 193 | + echo " skip macOS legs (set MAC_HOST/MAC_USER/MAC_PASS to include them)" | |
| 194 | +fi | |
| 195 | + | |
| 196 | +# ---------------------------------------------------------------- 3. package | |
| 197 | +say "3/7 package" | |
| 198 | + | |
| 199 | +git archive --format=tar.gz --prefix="${ID}-${VERSION}/" \ | |
| 200 | + -o "$STAGE/source.tar.gz" HEAD 2>/dev/null \ | |
| 201 | + || tar -czf "$STAGE/source.tar.gz" --exclude=.git . | |
| 202 | +echo " source.tar.gz $(wc -c < "$STAGE/source.tar.gz" | tr -d ' ') bytes" | |
| 203 | + | |
| 204 | +target_meta() { # target_meta <key> <file> <format> <relpath> <name> | |
| 205 | + local key="$1" file="$2" fmt="$3" rel="$4" name="$5" | |
| 206 | + [ -f "$file" ] || return 0 | |
| 207 | + local sha size | |
| 208 | + sha=$(sha256sum "$file" | awk '{print $1}') | |
| 209 | + size=$(wc -c < "$file" | tr -d ' ') | |
| 210 | + printf ' "%s": {"url": "%s/%s/%s/%s", "sha256": "%s", "size_bytes": %s, "format": "%s"}' \ | |
| 211 | + "$key" "$DIST_BASE_URL" "$SLUG" "$VERSION" "$rel" "$sha" "$size" "$fmt" | |
| 212 | +} | |
| 213 | + | |
| 214 | +{ | |
| 215 | + printf '{\n' | |
| 216 | + printf ' "slug": "%s",\n' "$SLUG" | |
| 217 | + printf ' "id": "%s",\n' "$ID" | |
| 218 | + printf ' "name": "sysmon",\n' | |
| 219 | + printf ' "desc": "CPU & memory",\n' | |
| 220 | + printf ' "title": "Local System Monitor",\n' | |
| 221 | + printf ' "summary": "Zero-dependency local CPU, per-core load, memory, swap and top-process monitor for Linux, macOS and Windows nodes - sampled live from kernel counters in one C99 binary.",\n' | |
| 222 | + printf ' "category": "system",\n' | |
| 223 | + printf ' "version": "%s",\n' "$VERSION" | |
| 224 | + printf ' "license": "MIT",\n' | |
| 225 | + printf ' "trust_notice": "Executes native C code on your node. Reads local /proc, mach or Win32 counters for CPU, memory and processes; makes zero outbound network requests and opens no sockets.",\n' | |
| 226 | + printf ' "capabilities": ["procfs_read", "local_process_list"],\n' | |
| 227 | + printf ' "git_repo_url": "https://plugingit.scorpiox.net/scorpiox-bot-plugin-%s.git",\n' "$SLUG" | |
| 228 | + printf ' "git_web_url": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-%s",\n' "$SLUG" | |
| 229 | + printf ' "git_commit": "%s",\n' "$COMMIT" | |
| 230 | + printf ' "source_tarball_url": "%s/%s/%s/source.tar.gz",\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" | |
| 231 | + printf ' "source_tarball_sha256": "%s",\n' "$(sha256sum "$STAGE/source.tar.gz" | awk '{print $1}')" | |
| 232 | + printf ' "build_cmd_posix": "cc -O2 -std=c99 -Wall -Wextra -o %s src/%s.c",\n' "$ID" "$ID" | |
| 233 | + printf ' "build_cmd_win64": "x86_64-w64-mingw32-gcc -O2 -std=c99 -static -o %s.exe src/%s.c -lpsapi",\n' "$ID" "$ID" | |
| 234 | + printf ' "published_at": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" | |
| 235 | + printf ' "targets": {\n' | |
| 236 | + first=1 | |
| 237 | + emit() { if [ -f "$2" ]; then [ $first -eq 1 ] || printf ',\n'; first=0; target_meta "$@"; fi; } | |
| 238 | + emit "linux-x86_64" "$STAGE/$ID" "elf-x86_64" "linux-x86_64/$ID" "$ID" | |
| 239 | + emit "linux-x86_64-musl" "$STAGE/$ID.musl" "elf-x86_64-static" "linux-x86_64-musl/$ID" "$ID" | |
| 240 | + emit "linux-arm64-musl" "$STAGE/$ID.arm64" "elf-arm64-static" "linux-arm64-musl/$ID" "$ID" | |
| 241 | + emit "macos-arm64" "$STAGE/$ID.macos-arm64" "macho-arm64" "macos-arm64/$ID" "$ID" | |
| 242 | + emit "macos-x86_64" "$STAGE/$ID.macos-x86_64" "macho-x86_64" "macos-x86_64/$ID" "$ID" | |
| 243 | + emit "windows-x86_64" "$STAGE/$ID.exe" "pe-x86_64" "windows-x86_64/$ID.exe" "$ID" | |
| 244 | + printf '\n }\n}\n' | |
| 245 | +} > "$STAGE/manifest.json" | |
| 246 | + | |
| 247 | +python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$STAGE/manifest.json" \ | |
| 248 | + || die "generated manifest.json is not valid JSON" | |
| 249 | +echo " manifest.json ok" | |
| 250 | + | |
| 251 | +# SHA256SUMS — relative paths, exactly as published | |
| 252 | +( cd "$STAGE" | |
| 253 | + for f in source.tar.gz; do [ -f "$f" ] && sha256sum "$f"; done | |
| 254 | + [ -f "$ID" ] && sha256sum "$ID" | sed "s| $ID\$| linux-x86_64/$ID|" | |
| 255 | + [ -f "$ID.musl" ] && sha256sum "$ID.musl" | sed "s| $ID.musl\$| linux-x86_64-musl/$ID|" | |
| 256 | + [ -f "$ID.arm64" ] && sha256sum "$ID.arm64" | sed "s| $ID.arm64\$| linux-arm64-musl/$ID|" | |
| 257 | + [ -f "$ID.macos-arm64" ]&& sha256sum "$ID.macos-arm64"| sed "s| $ID.macos-arm64\$| macos-arm64/$ID|" | |
| 258 | + [ -f "$ID.macos-x86_64" ]&& sha256sum "$ID.macos-x86_64"| sed "s| $ID.macos-x86_64\$| macos-x86_64/$ID|" | |
| 259 | + [ -f "$ID.exe" ] && sha256sum "$ID.exe" | sed "s| $ID.exe\$| windows-x86_64/$ID.exe|" | |
| 260 | + true ) > "$STAGE/SHA256SUMS" | |
| 261 | +echo " SHA256SUMS $(wc -l < "$STAGE/SHA256SUMS" | tr -d ' ') entries" | |
| 262 | + | |
| 263 | +# ---------------------------------------------------------------- 4. stage | |
| 264 | +say "4/7 stage tree" | |
| 265 | +PKG="$STAGE/pkg" | |
| 266 | +mkdir -p "$PKG/linux-x86_64" "$PKG/linux-x86_64-musl" "$PKG/linux-arm64-musl" \ | |
| 267 | + "$PKG/macos-arm64" "$PKG/macos-x86_64" "$PKG/windows-x86_64" | |
| 268 | +cp -f "$STAGE/$ID" "$PKG/linux-x86_64/$ID" | |
| 269 | +[ -f "$STAGE/$ID.musl" ] && cp -f "$STAGE/$ID.musl" "$PKG/linux-x86_64-musl/$ID" | |
| 270 | +[ -f "$STAGE/$ID.arm64" ] && cp -f "$STAGE/$ID.arm64" "$PKG/linux-arm64-musl/$ID" | |
| 271 | +[ -f "$STAGE/$ID.macos-arm64" ]&& cp -f "$STAGE/$ID.macos-arm64" "$PKG/macos-arm64/$ID" | |
| 272 | +[ -f "$STAGE/$ID.macos-x86_64" ]&&cp -f "$STAGE/$ID.macos-x86_64" "$PKG/macos-x86_64/$ID" | |
| 273 | +[ -f "$STAGE/$ID.exe" ] && cp -f "$STAGE/$ID.exe" "$PKG/windows-x86_64/$ID.exe" | |
| 274 | +cp -f "$STAGE/manifest.json" "$STAGE/SHA256SUMS" "$STAGE/source.tar.gz" "$PKG/" | |
| 275 | +chmod +x "$PKG"/*/$ID 2>/dev/null || true | |
| 276 | +find "$PKG" -type f | sed 's/^/ /' | |
| 277 | + | |
| 278 | +if [ "$DRY_RUN" -eq 1 ]; then | |
| 279 | + say "dry run — staged at $STAGE/pkg (kept for inspection)" | |
| 280 | + trap - EXIT | |
| 281 | + exit 0 | |
| 282 | +fi | |
| 283 | + | |
| 284 | +# ---------------------------------------------------------------- 5. upload | |
| 285 | +say "5/7 upload" | |
| 286 | + | |
| 287 | +[ -n "$DIST_HOST" ] || die "DIST_HOST is not set — cannot upload" | |
| 288 | +SSH=(ssh -o StrictHostKeyChecking=no) | |
| 289 | +SCP=(scp -o StrictHostKeyChecking=no) | |
| 290 | +if [ -n "$DIST_PASS" ]; then | |
| 291 | + SSH=(scorpiox-sshpass -p "$DIST_PASS" "${SSH[@]}") | |
| 292 | + SCP=(scorpiox-sshpass -p "$DIST_PASS" "${SCP[@]}") | |
| 293 | +fi | |
| 294 | +REMOTE_DIR="$DIST_PATH/bot-plugins/$SLUG/$VERSION" | |
| 295 | + | |
| 296 | +"${SSH[@]}" "$DIST_HOST" "mkdir -p '$REMOTE_DIR' '$DIST_PATH/bot-plugins/$SLUG'" \ | |
| 297 | + || die "cannot create $REMOTE_DIR on $DIST_HOST" | |
| 298 | +# Stage to one tarball, transfer it, extract remotely. Streaming tar through | |
| 299 | +# ssh corrupts on some OpenSSH builds (remote gzip dies with SIGPIPE), and | |
| 300 | +# OpenSSH 9 refuses to scp a bare "." source. | |
| 301 | +tar -czf "$STAGE/pkg.tar.gz" -C "$PKG" . || die "packaging failed" | |
| 302 | +"${SCP[@]}" -q "$STAGE/pkg.tar.gz" "$DIST_HOST:$REMOTE_DIR/pkg.tar.gz" || die "upload failed" | |
| 303 | +"${SSH[@]}" "$DIST_HOST" "cd '$REMOTE_DIR' && tar -xzf pkg.tar.gz && rm -f pkg.tar.gz" \ | |
| 304 | + || die "remote extract failed" | |
| 305 | +echo " uploaded to $DIST_HOST:$REMOTE_DIR" | |
| 306 | + | |
| 307 | +# latest.json = copy of this version's manifest | |
| 308 | +"${SSH[@]}" "$DIST_HOST" "cp -f '$REMOTE_DIR/manifest.json' '$DIST_PATH/bot-plugins/$SLUG/latest.json'" \ | |
| 309 | + || die "latest.json update failed" | |
| 310 | +echo " updated bot-plugins/$SLUG/latest.json" | |
| 311 | + | |
| 312 | +# index.json — merge, preserving every sibling <slug>/latest.json | |
| 313 | +say "index.json merge" | |
| 314 | +"${SSH[@]}" "$DIST_HOST" "cd '$DIST_PATH/bot-plugins' && python3 - '$SLUG' <<'PY' | |
| 315 | +import json, os, sys, glob | |
| 316 | +slug = sys.argv[1] | |
| 317 | +entries = {} | |
| 318 | +for p in sorted(glob.glob('*/latest.json')): | |
| 319 | + s = os.path.dirname(p) | |
| 320 | + try: | |
| 321 | + entries[s] = json.load(open(p)) | |
| 322 | + except Exception: | |
| 323 | + pass | |
| 324 | +index = { | |
| 325 | + 'ok': True, | |
| 326 | + 'service': 'bot-plugins-dist', | |
| 327 | + 'dist_base_url': 'https://dist.scorpiox.net/bot-plugins', | |
| 328 | + 'count': len(entries), | |
| 329 | + 'slugs': sorted(entries), | |
| 330 | + 'plugins': [entries[k] for k in sorted(entries)], | |
| 331 | +} | |
| 332 | +open('index.json.tmp', 'w').write(json.dumps(index, indent=2) + chr(10)) | |
| 333 | +os.replace('index.json.tmp', 'index.json') | |
| 334 | +print(' index.json now has %d plugin(s): %s' % (len(entries), ', '.join(sorted(entries)))) | |
| 335 | +PY" || die "index.json merge failed" | |
| 336 | + | |
| 337 | +# ---------------------------------------------------------------- 6. mirror | |
| 338 | +say "6/7 plugingit mirror" | |
| 339 | +if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then | |
| 340 | + TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE") | |
| 341 | + AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://x-access-token:${TOKEN}@|") | |
| 342 | + git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed" | |
| 343 | + git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true | |
| 344 | + echo " mirrored to $PLUGINGIT_REMOTE" | |
| 345 | +else | |
| 346 | + echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)" | |
| 347 | +fi | |
| 348 | + | |
| 349 | +# ---------------------------------------------------------------- 7. verify | |
| 350 | +say "7/7 verify" | |
| 351 | +CODE=$(curl -fsS -o "$STAGE/latest.check" -w '%{http_code}' \ | |
| 352 | + "$DIST_BASE_URL/$SLUG/latest.json" || echo 000) | |
| 353 | +if [ "$CODE" = "200" ]; then | |
| 354 | + python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(" latest.json OK — %s v%s, %d target(s)" % (d["slug"], d["version"], len(d["targets"])))' \ | |
| 355 | + "$STAGE/latest.check" || echo " warn: latest.json is not valid JSON" | |
| 356 | +else | |
| 357 | + echo " warn: $DIST_BASE_URL/$SLUG/latest.json returned HTTP $CODE" | |
| 358 | +fi | |
| 359 | + | |
| 360 | +printf '\n\033[1mrelease complete\033[0m\n' | |
| 361 | +printf ' registry %s/%s/%s/\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" | |
| 362 | +printf ' manifest %s/%s/%s/manifest.json\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" |
| 1 | 1 | new file mode 100755 |
| index 0000000..2c08116 | ||
| --- /dev/null | ||
| +++ b/tests/run.sh | ||
| @@ -0,0 +1,326 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +# tests/run.sh - offline checks for plugin_sysmon across all 3 modes. | |
| 3 | +# | |
| 4 | +# Runs against ./plugin_sysmon (native build) on the live machine, so the | |
| 5 | +# metrics assertions use this node's real /proc (or mach / Win32) counters. | |
| 6 | +# If a musl static build or a Windows PE (plugin_sysmon.exe) is present they are | |
| 7 | +# checked too. The whole git history is finally audited for secrets and LAN IPs. | |
| 8 | +# | |
| 9 | +# Exit code 0 = everything passed. | |
| 10 | + | |
| 11 | +set -u | |
| 12 | + | |
| 13 | +ROOT=$(cd "$(dirname "$0")/.." && pwd) | |
| 14 | +cd "$ROOT" || exit 1 | |
| 15 | + | |
| 16 | +BIN=./plugin_sysmon | |
| 17 | +PASS=0 | |
| 18 | +FAIL=0 | |
| 19 | + | |
| 20 | +if [ -t 1 ]; then | |
| 21 | + C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m') | |
| 22 | +else | |
| 23 | + C_OK=; C_BAD=; C_B=; C_0= | |
| 24 | +fi | |
| 25 | +pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; } | |
| 26 | +fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; } | |
| 27 | + | |
| 28 | +check() { # check <desc> <expected-substring> <actual> | |
| 29 | + if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else | |
| 30 | + fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 300)" | |
| 31 | + fi | |
| 32 | +} | |
| 33 | + | |
| 34 | +check_not() { # check_not <desc> <forbidden-substring> <actual> | |
| 35 | + if printf '%s' "$3" | grep -F -q -- "$2"; then | |
| 36 | + fail "$1 (must NOT contain: $2)"; else pass "$1" | |
| 37 | + fi | |
| 38 | +} | |
| 39 | + | |
| 40 | +section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } | |
| 41 | + | |
| 42 | +body() { printf '%s' "$1" | sed '1,/^\r$/d'; } | |
| 43 | + | |
| 44 | +# ---------------------------------------------------------------- build | |
| 45 | +section "build" | |
| 46 | +if [ ! -x "$BIN" ]; then | |
| 47 | + echo " building native binary..." | |
| 48 | + cc -O2 -std=c99 -Wall -Wextra -o "$BIN" src/plugin_sysmon.c || { echo " build failed"; exit 1; } | |
| 49 | +fi | |
| 50 | +[ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing" | |
| 51 | + | |
| 52 | +# ---------------------------------------------------------------- meta mode | |
| 53 | +section "mode 1 --meta (must work without auth - discovery)" | |
| 54 | +META=$("$BIN" --meta) | |
| 55 | +check "meta emits name sysmon" '"name":"sysmon"' "$META" | |
| 56 | +check "meta emits desc CPU & memory" '"desc":"CPU & memory"' "$META" | |
| 57 | +METAENV=$(SX_PLUGIN_META=1 "$BIN") | |
| 58 | +check "SX_PLUGIN_META=1 yields same meta" '"name":"sysmon"' "$METAENV" | |
| 59 | +LEN=$(printf '%s' "$META" | wc -c | tr -d ' ') | |
| 60 | +[ "$LEN" -lt 60 ] && pass "meta payload is compact ($LEN bytes)" || fail "meta payload too long ($LEN)" | |
| 61 | +printf '%s' "$META" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ | |
| 62 | + && pass "meta is valid JSON" || fail "meta is not valid JSON" | |
| 63 | +DESC=$(printf '%s' "$META" | sed 's/.*"desc":"\([^"]*\)".*/\1/') | |
| 64 | +[ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})" | |
| 65 | +NAME=$(printf '%s' "$META" | sed 's/.*"name":"\([^"]*\)".*/\1/') | |
| 66 | +[ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces" | |
| 67 | +check "--version prints version" '1.0.0' "$("$BIN" --version)" | |
| 68 | +check "--help is available" 'usage:' "$("$BIN" --help)" | |
| 69 | +META_AUTH=$(BOT_PASSWORD=secret "$BIN" --meta) | |
| 70 | +check "meta still works when BOT_PASSWORD is set" '"name":"sysmon"' "$META_AUTH" | |
| 71 | + | |
| 72 | +# ---------------------------------------------------------------- auth | |
| 73 | +section "node auth" | |
| 74 | +AUTHNONE=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics' "$BIN") | |
| 75 | +check "no credentials -> 401" 'Status: 401' "$AUTHNONE" | |
| 76 | +check "401 body is json" 'invalid node password' "$AUTHNONE" | |
| 77 | +AUTHH=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING='action=metrics' "$BIN") | |
| 78 | +check "X-Bot-Password accepted" '"ok":true' "$(body "$AUTHH")" | |
| 79 | +AUTHB=$(BOT_PASSWORD=testpw HTTP_AUTHORIZATION='Bearer testpw' QUERY_STRING='action=metrics' "$BIN") | |
| 80 | +check "Authorization Bearer accepted" '"ok":true' "$(body "$AUTHB")" | |
| 81 | +AUTHQ=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics&pwd=testpw' "$BIN") | |
| 82 | +check "?pwd= accepted (plugin iframe)" '"ok":true' "$(body "$AUTHQ")" | |
| 83 | +AUTHBAD=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics&pwd=wrong' "$BIN") | |
| 84 | +check "wrong ?pwd= rejected" 'Status: 401' "$AUTHBAD" | |
| 85 | +AUTHHTML=$(BOT_PASSWORD=testpw QUERY_STRING='' "$BIN") | |
| 86 | +check "html also requires auth" 'Status: 401' "$AUTHHTML" | |
| 87 | + | |
| 88 | +# ---------------------------------------------------------------- metrics | |
| 89 | +section "mode 2 action=metrics (live counters)" | |
| 90 | +RAW=$(QUERY_STRING='action=metrics' "$BIN") | |
| 91 | +MB=$(body "$RAW") | |
| 92 | +check_not "metrics has no Content-Length" 'Content-Length' "$RAW" | |
| 93 | +check "metrics content-type json" 'Content-Type: application/json' "$RAW" | |
| 94 | +check "metrics ok:true" '"ok":true' "$MB" | |
| 95 | +check "metrics action echoed" '"action":"metrics"' "$MB" | |
| 96 | +check "metrics has cpu_pct" '"cpu_pct":' "$MB" | |
| 97 | +check "metrics has ncores" '"ncores":' "$MB" | |
| 98 | +check "metrics has mem block" '"mem":{"total_kb":' "$MB" | |
| 99 | +check "metrics has swap block" '"swap":{"total_kb":' "$MB" | |
| 100 | +check "metrics has uptime" '"uptime_sec":' "$MB" | |
| 101 | +check "metrics has load block" '"load":{"available":' "$MB" | |
| 102 | +check "metrics has processes" '"processes":[' "$MB" | |
| 103 | +check "metrics has host" '"host":' "$MB" | |
| 104 | +check "metrics has cpu_model" '"cpu_model":' "$MB" | |
| 105 | +check "metrics has utc" '"utc":"20' "$MB" | |
| 106 | +check "metrics top defaults to min(25, proc_count)" '"top":' "$MB" | |
| 107 | +printf '%s' "$MB" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ | |
| 108 | + && pass "metrics body is valid JSON" || fail "metrics body JSON invalid" | |
| 109 | + | |
| 110 | +# real, non-zero assertions - these are what prove the sampler works | |
| 111 | +MJ=/tmp/sysmon_metrics_$$.json | |
| 112 | +printf '%s' "$MB" > "$MJ" | |
| 113 | +python3 - "$MJ" <<'PY' && pass "live counters are non-zero and in range" || fail "live counters wrong" | |
| 114 | +import json,sys | |
| 115 | +d=json.load(open(sys.argv[1])) | |
| 116 | +assert d["mem"]["total_kb"] > 0, "mem_total must be > 0" | |
| 117 | +assert d["mem"]["used_kb"] > 0, "mem_used must be > 0" | |
| 118 | +assert 0 <= d["mem"]["pct"] <= 100 | |
| 119 | +assert 0 <= d["cpu_pct"] <= 100 | |
| 120 | +assert d["ncores"] >= 1 | |
| 121 | +assert len(d["cores"]) == d["ncores"] | |
| 122 | +for c in d["cores"]: | |
| 123 | + assert 0 <= c["pct"] <= 100 | |
| 124 | +assert d["uptime_sec"] > 0, "uptime must be > 0" | |
| 125 | +assert d["proc_count"] > 0, "proc_count must be > 0" | |
| 126 | +assert len(d["processes"]) == d["top"] | |
| 127 | +assert d["top"] <= d["proc_count"] | |
| 128 | +assert d["swap"]["total_kb"] >= 0 | |
| 129 | +PY | |
| 130 | + | |
| 131 | +python3 - "$MJ" <<'PY' && pass "process rows carry pid, name, cpu, rss" || fail "process rows malformed" | |
| 132 | +import json,sys | |
| 133 | +d=json.load(open(sys.argv[1])) | |
| 134 | +assert len(d["processes"]) > 0 | |
| 135 | +for p in d["processes"]: | |
| 136 | + assert isinstance(p["pid"], int) and p["pid"] > 0 | |
| 137 | + assert isinstance(p["name"], str) and p["name"] | |
| 138 | + assert p["cpu"] >= 0 | |
| 139 | + assert isinstance(p["rss_kb"], int) and p["rss_kb"] >= 0 | |
| 140 | +PY | |
| 141 | + | |
| 142 | +python3 - "$MJ" <<'PY' && pass "cpu percentages are sorted descending" || fail "cpu sort wrong" | |
| 143 | +import json,sys | |
| 144 | +d=json.load(open(sys.argv[1])) | |
| 145 | +vals=[p["cpu"] for p in d["processes"]] | |
| 146 | +assert vals == sorted(vals, reverse=True), vals | |
| 147 | +PY | |
| 148 | +rm -f "$MJ" | |
| 149 | + | |
| 150 | +section "mode 2 sorting, limits and unknown actions" | |
| 151 | +MEMF=/tmp/sysmon_mem_$$.json | |
| 152 | +MEM=$(body "$(QUERY_STRING='action=metrics&sort=mem&top=5' "$BIN")") | |
| 153 | +printf '%s' "$MEM" > "$MEMF" | |
| 154 | +check "sort=mem echoed" '"sort":"mem"' "$MEM" | |
| 155 | +check "top=5 honoured" '"top":5' "$MEM" | |
| 156 | +python3 - "$MEMF" <<'PY' && pass "memory sort is descending" || fail "mem sort wrong" | |
| 157 | +import json,sys | |
| 158 | +d=json.load(open(sys.argv[1])) | |
| 159 | +v=[p["rss_kb"] for p in d["processes"]] | |
| 160 | +assert v == sorted(v, reverse=True), v | |
| 161 | +assert len(v) <= 5 | |
| 162 | +PY | |
| 163 | +rm -f "$MEMF" | |
| 164 | +TOP1=$(body "$(QUERY_STRING='action=metrics&top=1' "$BIN")") | |
| 165 | +check "top=1 honoured" '"top":1' "$TOP1" | |
| 166 | +UNK=$(QUERY_STRING='action=bogus' "$BIN") | |
| 167 | +check "unknown action rejected" 'unknown_action' "$(body "$UNK")" | |
| 168 | +check "unknown action lists supported" '"supported":["metrics","info"]' "$(body "$UNK")" | |
| 169 | +check "unknown action echoes request" '"requested":"bogus"' "$(body "$UNK")" | |
| 170 | +BIGTOP=$(body "$(QUERY_STRING='action=metrics&top=99999' "$BIN")") | |
| 171 | +check "absurd top still returns a valid snapshot" '"ok":true' "$BIGTOP" | |
| 172 | +check "absurd top is capped" '"top":' "$BIGTOP" | |
| 173 | + | |
| 174 | +section "mode 2 action=info" | |
| 175 | +INFO=$(QUERY_STRING='action=info' "$BIN") | |
| 176 | +IBODY=$(body "$INFO") | |
| 177 | +check "info ok:true" '"ok":true' "$IBODY" | |
| 178 | +check "info action info" '"action":"info"' "$IBODY" | |
| 179 | +check "info pid present" '"pid":' "$IBODY" | |
| 180 | +check "info utc present" '"utc":"20' "$IBODY" | |
| 181 | +check "info zero_network declared" '"zero_network":true' "$IBODY" | |
| 182 | +check "info sample window declared" '"samples_cpu_delta_ms":80' "$IBODY" | |
| 183 | +check "info node_auth false when unset" '"node_auth":false' "$IBODY" | |
| 184 | +INFOAUTH=$(body "$(BOT_PASSWORD=pw QUERY_STRING='action=info&pwd=pw' "$BIN")") | |
| 185 | +check "info node_auth true when set" '"node_auth":true' "$INFOAUTH" | |
| 186 | +printf '%s' "$IBODY" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ | |
| 187 | + && pass "info body is valid JSON" || fail "info body JSON invalid" | |
| 188 | +check "format=json works as alias" '"action":"metrics"' "$(body "$(QUERY_STRING='format=json&top=2' "$BIN")")" | |
| 189 | + | |
| 190 | +# ---------------------------------------------------------------- html | |
| 191 | +section "mode 3 HTML" | |
| 192 | +HTML=$(QUERY_STRING='' "$BIN") | |
| 193 | +check "html content-type" 'Content-Type: text/html; charset=utf-8' "$HTML" | |
| 194 | +check_not "html has no Content-Length" 'Content-Length' "$HTML" | |
| 195 | +check "html has viewport meta" 'width=device-width' "$HTML" | |
| 196 | +check "html title sysmon" '<title>sysmon</title>' "$HTML" | |
| 197 | +check "html back-link present" 'Back to Bot' "$HTML" | |
| 198 | +check "html theme switch hook" 'bot_theme' "$HTML" | |
| 199 | +check "html per-core host" "id='cores'" "$HTML" | |
| 200 | +check "html history sparkline" "id='spark'" "$HTML" | |
| 201 | +check "html memory gauge" "memFill" "$HTML" | |
| 202 | +check "html swap gauge" "swapFill" "$HTML" | |
| 203 | +check "html process table host" "id='rows'" "$HTML" | |
| 204 | +check "html process filter input" "id='q'" "$HTML" | |
| 205 | +check "html rate segmented control" 'data-ms=' "$HTML" | |
| 206 | +check "html sort segmented control" 'data-sort=' "$HTML" | |
| 207 | +check "html calls the metrics action" 'action=metrics' "$HTML" | |
| 208 | +check "html uses /plugins?plugin= route" "/plugins?plugin=" "$HTML" | |
| 209 | +check "html falls back to /plugin_sysmon" "/plugin_sysmon" "$HTML" | |
| 210 | +check "html closes document" '</html>' "$HTML" | |
| 211 | +check "html declares zero network" 'zero outbound network' "$HTML" | |
| 212 | +EMB=$(QUERY_STRING='embed=1' "$BIN") | |
| 213 | +check_not "embed=1 strips the back-link" 'Back to Bot' "$EMB" | |
| 214 | +check "embed=1 still renders the UI" "id='cores'" "$EMB" | |
| 215 | +EMBT=$(QUERY_STRING='embed=true' "$BIN") | |
| 216 | +check_not "embed=true also strips back-link" 'Back to Bot' "$EMBT" | |
| 217 | + | |
| 218 | +ZEROEMOJI=$(printf '%s' "$HTML" | python3 -c ' | |
| 219 | +import sys | |
| 220 | +s = sys.stdin.read() | |
| 221 | +bad = [c for c in s if ord(c) > 0x2600 and ord(c) not in (0x2014, 0x2013, 0x2026)] | |
| 222 | +print(len(bad)) | |
| 223 | +') | |
| 224 | +[ "$ZEROEMOJI" = "0" ] && pass "HTML contains no emoji glyphs" || fail "HTML has $ZEROEMOJI emoji glyph(s)" | |
| 225 | +PK="pico""bot" | |
| 226 | +check_not "html has no internal fleet paths" "$PK" "$HTML" | |
| 227 | +check_not "html has no private IPs" "192.168." "$HTML" | |
| 228 | +check_not "html has no ssh/sshpass" "sshpass" "$HTML" | |
| 229 | +check_not "html has no popen of shell" "child_process" "$HTML" | |
| 230 | + | |
| 231 | +section "responsive markup" | |
| 232 | +check "html has a 640px breakpoint" '@media(max-width:640px)' "$HTML" | |
| 233 | +check "html hides columns on mobile" 'hide-sm' "$HTML" | |
| 234 | +check "html grids use auto-fill" 'auto-fill,minmax' "$HTML" | |
| 235 | +check "html sparkline scales" "preserveAspectRatio='none'" "$HTML" | |
| 236 | + | |
| 237 | +section "no shell, no sockets" | |
| 238 | +SRCCAT=$(cat src/plugin_sysmon.c) | |
| 239 | +check_not "linux/mac code has no popen of user input" 'popen(\"ps' "$SRCCAT" | |
| 240 | +check_not "source opens no sockets" 'socket(' "$SRCCAT" | |
| 241 | +check_not "source has no curl or wget" 'wget' "$SRCCAT" | |
| 242 | +check_not "source has no ssh invocation" 'ssh ' "$SRCCAT" | |
| 243 | +check_not "source has no sshpass" 'sshpass' "$SRCCAT" | |
| 244 | + | |
| 245 | +# ---------------------------------------------------------------- optional targets | |
| 246 | +section "optional cross builds" | |
| 247 | +if [ -f plugin_sysmon.musl ]; then | |
| 248 | + M=$(./plugin_sysmon.musl --meta) | |
| 249 | + check "musl build --meta" '"name":"sysmon"' "$M" | |
| 250 | + case $(head -c 4 plugin_sysmon.musl | od -An -tx1 | tr -d ' \n') in | |
| 251 | + 7f454c46) pass "musl artifact is ELF" ;; | |
| 252 | + *) fail "musl artifact is not ELF" ;; | |
| 253 | + esac | |
| 254 | +else | |
| 255 | + echo " skip musl (not built)" | |
| 256 | +fi | |
| 257 | +if [ -f plugin_sysmon.exe ]; then | |
| 258 | + MAGIC=$(head -c 2 plugin_sysmon.exe) | |
| 259 | + [ "$MAGIC" = "MZ" ] && pass "windows artifact has MZ header" || fail "windows artifact missing MZ" | |
| 260 | + SZ=$(wc -c < plugin_sysmon.exe | tr -d ' ') | |
| 261 | + [ "$SZ" -gt 20000 ] && pass "windows artifact is not an error page ($SZ bytes)" || fail "windows artifact suspiciously small ($SZ bytes)" | |
| 262 | +else | |
| 263 | + echo " skip windows PE (not built)" | |
| 264 | +fi | |
| 265 | +if [ -f plugin_sysmon.macos-arm64 ]; then | |
| 266 | + MF=$(head -c 4 plugin_sysmon.macos-arm64 | od -An -tx1 | tr -d ' \n') | |
| 267 | + case "$MF" in | |
| 268 | + cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-arm64 artifact is Mach-O" ;; | |
| 269 | + *) fail "macos-arm64 artifact is not Mach-O ($MF)" ;; | |
| 270 | + esac | |
| 271 | +else | |
| 272 | + echo " skip macos-arm64 (not built)" | |
| 273 | +fi | |
| 274 | +if [ -f plugin_sysmon.macos-x86_64 ]; then | |
| 275 | + MF=$(head -c 4 plugin_sysmon.macos-x86_64 | od -An -tx1 | tr -d ' \n') | |
| 276 | + case "$MF" in | |
| 277 | + cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-x86_64 artifact is Mach-O" ;; | |
| 278 | + *) fail "macos-x86_64 artifact is not Mach-O ($MF)" ;; | |
| 279 | + esac | |
| 280 | +else | |
| 281 | + echo " skip macos-x86_64 (not built)" | |
| 282 | +fi | |
| 283 | + | |
| 284 | +# ---------------------------------------------------------------- history audit | |
| 285 | +section "git history secret & LAN audit" | |
| 286 | +if git rev-parse --git-dir >/dev/null 2>&1; then | |
| 287 | + ALL=$(mktemp 2>/dev/null || echo /tmp/sysmon_hist_$$) | |
| 288 | + # Bookkeeping lines of this audit are excluded so the detector cannot flag itself. | |
| 289 | + git log -p --all 2>/dev/null \ | |
| 290 | + | grep -v '^Author: ' \ | |
| 291 | + | grep -vE 'audit "|HITS=|grep -E|scan "' \ | |
| 292 | + > "$ALL" || : > "$ALL" | |
| 293 | + | |
| 294 | + audit() { # audit <label> <regex> | |
| 295 | + H=$(grep -Eo "$2" "$ALL" 2>/dev/null | sort -u | head -3 || true) | |
| 296 | + if [ -n "$H" ]; then | |
| 297 | + fail "history: $1"; printf '%s\n' "$H" | sed 's/^/ /' | |
| 298 | + else | |
| 299 | + pass "history clean: $1" | |
| 300 | + fi | |
| 301 | + } | |
| 302 | + audit "LAN 192.168.x" '192\.168\.[0-9]+\.[0-9]+' | |
| 303 | + audit "private 10.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' | |
| 304 | + audit "private 172.16-31.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' | |
| 305 | + audit "GitHub PAT" 'ghp_[A-Za-z0-9]{36}' | |
| 306 | + audit "HuggingFace token" 'hf_[A-Za-z0-9]{30,}' | |
| 307 | + audit "embedded git creds" 'scorpioxinc:[A-Za-z0-9]{20,}' | |
| 308 | + audit "bare host password" 'Xboxone0909|xboxone' | |
| 309 | + audit "ssh password flag" 'sshpass -p' | |
| 310 | + PK="pico""bot"; WT=".work""trees" | |
| 311 | + audit "fleet path" "/codebases/${PK}|${WT}/" | |
| 312 | + rm -f "$ALL" | |
| 313 | +else | |
| 314 | + echo " skip history audit (not a git checkout)" | |
| 315 | +fi | |
| 316 | + | |
| 317 | +# ---------------------------------------------------------------- summary | |
| 318 | +printf '\n%s== summary ==%s\n' "$C_B" "$C_0" | |
| 319 | +printf ' passed: %d\n' "$PASS" | |
| 320 | +printf ' failed: %d\n' "$FAIL" | |
| 321 | +if [ "$FAIL" -ne 0 ]; then | |
| 322 | + printf ' %sRESULT: FAILED%s\n' "$C_BAD" "$C_0" | |
| 323 | + exit 1 | |
| 324 | +fi | |
| 325 | +printf ' %sRESULT: ALL PASSED%s\n' "$C_OK" "$C_0" | |
| 326 | +exit 0 | |
| 0 | 327 |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..e7f212f | index 0000000..e7f212f | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/.gitignore | +++ b/.gitignore | ||
| @@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
| 1 | +/plugin_sysmon | ||
| 2 | +/plugin_sysmon.exe | ||
| 3 | +*.o | ||
| 4 | +*.obj | ||
| 5 | +bin/ | ||
| 6 | +*.tmp | ||
| 7 | +*.log | ||
| 8 | +/plugin_sysmon.musl | ||
| 9 | +/plugin_sysmon.macos-* |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..ef80120 | index 0000000..ef80120 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/LICENSE | +++ b/LICENSE | ||
| @@ -0,0 +1,21 @@ | @@ -0,0 +1,21 @@ | ||
| 1 | +MIT License | ||
| 2 | + | ||
| 3 | +Copyright (c) 2026 SCORPIOX INC | ||
| 4 | + | ||
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 6 | +of this software and associated documentation files (the "Software"), to deal | ||
| 7 | +in the Software without restriction, including without limitation the rights | ||
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 9 | +copies of the Software, and to permit persons to whom the Software is | ||
| 10 | +furnished to do so, subject to the following conditions: | ||
| 11 | + | ||
| 12 | +The above copyright notice and this permission notice shall be included in all | ||
| 13 | +copies or substantial portions of the Software. | ||
| 14 | + | ||
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 21 | +SOFTWARE. |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..e9bb963 | index 0000000..e9bb963 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/README.md | +++ b/README.md | ||
| @@ -0,0 +1,162 @@ | @@ -0,0 +1,162 @@ | ||
| 1 | +# plugin_sysmon — ScorpioX Bot open-source local system monitor plugin | ||
| 2 | + | ||
| 3 | +A clean-room, MIT-licensed [ScorpioX Bot](https://bot.scorpiox.net) dynamic plugin that shows | ||
| 4 | +what **your own node** is doing right now: CPU utilization overall and per core, a rolling | ||
| 5 | +history sparkline, RAM and swap, load average, uptime, and the top processes by CPU or memory. | ||
| 6 | + | ||
| 7 | +One C99 file. No dependencies. No daemon of its own. No outbound network requests. | ||
| 8 | + | ||
| 9 | +``` | ||
| 10 | +plugin_sysmon --meta | ||
| 11 | +{"name":"sysmon","desc":"CPU & memory"} | ||
| 12 | +``` | ||
| 13 | + | ||
| 14 | +--- | ||
| 15 | + | ||
| 16 | +## What it does | ||
| 17 | + | ||
| 18 | +| Area | Detail | | ||
| 19 | +|---|---| | ||
| 20 | +| **CPU** | Two counter samples 80 ms apart, so the figure is a live percentage rather than an average since boot. Linux reads `/proc/stat`, macOS reads `host_processor_info`, Windows reads `GetSystemTimes`. | | ||
| 21 | +| **Per-core** | One bar per logical core on Linux and macOS. Windows exposes an aggregate figure only, and the UI says so rather than showing a fake bar. | | ||
| 22 | +| **History** | The dashboard keeps the last 60 samples in the page and draws them as a polyline SVG. Nothing is cached on disk. | | ||
| 23 | +| **Memory** | Total, used, available and cached, plus swap. Linux uses `MemAvailable` (not `MemFree`) so the number matches what `free` reports. | | ||
| 24 | +| **Load / uptime** | `/proc/loadavg` + `/proc/uptime` on Linux, `getloadavg` + `kern.boottime` on macOS, `GetTickCount64` on Windows. | | ||
| 25 | +| **Processes** | Top 25 by CPU or by RSS, with client-side filter. CPU comes from a per-process tick delta over the same 80 ms window, not from a `ps` column. | | ||
| 26 | +| **Polling** | 2 s / 5 s / Pause, default 2 s. Polling stops when the tab is hidden and resumes on focus. | | ||
| 27 | +| **Theme** | Follows the Bot theme (`localStorage.bot_theme`), dark by default, no flash. | | ||
| 28 | +| **Embed mode** | `?embed=1` drops the back-link so it fits the chat floating window. | | ||
| 29 | + | ||
| 30 | +### Why it is safe | ||
| 31 | + | ||
| 32 | +**1. Node authentication.** The node API on `:8150` is reachable from the LAN, so the plugin | ||
| 33 | +refuses any request that does not carry the node password. Accepted forms match the rest of | ||
| 34 | +the node API: `X-Bot-Password: <pwd>` (what `bot-web` proxies with), `Authorization: Bearer | ||
| 35 | +<pwd>`, or `?pwd=<pwd>` (what the plugin iframe appends). If `BOT_PASSWORD` is unset the node | ||
| 36 | +is running without a password, which is a deliberate local-dev setup only. `--meta` stays | ||
| 37 | +unauthenticated so discovery keeps working. | ||
| 38 | + | ||
| 39 | +**2. No user input reaches a shell.** The only subprocess in the whole program is a fixed | ||
| 40 | +`ps -Ao pid=,pcpu=,rss=,comm=` on macOS, where the kernel offers no cheap per-process | ||
| 41 | +iterator. Every string it consumes comes from the kernel, not from the request. Query | ||
| 42 | +parameters are parsed into fixed-size buffers and are only ever compared to literals. | ||
| 43 | + | ||
| 44 | +**3. No sockets.** The plugin makes zero outbound connections. It never resolves a hostname, | ||
| 45 | +never opens a port, and never reads a credential. | ||
| 46 | + | ||
| 47 | +**4. `/proc` is streamed, never measured.** `st_size` for `/proc/stat` and `/proc/meminfo` is | ||
| 48 | +always 0, so the code reads with `fgets` until EOF. Seeking to the end and asking for the size | ||
| 49 | +is a classic bug that silently yields an empty buffer. | ||
| 50 | + | ||
| 51 | +--- | ||
| 52 | + | ||
| 53 | +## Install | ||
| 54 | + | ||
| 55 | +### Prebuilt (fastest) | ||
| 56 | + | ||
| 57 | +```bash | ||
| 58 | +curl -fsSL https://dist.scorpiox.net/bot-plugins/sysmon/latest.json | ||
| 59 | +``` | ||
| 60 | + | ||
| 61 | +Pick the target that matches the node, then place the binary in the node plugin directory: | ||
| 62 | + | ||
| 63 | +| Platform | Directory | | ||
| 64 | +|---|---| | ||
| 65 | +| Linux / macOS | `~/.scorpiox/plugins/`, or `/workspace/.scorpiox/plugins/` in a BotCloud container | | ||
| 66 | +| Windows | `%LOCALAPPDATA%\scorpiox\plugins\` | | ||
| 67 | + | ||
| 68 | +```bash | ||
| 69 | +mkdir -p ~/.scorpiox/plugins | ||
| 70 | +chmod +x plugin_sysmon | ||
| 71 | +mv plugin_sysmon ~/.scorpiox/plugins/ | ||
| 72 | +~/.scorpiox/plugins/plugin_sysmon --meta | ||
| 73 | +``` | ||
| 74 | + | ||
| 75 | +No restart is needed — `scorpiox-server` resolves plugin binaries on every request. | ||
| 76 | + | ||
| 77 | +### From source (auditable, customizable) | ||
| 78 | + | ||
| 79 | +```bash | ||
| 80 | +git clone https://plugingit.scorpiox.net/scorpiox-bot-plugin-sysmon.git | ||
| 81 | +cd scorpiox-bot-plugin-sysmon | ||
| 82 | +make # native | ||
| 83 | +make musl # fully static Linux x86_64 | ||
| 84 | +make win64 # static Windows x64 PE (needs MinGW-w64) | ||
| 85 | +make test # 100+ offline assertions against live counters | ||
| 86 | +``` | ||
| 87 | + | ||
| 88 | +--- | ||
| 89 | + | ||
| 90 | +## API | ||
| 91 | + | ||
| 92 | +All responses are `application/json; charset=utf-8`. No `Content-Length` is ever emitted. | ||
| 93 | + | ||
| 94 | +### `GET ?action=metrics` | ||
| 95 | + | ||
| 96 | +```json | ||
| 97 | +{ | ||
| 98 | + "ok": true, | ||
| 99 | + "action": "metrics", | ||
| 100 | + "plugin": "plugin_sysmon", | ||
| 101 | + "version": "1.0.0", | ||
| 102 | + "platform": "linux", | ||
| 103 | + "host": "node-1", | ||
| 104 | + "cpu_model": "Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz", | ||
| 105 | + "utc": "2026-11-11T04:00:00Z", | ||
| 106 | + "cpu_pct": 12.4, | ||
| 107 | + "ncores": 6, | ||
| 108 | + "cores": [{"core": 0, "pct": 10.0}, {"core": 1, "pct": 18.8}], | ||
| 109 | + "mem": {"total_kb": 32710248, "used_kb": 23329068, "free_kb": 9381180, | ||
| 110 | + "cached_kb": 7277468, "pct": 71.3}, | ||
| 111 | + "swap": {"total_kb": 8388604, "used_kb": 1234, "pct": 0.0}, | ||
| 112 | + "uptime_sec": 4077192, | ||
| 113 | + "load": {"available": true, "load1": 12.66, "load5": 13.60, "load15": 12.27}, | ||
| 114 | + "proc_count": 312, | ||
| 115 | + "top": 25, | ||
| 116 | + "sort": "cpu", | ||
| 117 | + "processes": [ | ||
| 118 | + {"pid": 1, "name": "scorpiox", "cpu": 4.5, "rss_kb": 30164, "state": "S"} | ||
| 119 | + ] | ||
| 120 | +} | ||
| 121 | +``` | ||
| 122 | + | ||
| 123 | +Parameters: | ||
| 124 | + | ||
| 125 | +| Param | Values | Default | | ||
| 126 | +|---|---|---| | ||
| 127 | +| `action` | `metrics`, `info` | `metrics` (when `format=json`) | | ||
| 128 | +| `sort` | `cpu`, `mem` | `cpu` | | ||
| 129 | +| `top` | 1–4096 | 25 | | ||
| 130 | + | ||
| 131 | +### `GET ?action=info` | ||
| 132 | + | ||
| 133 | +Plugin self-description: version, platform, PID, timestamp, whether node auth is enforced, | ||
| 134 | +the sample window, and the supported action list. | ||
| 135 | + | ||
| 136 | +### `GET` (no action) | ||
| 137 | + | ||
| 138 | +Serves the HTML dashboard. `?embed=1` / `?embed=true` drops the back-link. | ||
| 139 | + | ||
| 140 | +--- | ||
| 141 | + | ||
| 142 | +## Build matrix | ||
| 143 | + | ||
| 144 | +| Target | Compiler | Artifact | | ||
| 145 | +|---|---|---| | ||
| 146 | +| `linux-x86_64` | `cc` | glibc ELF | | ||
| 147 | +| `linux-x86_64-musl` | `musl-gcc -static` | static ELF, universal across distros | | ||
| 148 | +| `linux-arm64-musl` | `aarch64-linux-musl-gcc` | static ELF for ARM64 servers | | ||
| 149 | +| `macos-arm64` | Apple `clang -arch arm64` | Mach-O | | ||
| 150 | +| `macos-x86_64` | Apple `clang -arch x86_64` | Mach-O | | ||
| 151 | +| `windows-x86_64` | `x86_64-w64-mingw32-gcc -static` | static PE | | ||
| 152 | + | ||
| 153 | +`release-dist.sh` builds what the local machine can build, scans the whole git history for | ||
| 154 | +secrets and LAN addresses, writes `manifest.json`, `SHA256SUMS` and `source.tar.gz`, uploads to | ||
| 155 | +`dist.scorpiox.net/bot-plugins/sysmon/<version>/`, refreshes `latest.json` and the registry | ||
| 156 | +`index.json`, and mirrors the repo to `plugingit.scorpiox.net`. | ||
| 157 | + | ||
| 158 | +--- | ||
| 159 | + | ||
| 160 | +## Licence | ||
| 161 | + | ||
| 162 | +MIT. Copyright (c) 2026 SCORPIOX INC. |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..9ddc5de | index 0000000..9ddc5de | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/build.ps1 | +++ b/build.ps1 | ||
| @@ -0,0 +1,53 @@ | @@ -0,0 +1,53 @@ | ||
| 1 | +# build.ps1 — Windows MinGW-w64 static build for plugin_sysmon | ||
| 2 | +# | ||
| 3 | +# Usage (from the repo root, PowerShell): | ||
| 4 | +# powershell -ExecutionPolicy Bypass -File build.ps1 | ||
| 5 | +# | ||
| 6 | +# Produces .\plugin_sysmon.exe (static PE, runs on any Windows 10/11 x64 node). | ||
| 7 | +# Requires x86_64-w64-mingw32-gcc or a WinLibs/UCRT gcc on PATH (or C:\gcc16\bin). | ||
| 8 | + | ||
| 9 | +$ErrorActionPreference = "Stop" | ||
| 10 | +$ScriptDir = $PSScriptRoot | ||
| 11 | +Push-Location $ScriptDir | ||
| 12 | +try { | ||
| 13 | + $Src = Join-Path $ScriptDir "src\plugin_sysmon.c" | ||
| 14 | + $Out = Join-Path $ScriptDir "plugin_sysmon.exe" | ||
| 15 | + $Cc = "x86_64-w64-mingw32-gcc" | ||
| 16 | + | ||
| 17 | + if (-not (Test-Path $Src)) { throw "source not found: $Src" } | ||
| 18 | + | ||
| 19 | + # Prefer MinGW-w64, fall back to a plain gcc (WinLibs / MSYS2 UCRT) if present. | ||
| 20 | + if (-not (Get-Command $Cc -ErrorAction SilentlyContinue)) { | ||
| 21 | + if (Test-Path "C:\gcc16\bin\gcc.exe") { | ||
| 22 | + $Cc = "C:\gcc16\bin\gcc.exe" | ||
| 23 | + } elseif (Get-Command gcc -ErrorAction SilentlyContinue) { | ||
| 24 | + $Cc = "gcc" | ||
| 25 | + } else { | ||
| 26 | + throw "no C compiler found (need x86_64-w64-mingw32-gcc or gcc)" | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + Write-Host "==> building plugin_sysmon.exe with $Cc" | ||
| 31 | + & $Cc -O2 -std=c99 -Wall -Wextra -static -o $Out $Src -lpsapi | ||
| 32 | + if ($LASTEXITCODE -ne 0) { throw "compile failed (exit $LASTEXITCODE)" } | ||
| 33 | + | ||
| 34 | + if (-not (Test-Path $Out)) { throw "build produced no output" } | ||
| 35 | + | ||
| 36 | + # Sanity: a real PE starts with 'MZ'. An HTML error page saved as .exe does not. | ||
| 37 | + $bytes = [System.IO.File]::ReadAllBytes($Out) | ||
| 38 | + if ($bytes.Length -lt 2 -or $bytes[0] -ne 0x4D -or $bytes[1] -ne 0x5A) { | ||
| 39 | + throw "output is not a PE executable (missing MZ header)" | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + Write-Host "==> metadata check" | ||
| 43 | + & $Out --meta | ||
| 44 | + if ($LASTEXITCODE -ne 0) { throw "--meta failed" } | ||
| 45 | + | ||
| 46 | + $size = (Get-Item $Out).Length | ||
| 47 | + Write-Host "" | ||
| 48 | + Write-Host "Built $Out ($size bytes)" | ||
| 49 | + Write-Host "Install: Copy-Item plugin_sysmon.exe `"`$env:LOCALAPPDATA\scorpiox\plugins\`"" | ||
| 50 | +} | ||
| 51 | +finally { | ||
| 52 | + Pop-Location | ||
| 53 | +} |
| 1 | new file mode 100755 | 1 | new file mode 100755 |
| index 0000000..1ef4a00 | index 0000000..1ef4a00 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/release-dist.sh | +++ b/release-dist.sh | ||
| @@ -0,0 +1,362 @@ | @@ -0,0 +1,362 @@ | ||
| 1 | +#!/usr/bin/env bash | ||
| 2 | +# release-dist.sh — build, scan and publish plugin_sysmon to the plugin registry. | ||
| 3 | +# | ||
| 4 | +# Two paths: | ||
| 5 | +# A. Preferred: if `scorpiox-cli-botplugin` is installed, hand the whole job to it | ||
| 6 | +# (`scorpiox-cli-botplugin publish .`), which does the same work with a richer CLI. | ||
| 7 | +# B. Standalone (this script): secret scan -> multi-target build -> source.tar.gz -> | ||
| 8 | +# manifest.json + SHA256SUMS -> upload -> latest.json / index.json -> plugingit mirror. | ||
| 9 | +# | ||
| 10 | +# Nothing here contains a host address or a credential. Everything environment-specific | ||
| 11 | +# comes from the environment, so this file stays safe to publish: | ||
| 12 | +# | ||
| 13 | +# DIST_HOST ssh target for the dist server (required for upload) | ||
| 14 | +# DIST_PASS ssh password for DIST_HOST (optional, defaults to ssh-agent/key) | ||
| 15 | +# DIST_PATH remote web root of the dist server (default /data/dist) | ||
| 16 | +# MAC_HOST ssh target for a macOS builder (optional: macos-arm64/x86_64) | ||
| 17 | +# MAC_USER / MAC_PASS (optional) | ||
| 18 | +# PLUGINGIT_TOKEN_FILE file holding the plugingit write token (default /etc/scorpiox-plugingit.token) | ||
| 19 | +# PLUGINGIT_REMOTE public mirror URL to push into | ||
| 20 | +# | ||
| 21 | +# Usage: | ||
| 22 | +# ./release-dist.sh # full release | ||
| 23 | +# ./release-dist.sh --dry-run # build + stage in /tmp, no upload | ||
| 24 | +# ./release-dist.sh --no-macos # skip the macOS legs | ||
| 25 | +# ./release-dist.sh --no-mirror # skip the plugingit mirror | ||
| 26 | + | ||
| 27 | +set -euo pipefail | ||
| 28 | + | ||
| 29 | +SLUG="sysmon" | ||
| 30 | +ID="plugin_sysmon" | ||
| 31 | +VERSION="1.0.0" | ||
| 32 | +DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" | ||
| 33 | + | ||
| 34 | +ROOT="$(cd "$(dirname "$0")" && pwd)" | ||
| 35 | +cd "$ROOT" | ||
| 36 | + | ||
| 37 | +DRY_RUN=0 | ||
| 38 | +DO_MACOS=1 | ||
| 39 | +DO_MIRROR=1 | ||
| 40 | +for arg in "$@"; do | ||
| 41 | + case "$arg" in | ||
| 42 | + --dry-run) DRY_RUN=1 ;; | ||
| 43 | + --no-macos) DO_MACOS=0 ;; | ||
| 44 | + --no-mirror) DO_MIRROR=0 ;; | ||
| 45 | + -h|--help) sed -n '2,30p' "$0"; exit 0 ;; | ||
| 46 | + *) echo "unknown argument: $arg" >&2; exit 2 ;; | ||
| 47 | + esac | ||
| 48 | +done | ||
| 49 | + | ||
| 50 | +DIST_HOST="${DIST_HOST:-}" | ||
| 51 | +DIST_PASS="${DIST_PASS:-}" | ||
| 52 | +DIST_PATH="${DIST_PATH:-/data/dist}" | ||
| 53 | +PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}" | ||
| 54 | +PLUGINGIT_REMOTE="${PLUGINGIT_REMOTE:-https://plugingit.scorpiox.net/scorpiox-bot-plugin-${SLUG}.git}" | ||
| 55 | + | ||
| 56 | +STAGE="/tmp/${SLUG}-dist-${VERSION}-$$" | ||
| 57 | +mkdir -p "$STAGE" | ||
| 58 | +trap 'rm -rf "$STAGE"' EXIT | ||
| 59 | + | ||
| 60 | +say() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; } | ||
| 61 | +die() { printf '\033[31mERROR:\033[0m %s\n' "$1" >&2; exit 1; } | ||
| 62 | + | ||
| 63 | +# ---------------------------------------------------------------- 0. preflight | ||
| 64 | +say "0/7 preflight" | ||
| 65 | + | ||
| 66 | +if command -v scorpiox-cli-botplugin >/dev/null 2>&1; then | ||
| 67 | + say "using scorpiox-cli-botplugin publish" | ||
| 68 | + exec scorpiox-cli-botplugin publish "$ROOT" | ||
| 69 | +fi | ||
| 70 | +echo " scorpiox-cli-botplugin not installed — running standalone pipeline" | ||
| 71 | + | ||
| 72 | +[ -n "$(git status --porcelain 2>/dev/null)" ] && \ | ||
| 73 | + echo " note: working tree is dirty; source.tar.gz comes from the last commit" | ||
| 74 | +COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown") | ||
| 75 | +echo " version $VERSION" | ||
| 76 | +echo " commit $COMMIT" | ||
| 77 | + | ||
| 78 | +# ---------------------------------------------------------------- 1. secret scan | ||
| 79 | +say "1/7 secret & LAN scan (full history)" | ||
| 80 | + | ||
| 81 | +SCAN_TMP="$STAGE/history.txt" | ||
| 82 | +# Scan every added/removed line of every commit. Two exclusions, both about the | ||
| 83 | +# audit rather than the code: the committer identity line, and any line that is | ||
| 84 | +# itself a scan definition — otherwise this file's own detector patterns read as | ||
| 85 | +# findings in every historical revision of it. | ||
| 86 | +git log -p --all 2>/dev/null \ | ||
| 87 | + | grep -v '^Author: ' \ | ||
| 88 | + | grep -vE 'scan "|HITS=|grep -E|P_ROOT=|P_WT=|\$ALL' \ | ||
| 89 | + > "$SCAN_TMP" || : > "$SCAN_TMP" | ||
| 90 | +HITS=0 | ||
| 91 | +scan() { # scan <label> <regex> | ||
| 92 | + local out | ||
| 93 | + out=$(grep -Eo "$2" "$SCAN_TMP" 2>/dev/null | sort -u | head -5 || true) | ||
| 94 | + if [ -n "$out" ]; then | ||
| 95 | + printf ' \033[31mHIT\033[0m %s\n' "$1" | ||
| 96 | + printf '%s\n' "$out" | sed 's/^/ /' | ||
| 97 | + HITS=$((HITS + 1)) | ||
| 98 | + else | ||
| 99 | + printf ' ok %s\n' "$1" | ||
| 100 | + fi | ||
| 101 | +} | ||
| 102 | +scan "LAN IPs 192.168.x.x" '192\.168\.[0-9]+\.[0-9]+' | ||
| 103 | +scan "private 10.x.x.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' | ||
| 104 | +scan "private 172.16-31.x.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' | ||
| 105 | +scan "GitHub PAT" 'ghp_[A-Za-z0-9]{36}' | ||
| 106 | +scan "HuggingFace token" 'hf_[A-Za-z0-9]{30,}' | ||
| 107 | +scan "embedded git credentials" 'scorpioxinc:[A-Za-z0-9]{20,}' | ||
| 108 | +scan "hard-coded ssh password" 'sshpass -p [^"$]' | ||
| 109 | +P_ROOT="/codebases" | ||
| 110 | +P_PICOBOT="${P_ROOT}/pico""bot" | ||
| 111 | +P_WT=".work""trees" | ||
| 112 | +scan "internal fleet paths" "/root/tools|${P_PICOBOT}|${P_WT}" | ||
| 113 | +scan "run-token env assignment" 'RUNNER_'"PASS=" | ||
| 114 | +if [ "$HITS" -ne 0 ]; then | ||
| 115 | + die "secret scan found $HITS problem(s) — refusing to publish" | ||
| 116 | +fi | ||
| 117 | +echo " clean: 0 hits" | ||
| 118 | + | ||
| 119 | +# ---------------------------------------------------------------- 2. build | ||
| 120 | +say "2/7 build targets" | ||
| 121 | + | ||
| 122 | +build_ok() { [ -f "$1" ]; } | ||
| 123 | + | ||
| 124 | +# linux-x86_64 (glibc, native) | ||
| 125 | +if command -v cc >/dev/null 2>&1; then | ||
| 126 | + cc -O2 -std=c99 -Wall -Wextra -o "$STAGE/$ID" src/$ID.c || die "native build failed" | ||
| 127 | + echo " built linux-x86_64 (glibc) $(wc -c < "$STAGE/$ID" | tr -d ' ') bytes" | ||
| 128 | +else | ||
| 129 | + die "no native cc found" | ||
| 130 | +fi | ||
| 131 | + | ||
| 132 | +# linux-x86_64-musl (fully static — the universal Linux artifact) | ||
| 133 | +if command -v musl-gcc >/dev/null 2>&1; then | ||
| 134 | + musl-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.musl" src/$ID.c \ | ||
| 135 | + || die "musl build failed" | ||
| 136 | + echo " built linux-x86_64-musl $(wc -c < "$STAGE/$ID.musl" | tr -d ' ') bytes" | ||
| 137 | +else | ||
| 138 | + echo " skip linux-x86_64-musl (no musl-gcc)" | ||
| 139 | +fi | ||
| 140 | + | ||
| 141 | +# linux-arm64-musl (static aarch64 — Pi 5 / ARM64 servers) | ||
| 142 | +if command -v aarch64-linux-musl-gcc >/dev/null 2>&1; then | ||
| 143 | + aarch64-linux-musl-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.arm64" src/$ID.c \ | ||
| 144 | + || die "arm64 musl build failed" | ||
| 145 | + echo " built linux-arm64-musl $(wc -c < "$STAGE/$ID.arm64" | tr -d ' ') bytes" | ||
| 146 | +else | ||
| 147 | + echo " skip linux-arm64-musl (no aarch64-linux-musl-gcc)" | ||
| 148 | +fi | ||
| 149 | + | ||
| 150 | +# windows-x86_64 (static PE) | ||
| 151 | +if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then | ||
| 152 | + x86_64-w64-mingw32-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.exe" src/$ID.c -lpsapi \ | ||
| 153 | + || die "MinGW build failed" | ||
| 154 | + [ "$(head -c 2 "$STAGE/$ID.exe")" = "MZ" ] || die "windows artifact is not a PE" | ||
| 155 | + echo " built windows-x86_64 $(wc -c < "$STAGE/$ID.exe" | tr -d ' ') bytes" | ||
| 156 | +else | ||
| 157 | + echo " skip windows-x86_64 (no MinGW cross compiler)" | ||
| 158 | +fi | ||
| 159 | + | ||
| 160 | +# macOS legs — Mach-O must be produced by Apple clang on a real Mac. | ||
| 161 | +# Both slices are cross-built from one host: -arch arm64 and -arch x86_64. | ||
| 162 | +if [ "$DO_MACOS" -eq 1 ] && [ -n "${MAC_HOST:-}" ]; then | ||
| 163 | + MAC_USER="${MAC_USER:-$(whoami)}" | ||
| 164 | + MAC_DIR="/tmp/${ID}-mac-$$" | ||
| 165 | + echo " building macOS legs on $MAC_HOST" | ||
| 166 | + SSH_MAC=(ssh -o StrictHostKeyChecking=no) | ||
| 167 | + SCP_MAC=(scp -o StrictHostKeyChecking=no) | ||
| 168 | + if [ -n "${MAC_PASS:-}" ]; then | ||
| 169 | + SSH_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SSH_MAC[@]}") | ||
| 170 | + SCP_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SCP_MAC[@]}") | ||
| 171 | + fi | ||
| 172 | + if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR' && mkdir -p '$MAC_DIR'"; then | ||
| 173 | + if "${SCP_MAC[@]}" src/$ID.c "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.c"; then | ||
| 174 | + # Build straight from the uploaded file — no src/ tree on the remote. | ||
| 175 | + for slice in arm64 x86_64; do | ||
| 176 | + if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \ | ||
| 177 | + "cd '$MAC_DIR' && clang -O2 -std=c99 -Wall -Wextra -arch $slice -o $ID.$slice $ID.c" \ | ||
| 178 | + 2>/dev/null; then | ||
| 179 | + "${SCP_MAC[@]}" "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.$slice" "$STAGE/$ID.macos-$slice" 2>/dev/null \ | ||
| 180 | + && echo " built macos-$slice ($(wc -c < "$STAGE/$ID.macos-$slice" | tr -d ' ') bytes)" | ||
| 181 | + else | ||
| 182 | + echo " warn: macos-$slice build failed on $MAC_HOST" | ||
| 183 | + fi | ||
| 184 | + done | ||
| 185 | + "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR'" 2>/dev/null || true | ||
| 186 | + else | ||
| 187 | + echo " warn: mac scp failed" | ||
| 188 | + fi | ||
| 189 | + else | ||
| 190 | + echo " warn: mac host unreachable" | ||
| 191 | + fi | ||
| 192 | +else | ||
| 193 | + echo " skip macOS legs (set MAC_HOST/MAC_USER/MAC_PASS to include them)" | ||
| 194 | +fi | ||
| 195 | + | ||
| 196 | +# ---------------------------------------------------------------- 3. package | ||
| 197 | +say "3/7 package" | ||
| 198 | + | ||
| 199 | +git archive --format=tar.gz --prefix="${ID}-${VERSION}/" \ | ||
| 200 | + -o "$STAGE/source.tar.gz" HEAD 2>/dev/null \ | ||
| 201 | + || tar -czf "$STAGE/source.tar.gz" --exclude=.git . | ||
| 202 | +echo " source.tar.gz $(wc -c < "$STAGE/source.tar.gz" | tr -d ' ') bytes" | ||
| 203 | + | ||
| 204 | +target_meta() { # target_meta <key> <file> <format> <relpath> <name> | ||
| 205 | + local key="$1" file="$2" fmt="$3" rel="$4" name="$5" | ||
| 206 | + [ -f "$file" ] || return 0 | ||
| 207 | + local sha size | ||
| 208 | + sha=$(sha256sum "$file" | awk '{print $1}') | ||
| 209 | + size=$(wc -c < "$file" | tr -d ' ') | ||
| 210 | + printf ' "%s": {"url": "%s/%s/%s/%s", "sha256": "%s", "size_bytes": %s, "format": "%s"}' \ | ||
| 211 | + "$key" "$DIST_BASE_URL" "$SLUG" "$VERSION" "$rel" "$sha" "$size" "$fmt" | ||
| 212 | +} | ||
| 213 | + | ||
| 214 | +{ | ||
| 215 | + printf '{\n' | ||
| 216 | + printf ' "slug": "%s",\n' "$SLUG" | ||
| 217 | + printf ' "id": "%s",\n' "$ID" | ||
| 218 | + printf ' "name": "sysmon",\n' | ||
| 219 | + printf ' "desc": "CPU & memory",\n' | ||
| 220 | + printf ' "title": "Local System Monitor",\n' | ||
| 221 | + printf ' "summary": "Zero-dependency local CPU, per-core load, memory, swap and top-process monitor for Linux, macOS and Windows nodes - sampled live from kernel counters in one C99 binary.",\n' | ||
| 222 | + printf ' "category": "system",\n' | ||
| 223 | + printf ' "version": "%s",\n' "$VERSION" | ||
| 224 | + printf ' "license": "MIT",\n' | ||
| 225 | + printf ' "trust_notice": "Executes native C code on your node. Reads local /proc, mach or Win32 counters for CPU, memory and processes; makes zero outbound network requests and opens no sockets.",\n' | ||
| 226 | + printf ' "capabilities": ["procfs_read", "local_process_list"],\n' | ||
| 227 | + printf ' "git_repo_url": "https://plugingit.scorpiox.net/scorpiox-bot-plugin-%s.git",\n' "$SLUG" | ||
| 228 | + printf ' "git_web_url": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-%s",\n' "$SLUG" | ||
| 229 | + printf ' "git_commit": "%s",\n' "$COMMIT" | ||
| 230 | + printf ' "source_tarball_url": "%s/%s/%s/source.tar.gz",\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" | ||
| 231 | + printf ' "source_tarball_sha256": "%s",\n' "$(sha256sum "$STAGE/source.tar.gz" | awk '{print $1}')" | ||
| 232 | + printf ' "build_cmd_posix": "cc -O2 -std=c99 -Wall -Wextra -o %s src/%s.c",\n' "$ID" "$ID" | ||
| 233 | + printf ' "build_cmd_win64": "x86_64-w64-mingw32-gcc -O2 -std=c99 -static -o %s.exe src/%s.c -lpsapi",\n' "$ID" "$ID" | ||
| 234 | + printf ' "published_at": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" | ||
| 235 | + printf ' "targets": {\n' | ||
| 236 | + first=1 | ||
| 237 | + emit() { if [ -f "$2" ]; then [ $first -eq 1 ] || printf ',\n'; first=0; target_meta "$@"; fi; } | ||
| 238 | + emit "linux-x86_64" "$STAGE/$ID" "elf-x86_64" "linux-x86_64/$ID" "$ID" | ||
| 239 | + emit "linux-x86_64-musl" "$STAGE/$ID.musl" "elf-x86_64-static" "linux-x86_64-musl/$ID" "$ID" | ||
| 240 | + emit "linux-arm64-musl" "$STAGE/$ID.arm64" "elf-arm64-static" "linux-arm64-musl/$ID" "$ID" | ||
| 241 | + emit "macos-arm64" "$STAGE/$ID.macos-arm64" "macho-arm64" "macos-arm64/$ID" "$ID" | ||
| 242 | + emit "macos-x86_64" "$STAGE/$ID.macos-x86_64" "macho-x86_64" "macos-x86_64/$ID" "$ID" | ||
| 243 | + emit "windows-x86_64" "$STAGE/$ID.exe" "pe-x86_64" "windows-x86_64/$ID.exe" "$ID" | ||
| 244 | + printf '\n }\n}\n' | ||
| 245 | +} > "$STAGE/manifest.json" | ||
| 246 | + | ||
| 247 | +python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$STAGE/manifest.json" \ | ||
| 248 | + || die "generated manifest.json is not valid JSON" | ||
| 249 | +echo " manifest.json ok" | ||
| 250 | + | ||
| 251 | +# SHA256SUMS — relative paths, exactly as published | ||
| 252 | +( cd "$STAGE" | ||
| 253 | + for f in source.tar.gz; do [ -f "$f" ] && sha256sum "$f"; done | ||
| 254 | + [ -f "$ID" ] && sha256sum "$ID" | sed "s| $ID\$| linux-x86_64/$ID|" | ||
| 255 | + [ -f "$ID.musl" ] && sha256sum "$ID.musl" | sed "s| $ID.musl\$| linux-x86_64-musl/$ID|" | ||
| 256 | + [ -f "$ID.arm64" ] && sha256sum "$ID.arm64" | sed "s| $ID.arm64\$| linux-arm64-musl/$ID|" | ||
| 257 | + [ -f "$ID.macos-arm64" ]&& sha256sum "$ID.macos-arm64"| sed "s| $ID.macos-arm64\$| macos-arm64/$ID|" | ||
| 258 | + [ -f "$ID.macos-x86_64" ]&& sha256sum "$ID.macos-x86_64"| sed "s| $ID.macos-x86_64\$| macos-x86_64/$ID|" | ||
| 259 | + [ -f "$ID.exe" ] && sha256sum "$ID.exe" | sed "s| $ID.exe\$| windows-x86_64/$ID.exe|" | ||
| 260 | + true ) > "$STAGE/SHA256SUMS" | ||
| 261 | +echo " SHA256SUMS $(wc -l < "$STAGE/SHA256SUMS" | tr -d ' ') entries" | ||
| 262 | + | ||
| 263 | +# ---------------------------------------------------------------- 4. stage | ||
| 264 | +say "4/7 stage tree" | ||
| 265 | +PKG="$STAGE/pkg" | ||
| 266 | +mkdir -p "$PKG/linux-x86_64" "$PKG/linux-x86_64-musl" "$PKG/linux-arm64-musl" \ | ||
| 267 | + "$PKG/macos-arm64" "$PKG/macos-x86_64" "$PKG/windows-x86_64" | ||
| 268 | +cp -f "$STAGE/$ID" "$PKG/linux-x86_64/$ID" | ||
| 269 | +[ -f "$STAGE/$ID.musl" ] && cp -f "$STAGE/$ID.musl" "$PKG/linux-x86_64-musl/$ID" | ||
| 270 | +[ -f "$STAGE/$ID.arm64" ] && cp -f "$STAGE/$ID.arm64" "$PKG/linux-arm64-musl/$ID" | ||
| 271 | +[ -f "$STAGE/$ID.macos-arm64" ]&& cp -f "$STAGE/$ID.macos-arm64" "$PKG/macos-arm64/$ID" | ||
| 272 | +[ -f "$STAGE/$ID.macos-x86_64" ]&&cp -f "$STAGE/$ID.macos-x86_64" "$PKG/macos-x86_64/$ID" | ||
| 273 | +[ -f "$STAGE/$ID.exe" ] && cp -f "$STAGE/$ID.exe" "$PKG/windows-x86_64/$ID.exe" | ||
| 274 | +cp -f "$STAGE/manifest.json" "$STAGE/SHA256SUMS" "$STAGE/source.tar.gz" "$PKG/" | ||
| 275 | +chmod +x "$PKG"/*/$ID 2>/dev/null || true | ||
| 276 | +find "$PKG" -type f | sed 's/^/ /' | ||
| 277 | + | ||
| 278 | +if [ "$DRY_RUN" -eq 1 ]; then | ||
| 279 | + say "dry run — staged at $STAGE/pkg (kept for inspection)" | ||
| 280 | + trap - EXIT | ||
| 281 | + exit 0 | ||
| 282 | +fi | ||
| 283 | + | ||
| 284 | +# ---------------------------------------------------------------- 5. upload | ||
| 285 | +say "5/7 upload" | ||
| 286 | + | ||
| 287 | +[ -n "$DIST_HOST" ] || die "DIST_HOST is not set — cannot upload" | ||
| 288 | +SSH=(ssh -o StrictHostKeyChecking=no) | ||
| 289 | +SCP=(scp -o StrictHostKeyChecking=no) | ||
| 290 | +if [ -n "$DIST_PASS" ]; then | ||
| 291 | + SSH=(scorpiox-sshpass -p "$DIST_PASS" "${SSH[@]}") | ||
| 292 | + SCP=(scorpiox-sshpass -p "$DIST_PASS" "${SCP[@]}") | ||
| 293 | +fi | ||
| 294 | +REMOTE_DIR="$DIST_PATH/bot-plugins/$SLUG/$VERSION" | ||
| 295 | + | ||
| 296 | +"${SSH[@]}" "$DIST_HOST" "mkdir -p '$REMOTE_DIR' '$DIST_PATH/bot-plugins/$SLUG'" \ | ||
| 297 | + || die "cannot create $REMOTE_DIR on $DIST_HOST" | ||
| 298 | +# Stage to one tarball, transfer it, extract remotely. Streaming tar through | ||
| 299 | +# ssh corrupts on some OpenSSH builds (remote gzip dies with SIGPIPE), and | ||
| 300 | +# OpenSSH 9 refuses to scp a bare "." source. | ||
| 301 | +tar -czf "$STAGE/pkg.tar.gz" -C "$PKG" . || die "packaging failed" | ||
| 302 | +"${SCP[@]}" -q "$STAGE/pkg.tar.gz" "$DIST_HOST:$REMOTE_DIR/pkg.tar.gz" || die "upload failed" | ||
| 303 | +"${SSH[@]}" "$DIST_HOST" "cd '$REMOTE_DIR' && tar -xzf pkg.tar.gz && rm -f pkg.tar.gz" \ | ||
| 304 | + || die "remote extract failed" | ||
| 305 | +echo " uploaded to $DIST_HOST:$REMOTE_DIR" | ||
| 306 | + | ||
| 307 | +# latest.json = copy of this version's manifest | ||
| 308 | +"${SSH[@]}" "$DIST_HOST" "cp -f '$REMOTE_DIR/manifest.json' '$DIST_PATH/bot-plugins/$SLUG/latest.json'" \ | ||
| 309 | + || die "latest.json update failed" | ||
| 310 | +echo " updated bot-plugins/$SLUG/latest.json" | ||
| 311 | + | ||
| 312 | +# index.json — merge, preserving every sibling <slug>/latest.json | ||
| 313 | +say "index.json merge" | ||
| 314 | +"${SSH[@]}" "$DIST_HOST" "cd '$DIST_PATH/bot-plugins' && python3 - '$SLUG' <<'PY' | ||
| 315 | +import json, os, sys, glob | ||
| 316 | +slug = sys.argv[1] | ||
| 317 | +entries = {} | ||
| 318 | +for p in sorted(glob.glob('*/latest.json')): | ||
| 319 | + s = os.path.dirname(p) | ||
| 320 | + try: | ||
| 321 | + entries[s] = json.load(open(p)) | ||
| 322 | + except Exception: | ||
| 323 | + pass | ||
| 324 | +index = { | ||
| 325 | + 'ok': True, | ||
| 326 | + 'service': 'bot-plugins-dist', | ||
| 327 | + 'dist_base_url': 'https://dist.scorpiox.net/bot-plugins', | ||
| 328 | + 'count': len(entries), | ||
| 329 | + 'slugs': sorted(entries), | ||
| 330 | + 'plugins': [entries[k] for k in sorted(entries)], | ||
| 331 | +} | ||
| 332 | +open('index.json.tmp', 'w').write(json.dumps(index, indent=2) + chr(10)) | ||
| 333 | +os.replace('index.json.tmp', 'index.json') | ||
| 334 | +print(' index.json now has %d plugin(s): %s' % (len(entries), ', '.join(sorted(entries)))) | ||
| 335 | +PY" || die "index.json merge failed" | ||
| 336 | + | ||
| 337 | +# ---------------------------------------------------------------- 6. mirror | ||
| 338 | +say "6/7 plugingit mirror" | ||
| 339 | +if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then | ||
| 340 | + TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE") | ||
| 341 | + AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://x-access-token:${TOKEN}@|") | ||
| 342 | + git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed" | ||
| 343 | + git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true | ||
| 344 | + echo " mirrored to $PLUGINGIT_REMOTE" | ||
| 345 | +else | ||
| 346 | + echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)" | ||
| 347 | +fi | ||
| 348 | + | ||
| 349 | +# ---------------------------------------------------------------- 7. verify | ||
| 350 | +say "7/7 verify" | ||
| 351 | +CODE=$(curl -fsS -o "$STAGE/latest.check" -w '%{http_code}' \ | ||
| 352 | + "$DIST_BASE_URL/$SLUG/latest.json" || echo 000) | ||
| 353 | +if [ "$CODE" = "200" ]; then | ||
| 354 | + python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(" latest.json OK — %s v%s, %d target(s)" % (d["slug"], d["version"], len(d["targets"])))' \ | ||
| 355 | + "$STAGE/latest.check" || echo " warn: latest.json is not valid JSON" | ||
| 356 | +else | ||
| 357 | + echo " warn: $DIST_BASE_URL/$SLUG/latest.json returned HTTP $CODE" | ||
| 358 | +fi | ||
| 359 | + | ||
| 360 | +printf '\n\033[1mrelease complete\033[0m\n' | ||
| 361 | +printf ' registry %s/%s/%s/\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" | ||
| 362 | +printf ' manifest %s/%s/%s/manifest.json\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" |
| 1 | new file mode 100755 | 1 | new file mode 100755 |
| index 0000000..2c08116 | index 0000000..2c08116 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/tests/run.sh | +++ b/tests/run.sh | ||
| @@ -0,0 +1,326 @@ | @@ -0,0 +1,326 @@ | ||
| 1 | +#!/bin/sh | ||
| 2 | +# tests/run.sh - offline checks for plugin_sysmon across all 3 modes. | ||
| 3 | +# | ||
| 4 | +# Runs against ./plugin_sysmon (native build) on the live machine, so the | ||
| 5 | +# metrics assertions use this node's real /proc (or mach / Win32) counters. | ||
| 6 | +# If a musl static build or a Windows PE (plugin_sysmon.exe) is present they are | ||
| 7 | +# checked too. The whole git history is finally audited for secrets and LAN IPs. | ||
| 8 | +# | ||
| 9 | +# Exit code 0 = everything passed. | ||
| 10 | + | ||
| 11 | +set -u | ||
| 12 | + | ||
| 13 | +ROOT=$(cd "$(dirname "$0")/.." && pwd) | ||
| 14 | +cd "$ROOT" || exit 1 | ||
| 15 | + | ||
| 16 | +BIN=./plugin_sysmon | ||
| 17 | +PASS=0 | ||
| 18 | +FAIL=0 | ||
| 19 | + | ||
| 20 | +if [ -t 1 ]; then | ||
| 21 | + C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m') | ||
| 22 | +else | ||
| 23 | + C_OK=; C_BAD=; C_B=; C_0= | ||
| 24 | +fi | ||
| 25 | +pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; } | ||
| 26 | +fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; } | ||
| 27 | + | ||
| 28 | +check() { # check <desc> <expected-substring> <actual> | ||
| 29 | + if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else | ||
| 30 | + fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 300)" | ||
| 31 | + fi | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +check_not() { # check_not <desc> <forbidden-substring> <actual> | ||
| 35 | + if printf '%s' "$3" | grep -F -q -- "$2"; then | ||
| 36 | + fail "$1 (must NOT contain: $2)"; else pass "$1" | ||
| 37 | + fi | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } | ||
| 41 | + | ||
| 42 | +body() { printf '%s' "$1" | sed '1,/^\r$/d'; } | ||
| 43 | + | ||
| 44 | +# ---------------------------------------------------------------- build | ||
| 45 | +section "build" | ||
| 46 | +if [ ! -x "$BIN" ]; then | ||
| 47 | + echo " building native binary..." | ||
| 48 | + cc -O2 -std=c99 -Wall -Wextra -o "$BIN" src/plugin_sysmon.c || { echo " build failed"; exit 1; } | ||
| 49 | +fi | ||
| 50 | +[ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing" | ||
| 51 | + | ||
| 52 | +# ---------------------------------------------------------------- meta mode | ||
| 53 | +section "mode 1 --meta (must work without auth - discovery)" | ||
| 54 | +META=$("$BIN" --meta) | ||
| 55 | +check "meta emits name sysmon" '"name":"sysmon"' "$META" | ||
| 56 | +check "meta emits desc CPU & memory" '"desc":"CPU & memory"' "$META" | ||
| 57 | +METAENV=$(SX_PLUGIN_META=1 "$BIN") | ||
| 58 | +check "SX_PLUGIN_META=1 yields same meta" '"name":"sysmon"' "$METAENV" | ||
| 59 | +LEN=$(printf '%s' "$META" | wc -c | tr -d ' ') | ||
| 60 | +[ "$LEN" -lt 60 ] && pass "meta payload is compact ($LEN bytes)" || fail "meta payload too long ($LEN)" | ||
| 61 | +printf '%s' "$META" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ | ||
| 62 | + && pass "meta is valid JSON" || fail "meta is not valid JSON" | ||
| 63 | +DESC=$(printf '%s' "$META" | sed 's/.*"desc":"\([^"]*\)".*/\1/') | ||
| 64 | +[ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})" | ||
| 65 | +NAME=$(printf '%s' "$META" | sed 's/.*"name":"\([^"]*\)".*/\1/') | ||
| 66 | +[ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces" | ||
| 67 | +check "--version prints version" '1.0.0' "$("$BIN" --version)" | ||
| 68 | +check "--help is available" 'usage:' "$("$BIN" --help)" | ||
| 69 | +META_AUTH=$(BOT_PASSWORD=secret "$BIN" --meta) | ||
| 70 | +check "meta still works when BOT_PASSWORD is set" '"name":"sysmon"' "$META_AUTH" | ||
| 71 | + | ||
| 72 | +# ---------------------------------------------------------------- auth | ||
| 73 | +section "node auth" | ||
| 74 | +AUTHNONE=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics' "$BIN") | ||
| 75 | +check "no credentials -> 401" 'Status: 401' "$AUTHNONE" | ||
| 76 | +check "401 body is json" 'invalid node password' "$AUTHNONE" | ||
| 77 | +AUTHH=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING='action=metrics' "$BIN") | ||
| 78 | +check "X-Bot-Password accepted" '"ok":true' "$(body "$AUTHH")" | ||
| 79 | +AUTHB=$(BOT_PASSWORD=testpw HTTP_AUTHORIZATION='Bearer testpw' QUERY_STRING='action=metrics' "$BIN") | ||
| 80 | +check "Authorization Bearer accepted" '"ok":true' "$(body "$AUTHB")" | ||
| 81 | +AUTHQ=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics&pwd=testpw' "$BIN") | ||
| 82 | +check "?pwd= accepted (plugin iframe)" '"ok":true' "$(body "$AUTHQ")" | ||
| 83 | +AUTHBAD=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics&pwd=wrong' "$BIN") | ||
| 84 | +check "wrong ?pwd= rejected" 'Status: 401' "$AUTHBAD" | ||
| 85 | +AUTHHTML=$(BOT_PASSWORD=testpw QUERY_STRING='' "$BIN") | ||
| 86 | +check "html also requires auth" 'Status: 401' "$AUTHHTML" | ||
| 87 | + | ||
| 88 | +# ---------------------------------------------------------------- metrics | ||
| 89 | +section "mode 2 action=metrics (live counters)" | ||
| 90 | +RAW=$(QUERY_STRING='action=metrics' "$BIN") | ||
| 91 | +MB=$(body "$RAW") | ||
| 92 | +check_not "metrics has no Content-Length" 'Content-Length' "$RAW" | ||
| 93 | +check "metrics content-type json" 'Content-Type: application/json' "$RAW" | ||
| 94 | +check "metrics ok:true" '"ok":true' "$MB" | ||
| 95 | +check "metrics action echoed" '"action":"metrics"' "$MB" | ||
| 96 | +check "metrics has cpu_pct" '"cpu_pct":' "$MB" | ||
| 97 | +check "metrics has ncores" '"ncores":' "$MB" | ||
| 98 | +check "metrics has mem block" '"mem":{"total_kb":' "$MB" | ||
| 99 | +check "metrics has swap block" '"swap":{"total_kb":' "$MB" | ||
| 100 | +check "metrics has uptime" '"uptime_sec":' "$MB" | ||
| 101 | +check "metrics has load block" '"load":{"available":' "$MB" | ||
| 102 | +check "metrics has processes" '"processes":[' "$MB" | ||
| 103 | +check "metrics has host" '"host":' "$MB" | ||
| 104 | +check "metrics has cpu_model" '"cpu_model":' "$MB" | ||
| 105 | +check "metrics has utc" '"utc":"20' "$MB" | ||
| 106 | +check "metrics top defaults to min(25, proc_count)" '"top":' "$MB" | ||
| 107 | +printf '%s' "$MB" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ | ||
| 108 | + && pass "metrics body is valid JSON" || fail "metrics body JSON invalid" | ||
| 109 | + | ||
| 110 | +# real, non-zero assertions - these are what prove the sampler works | ||
| 111 | +MJ=/tmp/sysmon_metrics_$$.json | ||
| 112 | +printf '%s' "$MB" > "$MJ" | ||
| 113 | +python3 - "$MJ" <<'PY' && pass "live counters are non-zero and in range" || fail "live counters wrong" | ||
| 114 | +import json,sys | ||
| 115 | +d=json.load(open(sys.argv[1])) | ||
| 116 | +assert d["mem"]["total_kb"] > 0, "mem_total must be > 0" | ||
| 117 | +assert d["mem"]["used_kb"] > 0, "mem_used must be > 0" | ||
| 118 | +assert 0 <= d["mem"]["pct"] <= 100 | ||
| 119 | +assert 0 <= d["cpu_pct"] <= 100 | ||
| 120 | +assert d["ncores"] >= 1 | ||
| 121 | +assert len(d["cores"]) == d["ncores"] | ||
| 122 | +for c in d["cores"]: | ||
| 123 | + assert 0 <= c["pct"] <= 100 | ||
| 124 | +assert d["uptime_sec"] > 0, "uptime must be > 0" | ||
| 125 | +assert d["proc_count"] > 0, "proc_count must be > 0" | ||
| 126 | +assert len(d["processes"]) == d["top"] | ||
| 127 | +assert d["top"] <= d["proc_count"] | ||
| 128 | +assert d["swap"]["total_kb"] >= 0 | ||
| 129 | +PY | ||
| 130 | + | ||
| 131 | +python3 - "$MJ" <<'PY' && pass "process rows carry pid, name, cpu, rss" || fail "process rows malformed" | ||
| 132 | +import json,sys | ||
| 133 | +d=json.load(open(sys.argv[1])) | ||
| 134 | +assert len(d["processes"]) > 0 | ||
| 135 | +for p in d["processes"]: | ||
| 136 | + assert isinstance(p["pid"], int) and p["pid"] > 0 | ||
| 137 | + assert isinstance(p["name"], str) and p["name"] | ||
| 138 | + assert p["cpu"] >= 0 | ||
| 139 | + assert isinstance(p["rss_kb"], int) and p["rss_kb"] >= 0 | ||
| 140 | +PY | ||
| 141 | + | ||
| 142 | +python3 - "$MJ" <<'PY' && pass "cpu percentages are sorted descending" || fail "cpu sort wrong" | ||
| 143 | +import json,sys | ||
| 144 | +d=json.load(open(sys.argv[1])) | ||
| 145 | +vals=[p["cpu"] for p in d["processes"]] | ||
| 146 | +assert vals == sorted(vals, reverse=True), vals | ||
| 147 | +PY | ||
| 148 | +rm -f "$MJ" | ||
| 149 | + | ||
| 150 | +section "mode 2 sorting, limits and unknown actions" | ||
| 151 | +MEMF=/tmp/sysmon_mem_$$.json | ||
| 152 | +MEM=$(body "$(QUERY_STRING='action=metrics&sort=mem&top=5' "$BIN")") | ||
| 153 | +printf '%s' "$MEM" > "$MEMF" | ||
| 154 | +check "sort=mem echoed" '"sort":"mem"' "$MEM" | ||
| 155 | +check "top=5 honoured" '"top":5' "$MEM" | ||
| 156 | +python3 - "$MEMF" <<'PY' && pass "memory sort is descending" || fail "mem sort wrong" | ||
| 157 | +import json,sys | ||
| 158 | +d=json.load(open(sys.argv[1])) | ||
| 159 | +v=[p["rss_kb"] for p in d["processes"]] | ||
| 160 | +assert v == sorted(v, reverse=True), v | ||
| 161 | +assert len(v) <= 5 | ||
| 162 | +PY | ||
| 163 | +rm -f "$MEMF" | ||
| 164 | +TOP1=$(body "$(QUERY_STRING='action=metrics&top=1' "$BIN")") | ||
| 165 | +check "top=1 honoured" '"top":1' "$TOP1" | ||
| 166 | +UNK=$(QUERY_STRING='action=bogus' "$BIN") | ||
| 167 | +check "unknown action rejected" 'unknown_action' "$(body "$UNK")" | ||
| 168 | +check "unknown action lists supported" '"supported":["metrics","info"]' "$(body "$UNK")" | ||
| 169 | +check "unknown action echoes request" '"requested":"bogus"' "$(body "$UNK")" | ||
| 170 | +BIGTOP=$(body "$(QUERY_STRING='action=metrics&top=99999' "$BIN")") | ||
| 171 | +check "absurd top still returns a valid snapshot" '"ok":true' "$BIGTOP" | ||
| 172 | +check "absurd top is capped" '"top":' "$BIGTOP" | ||
| 173 | + | ||
| 174 | +section "mode 2 action=info" | ||
| 175 | +INFO=$(QUERY_STRING='action=info' "$BIN") | ||
| 176 | +IBODY=$(body "$INFO") | ||
| 177 | +check "info ok:true" '"ok":true' "$IBODY" | ||
| 178 | +check "info action info" '"action":"info"' "$IBODY" | ||
| 179 | +check "info pid present" '"pid":' "$IBODY" | ||
| 180 | +check "info utc present" '"utc":"20' "$IBODY" | ||
| 181 | +check "info zero_network declared" '"zero_network":true' "$IBODY" | ||
| 182 | +check "info sample window declared" '"samples_cpu_delta_ms":80' "$IBODY" | ||
| 183 | +check "info node_auth false when unset" '"node_auth":false' "$IBODY" | ||
| 184 | +INFOAUTH=$(body "$(BOT_PASSWORD=pw QUERY_STRING='action=info&pwd=pw' "$BIN")") | ||
| 185 | +check "info node_auth true when set" '"node_auth":true' "$INFOAUTH" | ||
| 186 | +printf '%s' "$IBODY" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ | ||
| 187 | + && pass "info body is valid JSON" || fail "info body JSON invalid" | ||
| 188 | +check "format=json works as alias" '"action":"metrics"' "$(body "$(QUERY_STRING='format=json&top=2' "$BIN")")" | ||
| 189 | + | ||
| 190 | +# ---------------------------------------------------------------- html | ||
| 191 | +section "mode 3 HTML" | ||
| 192 | +HTML=$(QUERY_STRING='' "$BIN") | ||
| 193 | +check "html content-type" 'Content-Type: text/html; charset=utf-8' "$HTML" | ||
| 194 | +check_not "html has no Content-Length" 'Content-Length' "$HTML" | ||
| 195 | +check "html has viewport meta" 'width=device-width' "$HTML" | ||
| 196 | +check "html title sysmon" '<title>sysmon</title>' "$HTML" | ||
| 197 | +check "html back-link present" 'Back to Bot' "$HTML" | ||
| 198 | +check "html theme switch hook" 'bot_theme' "$HTML" | ||
| 199 | +check "html per-core host" "id='cores'" "$HTML" | ||
| 200 | +check "html history sparkline" "id='spark'" "$HTML" | ||
| 201 | +check "html memory gauge" "memFill" "$HTML" | ||
| 202 | +check "html swap gauge" "swapFill" "$HTML" | ||
| 203 | +check "html process table host" "id='rows'" "$HTML" | ||
| 204 | +check "html process filter input" "id='q'" "$HTML" | ||
| 205 | +check "html rate segmented control" 'data-ms=' "$HTML" | ||
| 206 | +check "html sort segmented control" 'data-sort=' "$HTML" | ||
| 207 | +check "html calls the metrics action" 'action=metrics' "$HTML" | ||
| 208 | +check "html uses /plugins?plugin= route" "/plugins?plugin=" "$HTML" | ||
| 209 | +check "html falls back to /plugin_sysmon" "/plugin_sysmon" "$HTML" | ||
| 210 | +check "html closes document" '</html>' "$HTML" | ||
| 211 | +check "html declares zero network" 'zero outbound network' "$HTML" | ||
| 212 | +EMB=$(QUERY_STRING='embed=1' "$BIN") | ||
| 213 | +check_not "embed=1 strips the back-link" 'Back to Bot' "$EMB" | ||
| 214 | +check "embed=1 still renders the UI" "id='cores'" "$EMB" | ||
| 215 | +EMBT=$(QUERY_STRING='embed=true' "$BIN") | ||
| 216 | +check_not "embed=true also strips back-link" 'Back to Bot' "$EMBT" | ||
| 217 | + | ||
| 218 | +ZEROEMOJI=$(printf '%s' "$HTML" | python3 -c ' | ||
| 219 | +import sys | ||
| 220 | +s = sys.stdin.read() | ||
| 221 | +bad = [c for c in s if ord(c) > 0x2600 and ord(c) not in (0x2014, 0x2013, 0x2026)] | ||
| 222 | +print(len(bad)) | ||
| 223 | +') | ||
| 224 | +[ "$ZEROEMOJI" = "0" ] && pass "HTML contains no emoji glyphs" || fail "HTML has $ZEROEMOJI emoji glyph(s)" | ||
| 225 | +PK="pico""bot" | ||
| 226 | +check_not "html has no internal fleet paths" "$PK" "$HTML" | ||
| 227 | +check_not "html has no private IPs" "192.168." "$HTML" | ||
| 228 | +check_not "html has no ssh/sshpass" "sshpass" "$HTML" | ||
| 229 | +check_not "html has no popen of shell" "child_process" "$HTML" | ||
| 230 | + | ||
| 231 | +section "responsive markup" | ||
| 232 | +check "html has a 640px breakpoint" '@media(max-width:640px)' "$HTML" | ||
| 233 | +check "html hides columns on mobile" 'hide-sm' "$HTML" | ||
| 234 | +check "html grids use auto-fill" 'auto-fill,minmax' "$HTML" | ||
| 235 | +check "html sparkline scales" "preserveAspectRatio='none'" "$HTML" | ||
| 236 | + | ||
| 237 | +section "no shell, no sockets" | ||
| 238 | +SRCCAT=$(cat src/plugin_sysmon.c) | ||
| 239 | +check_not "linux/mac code has no popen of user input" 'popen(\"ps' "$SRCCAT" | ||
| 240 | +check_not "source opens no sockets" 'socket(' "$SRCCAT" | ||
| 241 | +check_not "source has no curl or wget" 'wget' "$SRCCAT" | ||
| 242 | +check_not "source has no ssh invocation" 'ssh ' "$SRCCAT" | ||
| 243 | +check_not "source has no sshpass" 'sshpass' "$SRCCAT" | ||
| 244 | + | ||
| 245 | +# ---------------------------------------------------------------- optional targets | ||
| 246 | +section "optional cross builds" | ||
| 247 | +if [ -f plugin_sysmon.musl ]; then | ||
| 248 | + M=$(./plugin_sysmon.musl --meta) | ||
| 249 | + check "musl build --meta" '"name":"sysmon"' "$M" | ||
| 250 | + case $(head -c 4 plugin_sysmon.musl | od -An -tx1 | tr -d ' \n') in | ||
| 251 | + 7f454c46) pass "musl artifact is ELF" ;; | ||
| 252 | + *) fail "musl artifact is not ELF" ;; | ||
| 253 | + esac | ||
| 254 | +else | ||
| 255 | + echo " skip musl (not built)" | ||
| 256 | +fi | ||
| 257 | +if [ -f plugin_sysmon.exe ]; then | ||
| 258 | + MAGIC=$(head -c 2 plugin_sysmon.exe) | ||
| 259 | + [ "$MAGIC" = "MZ" ] && pass "windows artifact has MZ header" || fail "windows artifact missing MZ" | ||
| 260 | + SZ=$(wc -c < plugin_sysmon.exe | tr -d ' ') | ||
| 261 | + [ "$SZ" -gt 20000 ] && pass "windows artifact is not an error page ($SZ bytes)" || fail "windows artifact suspiciously small ($SZ bytes)" | ||
| 262 | +else | ||
| 263 | + echo " skip windows PE (not built)" | ||
| 264 | +fi | ||
| 265 | +if [ -f plugin_sysmon.macos-arm64 ]; then | ||
| 266 | + MF=$(head -c 4 plugin_sysmon.macos-arm64 | od -An -tx1 | tr -d ' \n') | ||
| 267 | + case "$MF" in | ||
| 268 | + cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-arm64 artifact is Mach-O" ;; | ||
| 269 | + *) fail "macos-arm64 artifact is not Mach-O ($MF)" ;; | ||
| 270 | + esac | ||
| 271 | +else | ||
| 272 | + echo " skip macos-arm64 (not built)" | ||
| 273 | +fi | ||
| 274 | +if [ -f plugin_sysmon.macos-x86_64 ]; then | ||
| 275 | + MF=$(head -c 4 plugin_sysmon.macos-x86_64 | od -An -tx1 | tr -d ' \n') | ||
| 276 | + case "$MF" in | ||
| 277 | + cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-x86_64 artifact is Mach-O" ;; | ||
| 278 | + *) fail "macos-x86_64 artifact is not Mach-O ($MF)" ;; | ||
| 279 | + esac | ||
| 280 | +else | ||
| 281 | + echo " skip macos-x86_64 (not built)" | ||
| 282 | +fi | ||
| 283 | + | ||
| 284 | +# ---------------------------------------------------------------- history audit | ||
| 285 | +section "git history secret & LAN audit" | ||
| 286 | +if git rev-parse --git-dir >/dev/null 2>&1; then | ||
| 287 | + ALL=$(mktemp 2>/dev/null || echo /tmp/sysmon_hist_$$) | ||
| 288 | + # Bookkeeping lines of this audit are excluded so the detector cannot flag itself. | ||
| 289 | + git log -p --all 2>/dev/null \ | ||
| 290 | + | grep -v '^Author: ' \ | ||
| 291 | + | grep -vE 'audit "|HITS=|grep -E|scan "' \ | ||
| 292 | + > "$ALL" || : > "$ALL" | ||
| 293 | + | ||
| 294 | + audit() { # audit <label> <regex> | ||
| 295 | + H=$(grep -Eo "$2" "$ALL" 2>/dev/null | sort -u | head -3 || true) | ||
| 296 | + if [ -n "$H" ]; then | ||
| 297 | + fail "history: $1"; printf '%s\n' "$H" | sed 's/^/ /' | ||
| 298 | + else | ||
| 299 | + pass "history clean: $1" | ||
| 300 | + fi | ||
| 301 | + } | ||
| 302 | + audit "LAN 192.168.x" '192\.168\.[0-9]+\.[0-9]+' | ||
| 303 | + audit "private 10.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' | ||
| 304 | + audit "private 172.16-31.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' | ||
| 305 | + audit "GitHub PAT" 'ghp_[A-Za-z0-9]{36}' | ||
| 306 | + audit "HuggingFace token" 'hf_[A-Za-z0-9]{30,}' | ||
| 307 | + audit "embedded git creds" 'scorpioxinc:[A-Za-z0-9]{20,}' | ||
| 308 | + audit "bare host password" 'Xboxone0909|xboxone' | ||
| 309 | + audit "ssh password flag" 'sshpass -p' | ||
| 310 | + PK="pico""bot"; WT=".work""trees" | ||
| 311 | + audit "fleet path" "/codebases/${PK}|${WT}/" | ||
| 312 | + rm -f "$ALL" | ||
| 313 | +else | ||
| 314 | + echo " skip history audit (not a git checkout)" | ||
| 315 | +fi | ||
| 316 | + | ||
| 317 | +# ---------------------------------------------------------------- summary | ||
| 318 | +printf '\n%s== summary ==%s\n' "$C_B" "$C_0" | ||
| 319 | +printf ' passed: %d\n' "$PASS" | ||
| 320 | +printf ' failed: %d\n' "$FAIL" | ||
| 321 | +if [ "$FAIL" -ne 0 ]; then | ||
| 322 | + printf ' %sRESULT: FAILED%s\n' "$C_BAD" "$C_0" | ||
| 323 | + exit 1 | ||
| 324 | +fi | ||
| 325 | +printf ' %sRESULT: ALL PASSED%s\n' "$C_OK" "$C_0" | ||
| 326 | +exit 0 | ||
| 0 | 327 |