diff --git a/crates/xframe-demo/examples/monitor_service_dashboard/README.md b/crates/xframe-demo/examples/monitor_service_dashboard/README.md new file mode 100644 index 0000000..800136e --- /dev/null +++ b/crates/xframe-demo/examples/monitor_service_dashboard/README.md @@ -0,0 +1,52 @@ +# Service Dashboard — monitor app (Rust) + +A service/port dashboard in one window (1024×768): a single +`MonitorWidget` (`XMonitorGrid`) of illuminator buttons for the +services that actually run on this host (hot_dallas). Each button +polls its live check every 2 seconds and glows green when up, red +when down, dim when unknown. Click a button to re-check it now. + +## What it monitors + +| Button | Kind | Check | +| --- | --- | --- | +| SSH :22 | Port | TCP connect to localhost:22 | +| HTTP :80 | Port | TCP connect to localhost:80 (nginx) | +| HTTPS :443 | Port | TCP connect to localhost:443 (nginx) | +| NPM admin :81 | Port | TCP connect to localhost:81 | +| ThinkMelt :2368 | Port | TCP connect to localhost:2368 | +| Ghost :5000 | Port | TCP connect to localhost:5000 | +| lightgui.com :5009 | Port | TCP connect to localhost:5009 | +| Traffic stats :7888 | Port | TCP connect to localhost:7888 (GoAccess) | +| dockerd | Process | `/proc//comm` == `dockerd` | +| containerd | Process | `/proc//comm` == `containerd` | +| docker.service | Unit | `systemctl is-active docker.service` == `active` | +| ssh.service | Unit | `systemctl is-active ssh.service` == `active` | + +No mock or static data: every cell is a live `MonitorButtonState` +check (`poll_all()` on a 2 s timer). + +## Widgets used + +- `XMonitorGrid` (`MonitorWidget`) — the panel; one shared + `poll_all()` timer. +- `XMonitorButton` (`MonitorButtonState`) — one cell per service + (kinds `TcpPort` / `Process` / `Unit`). + +## Build and run + +```sh +# from the lightgui_rust checkout +cargo run -p xframe-demo --example monitor_service_dashboard +``` + +Close the window (or press Esc / `q`) to quit. + +## Source + +- Rust: `crates/xframe-demo/examples/monitor_service_dashboard/main.rs` + in [lightgui_rust](https://github.com/cnmcdee/lightgui_rust) +- C++: `examples/monitor/service-dashboard/main.cpp` in + [lightgui](https://github.com/cnmcdee/lightgui) +- Python: `examples/monitor_service_dashboard/main.py` in + [lightgui_python](https://github.com/cnmcdee/lightgui_python) diff --git a/crates/xframe-demo/examples/monitor_service_dashboard/main.rs b/crates/xframe-demo/examples/monitor_service_dashboard/main.rs new file mode 100644 index 0000000..0c74efa --- /dev/null +++ b/crates/xframe-demo/examples/monitor_service_dashboard/main.rs @@ -0,0 +1,171 @@ +//! Service Dashboard — monitor app. +//! +//! One 1024x768 window holding a single `XMonitorGrid` of illuminator +//! buttons for the services that actually run on this host +//! (hot_dallas): TCP ports (sshd, nginx, NPM, ThinkMelt, Ghost blogs, +//! traffic stats), processes (dockerd, containerd) and systemd units +//! (docker.service, ssh.service). Every cell is a live +//! `MonitorButtonState` check, re-polled on a shared 2 s timer; green +//! when up, red when down. Click a cell to re-check it now. +//! +//! Run from the workspace root: +//! cargo run -p xframe-demo --example monitor_service_dashboard +//! Close the window (or press Esc / `q`) to quit. + +use std::process::ExitCode; +use std::time::{Duration, Instant}; + +use x11rb::connection::Connection; +use x11rb::protocol::xproto::*; +use x11rb::protocol::Event; +use xframe_common::style::FONT_DEFAULT; +use xframe_common::target::CtxTarget; +use xframe_common::x11::{DrawCtx, X11}; +use xframe_core::StyleManager; +use xframe_widgets::{Key, MonitorButtonState, MonitorTarget, MonitorWidget, Widget}; + +const WIN_W: u16 = 1024; +const WIN_H: u16 = 768; +const KEY_ESCAPE: u32 = 0xff1b; + +/// (label, target) for every real service on this host. +fn services() -> Vec<(&'static str, MonitorTarget)> { + vec![ + ("SSH :22", MonitorTarget::TcpPort(22)), + ("HTTP :80", MonitorTarget::TcpPort(80)), + ("HTTPS :443", MonitorTarget::TcpPort(443)), + ("NPM admin :81", MonitorTarget::TcpPort(81)), + ("ThinkMelt :2368", MonitorTarget::TcpPort(2368)), + ("Ghost :5000", MonitorTarget::TcpPort(5000)), + ("lightgui.com :5009", MonitorTarget::TcpPort(5009)), + ("Traffic stats :7888", MonitorTarget::TcpPort(7888)), + ("dockerd", MonitorTarget::Process("dockerd".to_string())), + ("containerd", MonitorTarget::Process("containerd".to_string())), + ("docker.service", MonitorTarget::Unit("docker.service".to_string())), + ("ssh.service", MonitorTarget::Unit("ssh.service".to_string())), + ] +} + +fn main() -> ExitCode { + match run() { + Ok(code) => ExitCode::from(code as u8), + Err(e) => { + eprintln!("monitor_service_dashboard: {}", e); + ExitCode::from(1) + } + } +} + +fn run() -> Result> { + let x = X11::connect()?; + let font = x.open_font(&[FONT_DEFAULT, "fixed"])?; + let event_mask = EventMask::EXPOSURE + | EventMask::KEY_PRESS + | EventMask::STRUCTURE_NOTIFY + | EventMask::BUTTON_PRESS + | EventMask::BUTTON_RELEASE + | EventMask::POINTER_MOTION; + let (win, gc) = x.create_window( + 100, + 100, + WIN_W, + WIN_H, + "Service Dashboard - monitor", + event_mask, + )?; + + let mut styles = StyleManager::new(); + xframe_demo::register_styles(&mut styles); + + // The grid constructor seeds demo monitors (incl. docker :2375, + // which is not a listening port here); replace them with the + // services that actually run on this host, then poll live. + let mut grid = MonitorWidget::new("Service Dashboard", "MonitorGrid", 16, 16, 992, 736); + let services = services(); + grid.buttons = services + .iter() + .map(|(label, target)| MonitorButtonState::new(label, target.clone())) + .collect(); + grid.poll_all(); + + let ctx = DrawCtx::new(&x, win, gc, font); + ctx.bind_font(); + let mut target = CtxTarget::new(&ctx); + if !styles.resolve_font("default", &mut target) + && !styles.resolve_font("dark", &mut target) + { + eprintln!("monitor_service_dashboard: warning: no font resolved, labels will be blank"); + } + + let redraw = |grid: &MonitorWidget, + ctx: &DrawCtx, + target: &mut CtxTarget, + styles: &mut StyleManager| { + ctx.clear(255, 255, 255, WIN_W, WIN_H); + grid.draw(target, styles); + ctx.flush(); + }; + redraw(&grid, &ctx, &mut target, &mut styles); + + let mut last_poll = Instant::now(); + loop { + // 2 s shared poll timer, checked on every loop pass. + if last_poll.elapsed() >= Duration::from_secs(2) { + last_poll = Instant::now(); + grid.poll_all(); + redraw(&grid, &ctx, &mut target, &mut styles); + } + let ev = match x.conn.poll_for_event()? { + Some(ev) => ev, + None => { + std::thread::sleep(Duration::from_millis(10)); + continue; + } + }; + let mut handled = false; + match ev { + Event::Expose(_) => { + redraw(&grid, &ctx, &mut target, &mut styles); + } + Event::ButtonPress(ev) => { + handled = grid.on_button_press( + ev.event_x as i32, + ev.event_y as i32, + ev.detail as u32, + ); + } + Event::ButtonRelease(ev) => { + handled = grid.on_button_release( + ev.event_x as i32, + ev.event_y as i32, + ev.detail as u32, + ); + } + Event::MotionNotify(ev) => { + handled = grid.on_motion(ev.event_x as i32, ev.event_y as i32); + } + Event::KeyPress(ev) => { + let keysym = x.keysym_for_keycode(ev.detail); + if keysym == KEY_ESCAPE || keysym == b'q' as u32 { + break; + } + let text = if (0x20..0x7f).contains(&keysym) { + Some(keysym as u8 as char) + } else { + None + }; + handled = grid.on_key(Key::from_keysym(keysym, text), text); + } + Event::ClientMessage(ev) + if ev.type_ == x.wm_protocols && ev.data.as_data32()[0] == x.wm_delete => + { + break; + } + _ => {} + } + if handled { + redraw(&grid, &ctx, &mut target, &mut styles); + } + } + Ok(0) +} diff --git a/crates/xframe-demo/examples/monitor_service_dashboard/screenshot.png b/crates/xframe-demo/examples/monitor_service_dashboard/screenshot.png new file mode 100644 index 0000000..461346a Binary files /dev/null and b/crates/xframe-demo/examples/monitor_service_dashboard/screenshot.png differ