net check

This commit is contained in:
ari committed 2026-10-16 12:14:15 -04:00
1 parent faeeb885b6
commit ee00ccb0d7
5 files changed
+270 -26

No files matched your search

+28 -25
View File
@@ -21,6 +21,8 @@ bin/
├── build.nu # `skyg build image/openwrt`
├── render.nu # `skyg render html-tpl`
├── hm.nu # `skyg hm <action>`
├── net.nu # `skyg net check` (per-device NIC info + shared-server speed test)
├── net-nic.sh # Bash probe piped to each device by `skyg net check` (NIC model, driver, link/supported speeds)
├── helpers.nu # Shared helpers: REPO_ROOT const + internal functions
├── profile.nu # Profile helpers and tab-completion data
├── types.nu # Tab-completion completer functions
@@ -29,31 +31,32 @@ bin/
## Key Commands
| Command | Description |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `skyg os switch` | Rebuild and switch local system |
| `skyg os rollback` | Roll back to previous generation |
| `skyg remote switch <host>` | Deploy to a remote host |
| `skyg remote install <host>` | Fresh-install a remote host via nixos-anywhere, using `hosts/<profile>.disko.nix`. Always wipes the disks in that file. Aborts if the file is missing, if the host already boots NixOS from disk (unless `--force-wipe`), or if you do not type the short hostname. |
| `skyg remote status <host>` | Check status of a single remote host |
| `skyg remote status --all` | Check status of all remote hosts |
| `skyg remote boot-all` | Boot all remote hosts |
| `skyg remote reboot-pending` | Reboot every remote host with a pending reboot (`booted-system` != `current-system`). Lists them, asks for confirmation, then reboots one at a time, waiting for each to come back online and sending a desktop notification when it does. |
| `skyg hm switch` | Apply Home Manager config |
| `skyg flake update [input]` | Update flake inputs |
| `skyg flake check` | Run `nix flake check --no-build` |
| `skyg flake check-versions` | Verify NixOS release version consistency in flake.nix |
| `skyg build image iso\|pi1\|pi2` | Build bootable image — ISO (`iso`) or Pi SD card image (`pi1`, `pi2`) |
| `skyg build openwrt [router]` | Deploy OpenWrt config |
| `skyg profiles` | List valid local/remote profiles |
| `skyg secrets list` | List known secret names |
| `skyg secrets show <secret>` | Decrypt a secret to `.tmp/unencrypted-<secret>` |
| `skyg secrets edit <secret> [--yaml]` | Encrypt a working copy back into the secret |
| `skyg secrets compare <secret>` | Diff the working copy against the stored secret |
| `skyg ca init` | Generate the lab root CA: key → `secrets/lab-ca-key.age`, cert → `configs/pki/lab-ca.crt` (see `configs/pki/ABOUTME.md`) |
| `skyg ca issue <domain> --for <host>` | Mint a TLS leaf cert signed by the lab CA: cert → `configs/pki/<domain>.crt`, key → `secrets/<name>-tls-key.age`. Auto-registers missing `secrets.nix` entries. |
| `skyg pkgs update [--all\|<pkg>]` | Update one or all custom packages |
| `skyg render html-tpl <file>` | Render a Gitea `.tmpl` and serve it on `127.0.0.1` for preview. Foreground — Ctrl+C to stop. `<file>` tab-completes from `**/*.tmpl`. |
| Command | Description |
| ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `skyg os switch` | Rebuild and switch local system |
| `skyg os rollback` | Roll back to previous generation |
| `skyg remote switch <host>` | Deploy to a remote host |
| `skyg remote install <host>` | Fresh-install a remote host via nixos-anywhere, using `hosts/<profile>.disko.nix`. Always wipes the disks in that file. Aborts if the file is missing, if the host already boots NixOS from disk (unless `--force-wipe`), or if you do not type the short hostname. |
| `skyg remote status <host>` | Check status of a single remote host |
| `skyg remote status --all` | Check status of all remote hosts |
| `skyg remote boot-all` | Boot all remote hosts |
| `skyg remote reboot-pending` | Reboot every remote host with a pending reboot (`booted-system` != `current-system`). Lists them, asks for confirmation, then reboots one at a time, waiting for each to come back online and sending a desktop notification when it does. |
| `skyg hm switch` | Apply Home Manager config |
| `skyg flake update [input]` | Update flake inputs |
| `skyg flake check` | Run `nix flake check --no-build` |
| `skyg flake check-versions` | Verify NixOS release version consistency in flake.nix |
| `skyg build image iso\|pi1\|pi2` | Build bootable image — ISO (`iso`) or Pi SD card image (`pi1`, `pi2`) |
| `skyg build openwrt [router]` | Deploy OpenWrt config |
| `skyg profiles` | List valid local/remote profiles |
| `skyg secrets list` | List known secret names |
| `skyg secrets show <secret>` | Decrypt a secret to `.tmp/unencrypted-<secret>` |
| `skyg secrets edit <secret> [--yaml]` | Encrypt a working copy back into the secret |
| `skyg secrets compare <secret>` | Diff the working copy against the stored secret |
| `skyg ca init` | Generate the lab root CA: key → `secrets/lab-ca-key.age`, cert → `configs/pki/lab-ca.crt` (see `configs/pki/ABOUTME.md`) |
| `skyg ca issue <domain> --for <host>` | Mint a TLS leaf cert signed by the lab CA: cert → `configs/pki/<domain>.crt`, key → `secrets/<name>-tls-key.age`. Auto-registers missing `secrets.nix` entries. |
| `skyg pkgs update [--all\|<pkg>]` | Update one or all custom packages |
| `skyg net check [device]` | For every managed device (or just `[device]`): report the NIC on the default route (model, driver, current link speed, supported speeds), then run `speedtest-go` via `nix-shell` against one shared server. Logs progress per device and returns a table. `--server <id>` picks the server; `--no-speedtest` skips the test. Offline devices are listed, not tested. |
| `skyg render html-tpl <file>` | Render a Gitea `.tmpl` and serve it on `127.0.0.1` for preview. Foreground — Ctrl+C to stop. `<file>` tab-completes from `**/*.tmpl`. |
## Agent Note
+1
View File
@@ -10,6 +10,7 @@ export use flake.nu *
export use build.nu *
export use render.nu *
export use hm.nu *
export use net.nu *
export def skyg [] {
print $REPO_ROOT
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env bash
# Probe the network card a host uses for internet traffic.
# Piped to `bash -s` locally or over SSH by `skyg net check` (bin/lib/net.nu).
# Prints KEY=VALUE lines; empty values mean "unknown".
set -u
et() {
if command -v ethtool >/dev/null 2>&1; then
ethtool "$@"
else
nix-shell -p ethtool --run "ethtool $*"
fi
}
is_phys() { [ -e "/sys/class/net/$1/device" ]; }
# Interface on the default route (may be a bridge or other virtual device).
iface=$(ip -4 route show default 2>/dev/null | awk '{for (i = 1; i <= NF; i++) if ($i == "dev") { print $(i + 1); exit }}')
nic="$iface"
# Follow a bridge/virtual interface down to a physical port.
if [ -n "$nic" ] && ! is_phys "$nic"; then
for p in /sys/class/net/"$nic"/brif/* /sys/class/net/"$nic"/lower_*; do
[ -e "$p" ] || continue
c=$(basename "$p")
c=${c#lower_}
if is_phys "$c"; then nic="$c"; break; fi
done
fi
# Fallback: first physical interface that is up.
if [ -z "$nic" ] || ! is_phys "$nic"; then
nic=""
for d in /sys/class/net/*; do
n=$(basename "$d")
if is_phys "$n" && [ "$(cat "$d/operstate" 2>/dev/null)" = "up" ]; then nic="$n"; break; fi
done
fi
echo "IFACE=$iface"
echo "NIC=$nic"
[ -n "$nic" ] || exit 0
if [ -d "/sys/class/net/$nic/wireless" ] || [ -e "/sys/class/net/$nic/phy80211" ]; then
echo "TYPE=wifi"
else
echo "TYPE=ethernet"
fi
echo "DRIVER=$(basename "$(readlink -f "/sys/class/net/$nic/device/driver" 2>/dev/null)" 2>/dev/null)"
echo "LINK=$(cat "/sys/class/net/$nic/speed" 2>/dev/null)"
props=$(udevadm info -q property -p "/sys/class/net/$nic" 2>/dev/null)
echo "VENDOR=$(printf '%s\n' "$props" | sed -n 's/^ID_VENDOR_FROM_DATABASE=//p' | head -1)"
echo "MODEL=$(printf '%s\n' "$props" | sed -n 's/^ID_MODEL_FROM_DATABASE=//p' | head -1)"
# Speeds from ethtool's "Supported link modes" block, e.g. "10 100 1000 2500".
supported=$(et "$nic" 2>/dev/null \
| awk '/Supported link modes:/ { f = 1; sub(/.*Supported link modes:/, "") }
/Supported pause frame use:|Supports auto-negotiation:/ { f = 0 }
f { print }' \
| grep -oE '[0-9]+base' | grep -oE '^[0-9]+' | sort -n | uniq | tr '\n' ' ')
echo "SUPPORTED=${supported% }"
+177
View File
@@ -0,0 +1,177 @@
use profile.nu *
const NIC_SCRIPT = path self | path dirname | path join "net-nic.sh"
const SSH_OPTS = ["-o" "BatchMode=yes" "-o" "ConnectTimeout=5" "-o" "StrictHostKeyChecking=accept-new"]
def local-hostname [] {
^cat /proc/sys/kernel/hostname | str trim
}
# Devices `skyg net check` can test: every remote (hl-*) profile, plus this machine if it is a profile.
export def net-targets [] {
let local = (local-hostname)
valid_profiles
| each {|p|
if ($p | str starts-with "hl-") {
$p | str replace "hl-" ""
} else if $p == $local {
$p
} else {
null
}
}
| compact
}
def is-local [device: string] {
let local = (local-hostname)
$device == $local or $"hl-($device)" == $local
}
# Run a bash script on a device (locally or over SSH) and return the `complete` record.
def run-on [device: string, script: string] {
if (is-local $device) {
$script | ^bash -s | complete
} else {
$script | ^ssh ...$SSH_OPTS $"root@($device).lab.internal" bash -s | complete
}
}
def is-online [device: string] {
if (is-local $device) { return true }
(^ssh ...$SSH_OPTS $"root@($device).lab.internal" true | complete | get exit_code) == 0
}
def to-int-or-null [v] {
if ($v | is-empty) { return null }
try {
let n = ($v | into int)
if $n > 0 { $n } else { null }
} catch { null }
}
def bps-to-mbps [v] {
if $v == null { null } else { ($v * 8 / 1_000_000) | math round --precision 1 }
}
# Read the device's network card details via net-nic.sh (KEY=VALUE lines -> record).
def probe-nic [device: string] {
let r = (run-on $device (^cat $NIC_SCRIPT))
$r.stdout
| lines
| parse --regex '^(?P<key>[A-Z]+)=(?P<value>.*)$'
| reduce --fold {} {|it, acc| $acc | upsert $it.key ($it.value | str trim) }
}
# Closest speedtest.net server to this machine, as {id, rest}, or null.
def closest-server [] {
let r = (^bash -c "nix-shell -p speedtest-go --run 'speedtest-go --list'" | complete)
$r.stdout
| lines
| parse --regex '^\[(?P<id>\d+)\]\s+(?P<rest>.*)$'
| get 0?
}
# Run speedtest-go on a device. Assumes dl_speed/ul_speed are bytes/s and latency is ns.
def run-speedtest [device: string, server: string] {
let srv = if $server == "" { "" } else { $"--server ($server)" }
let r = (run-on $device $"timeout 600 nix-shell -p speedtest-go --run 'speedtest-go ($srv) --json'")
let json_line = ($r.stdout | lines | where {|l| $l | str starts-with "{" } | reverse | get 0?)
let s = if $json_line == null { null } else {
try { $json_line | from json | get servers.0 } catch { null }
}
if $s == null {
let err = ($r.stderr | lines | where {|l| $l != "" } | last 1 | str join)
return {ok: false, error: (if $err == "" { $"exit code ($r.exit_code)" } else { $err })}
}
{
ok: true
server: $"($s.id? | default '?') ($s.name? | default '') / ($s.sponsor? | default '')"
ping_ms: (if $s.latency? == null { null } else { ($s.latency / 1_000_000) | math round --precision 1 })
down_mbps: (bps-to-mbps $s.dl_speed?)
up_mbps: (bps-to-mbps $s.ul_speed?)
}
}
def empty-row [device: string, status: string] {
{
host: $device, status: $status, iface: "", nic: "", type: "", model: "", driver: ""
link_mbps: null, supported_mbps: "", max_mbps: null
server: null, ping_ms: null, down_mbps: null, up_mbps: null
}
}
# Check each managed device's network card and run a speed test against one shared server.
export def "skyg net check" [
device?: string@net-targets, # Only check this device (default: all devices)
--server: string = "", # speedtest.net server id (default: closest to this machine)
--no-speedtest, # Only report network card details
] {
let all = (net-targets)
let devices = if $device == null { $all } else {
if not ($device in $all) {
error make {msg: $"Unknown device '($device)'. Valid devices: ($all | str join ', ')"}
}
[$device]
}
mut server_id = $server
if (not $no_speedtest) and $server_id == "" {
print "Finding the closest speedtest server (shared by all devices)..."
let s = (closest-server)
if $s == null {
print $"(ansi yellow)Could not list servers; each device will pick its own.(ansi reset)"
} else {
$server_id = $s.id
print $"Using server ($s.id): ($s.rest)"
}
}
let total = ($devices | length)
mut rows = []
for it in ($devices | enumerate) {
let d = $it.item
let tag = $"[($it.index + 1)/($total)] ($d)"
print $"($tag): checking reachability..."
if not (is-online $d) {
print $"($tag): (ansi yellow)OFFLINE(ansi reset)"
$rows = ($rows | append (empty-row $d "offline"))
continue
}
print $"($tag): reading network card..."
let nic = (probe-nic $d)
let supported = ($nic.SUPPORTED? | default "" | split row " " | where {|x| $x != "" })
let max = if ($supported | is-empty) { null } else { $supported | each {|x| $x | into int } | math max }
let model = ([($nic.VENDOR? | default "") ($nic.MODEL? | default "")] | where {|x| $x != "" } | str join " ")
mut row = (empty-row $d "online" | merge {
iface: ($nic.IFACE? | default "")
nic: ($nic.NIC? | default "")
type: ($nic.TYPE? | default "")
model: $model
driver: ($nic.DRIVER? | default "")
link_mbps: (to-int-or-null ($nic.LINK? | default ""))
supported_mbps: ($supported | str join " ")
max_mbps: $max
})
print $"($tag): ($row.nic) \(($row.type)\) ($model) | link ($row.link_mbps | default '?') Mbps | supports up to ($max | default '?') Mbps"
if not $no_speedtest {
print $"($tag): running speedtest-go \(first run may download it via nix-shell\)..."
let st = (run-speedtest $d $server_id)
if $st.ok {
$row = ($row | merge ($st | reject ok))
print $"($tag): (ansi green)down ($st.down_mbps) Mbps, up ($st.up_mbps) Mbps, ping ($st.ping_ms) ms(ansi reset)"
} else {
$row = ($row | upsert status "speedtest failed")
print $"($tag): (ansi red)speedtest failed: ($st.error)(ansi reset)"
}
}
$rows = ($rows | append $row)
}
print $"Done: checked ($total) device\(s\)."
$rows
}
+1 -1
View File
@@ -34,7 +34,7 @@ in
environmentFiles = [ config.age.secrets.immich-shared.path ];
environment = envVars;
deploy.resources.limits = {
cpus = "2";
cpus = "4";
memory = "8G";
};
networks = {