Skip to content

sems-stats: port to safe Rust, add -q switch#439

Open
hecko wants to merge 2 commits into
masterfrom
claude/sems-stats-rust
Open

sems-stats: port to safe Rust, add -q switch#439
hecko wants to merge 2 commits into
masterfrom
claude/sems-stats-rust

Conversation

@hecko
Copy link
Copy Markdown
Contributor

@hecko hecko commented Apr 21, 2026

Summary

  • Re-implement sems-stats in safe Rust (#![forbid(unsafe_code)]), std-only, no external crates. Lives in the existing Cargo workspace at apps/monitoring/tools/sems-stats/ alongside the other Rust monitoring tools.
  • Feature parity with the C++ client: same flags (-s -p -c -t -h), same defaults (127.0.0.1:5040, calls, 5s), same human-readable framing (sending '...\n' to host:port / received:\n<reply>), same exit-code semantics.
  • Add -q / --quiet switch that prints only the server reply with a single trailing newline — useful for shell captures ($(sems-stats -q -c calls)) and Zabbix UserParameters.
  • The C++ sources (core/plug-in/stats/query_stats.cxx) are untouched. The C++ target still builds as a reference / fallback; the install step only runs when the Rust toolchain is unavailable, so there is no binary collision at $SEMS_EXEC_PREFIX/sbin/sems-stats.

Build integration

  • Added sems-stats to apps/monitoring/tools/Cargo.toml workspace members.
  • Added it to SEMS_MONITORING_BINARIES in apps/monitoring/CMakeLists.txt so the existing cargo build / install plumbing picks it up.
  • Exposed Rust-toolchain detection as a CACHE INTERNAL (SEMS_HAS_RUST) so core/plug-in/stats/CMakeLists.txt can skip its install() when the Rust port is being shipped.
  • Registered cargo test -p sems-stats with CTest via add_test, so ctest -V exercises the crate.

Tests

  • 12 unit tests in src/lib.rs covering parse_args (defaults, every flag combination, every error variant), resolve_server, trim_reply (including the trailing-NUL semantic matching the C++ server's wire format), and the usage text.
  • 3 integration tests in tests/udp_roundtrip.rs that spin up a local UDP responder on 127.0.0.1:0 and exercise:
    • happy-path request/reply,
    • a command with a space (set_loglevel 1) to confirm argv isn't tokenised,
    • the receive-timeout path (200 ms budget).
  • cargo test -p sems-stats --release15 passed, 0 failed.

Test plan

  • cmake -S . -B build && cmake --build build succeeds on a host with a recent Rust toolchain and produces apps/monitoring/tools/target/release/sems-stats.
  • ctest -V in the build directory runs the cargo tests and they pass.
  • On a host without cargo/rustc, the C++ sems-stats is still installed (existing behaviour preserved).
  • sems-stats -c calls prints the same sending ... / received: ... framing as the C++ version against a live SEMS instance.
  • sems-stats -q -c calls prints a single line Active calls: N.
  • RPM build (Dockerfile-rhel{8,9,10}) still lands a sems-stats in the package at the same path.

Refs #438 (indirectly — this is the Zabbix-UserParameter-friendly output path that issue will rely on).

https://claude.ai/code/session_01BDutRs8JU8stspu6kpCQpd

claude added 2 commits April 21, 2026 17:42
The Rust port lives alongside the other Rust monitoring tools in the
existing Cargo workspace. It is feature-parity with the C++ client
(same flags, same exit codes, same default output) plus a new `-q`
/ `--quiet` switch that prints only the server reply - useful for
shell captures and Zabbix UserParameters.

The C++ sources (core/plug-in/stats/query_stats.cxx) are untouched
and still build; the install step only triggers when the Rust
toolchain is unavailable, so there is no binary collision.

Tests: 12 lib-level unit tests plus 3 integration tests that spin
up a local UDP responder to exercise the request/reply roundtrip
and the timeout path. `cargo test -p sems-stats` is wired into
ctest via add_test.

https://claude.ai/code/session_01BDutRs8JU8stspu6kpCQpd
sems-stats is an admin CLI for the stats/control UDP plug-in, not a
monitoring query. It has no dependency on sems-monitoring-lib, the
monitoring app, or xmlrpc2di - unlike every other Rust crate in
apps/monitoring/tools/. Its natural home is /tools/, next to the
sems-rtp-mux-* shell wrappers that call it.

Cargo workspace is now at the repo root so /tools/sems-stats and
apps/monitoring/tools/* share one target/ directory and one
dependency graph. Rust toolchain detection is hoisted to the root
CMakeLists.txt (SEMS_RUST_OK as a plain variable), which lets
core/plug-in/stats/CMakeLists.txt gate its C++ install directly
without the CACHE INTERNAL workaround.

No behaviour change: the same binary ships to the same path; `cargo
test -p sems-stats` still runs via ctest; all 15 tests pass. On a
host without cargo the C++ sems-stats is still installed as the
fallback.

https://claude.ai/code/session_01BDutRs8JU8stspu6kpCQpd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants