stackpatrol 0.2.5

Single-binary Rust CLI that monitors a server and reports to the StackPatrol control plane.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};
use std::time::Instant;

use anyhow::{Context, Result, bail};
use stackpatrol_core::event::Event;
use stackpatrol_core::token;
use sysinfo::{Disks, System};

use crate::buffer::{self, Buffer};
use crate::client::{build_client, events_url, send_event};
use crate::config::{Config, config_path};
use crate::style;

pub fn init() -> Result<()> {
    println!("{}", style::header("StackPatrol setup"));
    println!();
    println!(
        "  {} Open @StackPatrolBot on Telegram and run /start",
        style::dim("1.")
    );
    println!(
        "  {} Tap \"Add server\" and give it a name",
        style::dim("2.")
    );
    println!("  {} Paste the token it gives you below", style::dim("3."));
    println!();

    let server_name = prompt("Server name (e.g. web-01)")?;
    if server_name.is_empty() {
        bail!("server name cannot be empty");
    }

    let raw_token = prompt("Server token (sp_live_…)")?;
    token::validate(&raw_token).context("invalid token format")?;

    let api_endpoint = prompt_default("API endpoint", "https://api.stackpatrol.dev")?;

    let cfg = Config {
        server_name,
        token: raw_token,
        api_endpoint,
        daemon: Default::default(),
        docker: Default::default(),
        systemd: Default::default(),
        resources: Default::default(),
        ports: Default::default(),
    };
    let path = cfg.save()?;

    println!();
    println!(
        "{} wrote {}",
        style::ok_mark(),
        style::cyan(&path.display().to_string())
    );

    let daemon_running = try_autostart_systemd();

    println!();
    println!("{}", style::bold("Next steps:"));
    println!(
        "  {}  {}             {}",
        style::dim("1."),
        style::cyan("stackpatrol alert-test"),
        style::dim("# verify token + Telegram delivery")
    );
    println!(
        "  {}  {}      {}",
        style::dim("2."),
        style::cyan("stackpatrol probe add …"),
        style::dim("# tell it what to watch (try `stackpatrol probe --help`)")
    );
    if !daemon_running {
        println!(
            "  {}  {}             {}",
            style::dim("3."),
            style::cyan("stackpatrol daemon"),
            style::dim("# or install the systemd unit (see packaging/)")
        );
    }
    Ok(())
}

/// If the systemd unit installed by `install.sh` is present, enable + restart
/// it so the daemon starts running with the freshly written config. Returns
/// `true` if the daemon is now running. Honors `STACKPATROL_NO_AUTOSTART=1`
/// for users who run their own supervisor.
fn try_autostart_systemd() -> bool {
    if std::env::var("STACKPATROL_NO_AUTOSTART").is_ok() {
        return false;
    }
    let unit = std::path::Path::new("/etc/systemd/system/stackpatrol.service");
    if !unit.exists() {
        return false;
    }

    println!();
    println!(
        "{} systemd unit detected — starting daemon...",
        style::dim(""),
    );

    // `enable` is idempotent. `restart` forces a config reload, so re-running
    // `init` after rotating a token Just Works.
    let enable = std::process::Command::new("systemctl")
        .args(["enable", "stackpatrol.service"])
        .output();
    let restart = std::process::Command::new("systemctl")
        .args(["restart", "stackpatrol.service"])
        .output();

    match (enable, restart) {
        (Ok(e), Ok(r)) if e.status.success() && r.status.success() => {
            println!(
                "  {} {} the dashboard should mark this server online within ~30s",
                style::ok_mark(),
                style::dim("daemon running —"),
            );
            println!(
                "  {} tail logs: {}",
                style::dim(""),
                style::cyan("journalctl -u stackpatrol -f"),
            );
            true
        }
        (e, r) => {
            println!(
                "  {} could not start daemon automatically (likely needs sudo)",
                style::warn_mark(),
            );
            for stream in [e, r].iter().filter_map(|x| x.as_ref().ok()) {
                let msg = String::from_utf8_lossy(&stream.stderr);
                let trimmed = msg.trim();
                if !trimmed.is_empty() {
                    eprintln!("  {}", style::dim(trimmed));
                }
            }
            println!(
                "  {} start it manually: {}",
                style::dim(""),
                style::cyan("sudo systemctl enable --now stackpatrol"),
            );
            false
        }
    }
}

pub async fn status() -> Result<()> {
    let cfg = Config::load().context("could not load config — run `stackpatrol init` first")?;

    println!(
        "{} — server: {}, endpoint: {}",
        style::header("StackPatrol"),
        style::bold(&cfg.server_name),
        style::dim(&cfg.api_endpoint),
    );
    println!();

    // System
    let mut sys = System::new();
    sys.refresh_memory();
    let disks = Disks::new_with_refreshed_list();
    let load = System::load_average();

    println!("{}", style::header("System"));
    println!("  uptime           {}", format_duration(System::uptime()));
    println!(
        "  load (1m/5m/15m) {:.2} / {:.2} / {:.2}",
        load.one, load.five, load.fifteen
    );

    let mem_total = sys.total_memory();
    let mem_used = sys.used_memory();
    let mem_pct = if mem_total > 0 {
        (mem_used as f64 / mem_total as f64 * 100.0).round() as u32
    } else {
        0
    };
    let mem_pct_str = format!("{mem_pct}%");
    let mem_pct_styled = colorize_percent(&mem_pct_str, mem_pct, cfg.resources.memory_high_percent);
    println!(
        "  memory           {} / {} ({})",
        format_bytes(mem_used),
        format_bytes(mem_total),
        mem_pct_styled,
    );

    println!("  disks");
    for disk in &disks {
        let total = disk.total_space();
        if total == 0 {
            continue;
        }
        let used = total.saturating_sub(disk.available_space());
        let pct = (used as f64 / total as f64 * 100.0).round() as u32;
        let pct_str = format!("{pct:>3}%");
        let pct_styled = colorize_percent(&pct_str, pct, cfg.resources.disk_high_percent);
        println!(
            "    {:<30} {} ({} / {})",
            disk.mount_point().display(),
            pct_styled,
            format_bytes(used),
            format_bytes(total)
        );
    }

    // Probes
    println!();
    println!("{}", style::header("Probes"));
    println!(
        "  docker     {}",
        if cfg.docker.projects.is_empty() {
            "off".to_string()
        } else {
            format!("{} project(s)", cfg.docker.projects.len())
        }
    );
    println!(
        "  systemd    {}",
        if cfg.systemd.units.is_empty() {
            "off".to_string()
        } else {
            format!("{} unit(s)", cfg.systemd.units.len())
        }
    );
    println!(
        "  ports      {}",
        if cfg.ports.tcp.is_empty() {
            "off".to_string()
        } else {
            format!("{} TCP target(s)", cfg.ports.tcp.len())
        }
    );
    let load_threshold = if cfg.resources.load_1m_high > 0.0 {
        format!("{:.2}", cfg.resources.load_1m_high)
    } else {
        "auto".to_string()
    };
    let load_warn = if cfg.resources.load_1m_warning > 0.0 {
        format!("{:.2}", cfg.resources.load_1m_warning)
    } else {
        "auto".to_string()
    };
    let disk_warn = effective_warning_percent(
        cfg.resources.disk_warning_percent,
        cfg.resources.disk_high_percent,
    );
    let mem_warn = effective_warning_percent(
        cfg.resources.memory_warning_percent,
        cfg.resources.memory_high_percent,
    );
    println!(
        "  resources  disk warn≥{}% / crit≥{}%  mem warn≥{}% / crit≥{}%  load1 warn≥{} / crit≥{}",
        disk_warn,
        cfg.resources.disk_high_percent,
        mem_warn,
        cfg.resources.memory_high_percent,
        load_warn,
        load_threshold,
    );

    // Buffer
    println!();
    println!("{}", style::header("Buffer"));
    let buffer = Buffer::new(buffer::default_path()?);
    let queued = buffer.read_all().await.unwrap_or_default();
    println!(
        "  queue      {} event(s) at {}",
        queued.len(),
        buffer.path().display()
    );

    // API reachability — probe the events URL we'll actually POST to (not the
    // root). A connection error is ✗. A 2xx/3xx, or auth/method-related 4xx
    // (401/403/405), means the route is mounted → ✓. A 404 or 5xx means the
    // server answered but the route is broken or undeployed → ⚠ with the code
    // shown so the user can tell something is off.
    println!();
    println!("{}", style::header("API"));
    let url = events_url(&cfg);
    let client = build_client()?;
    let started = Instant::now();
    match client.get(&url).send().await {
        Ok(resp) => {
            let code = resp.status();
            let elapsed = started.elapsed().as_millis();
            let mark = if code.is_success()
                || code.is_redirection()
                || matches!(code.as_u16(), 401 | 403 | 405)
            {
                style::ok_mark()
            } else {
                style::warn_mark()
            };
            println!("  reachable  {mark} HTTP {code} ({elapsed}ms)");
        }
        Err(e) => println!("  reachable  {} {e}", style::err_mark()),
    }
    println!("  endpoint   {}", style::dim(&url));

    if cfg.docker.projects.is_empty() && cfg.systemd.units.is_empty() && cfg.ports.tcp.is_empty() {
        println!();
        println!(
            "{} no probes configured. Add some with: {}",
            style::warn_mark(),
            style::cyan("stackpatrol probe add --help"),
        );
    }

    Ok(())
}

/// Color a percentage red ≥ critical, yellow within 10pp of critical, green below.
fn colorize_percent(label: &str, pct: u32, critical: u8) -> String {
    let crit = critical as u32;
    if pct >= crit {
        style::red(label)
    } else if pct + 10 >= crit {
        style::yellow(label)
    } else {
        style::green(label)
    }
}

pub async fn alert_test() -> Result<()> {
    let cfg = Config::load().context("could not load config — run `stackpatrol init` first")?;

    let url = events_url(&cfg);
    let client = build_client()?;

    println!("→ sending service_down(\"alert-test\") to {url}");
    send_event(
        &client,
        &url,
        &cfg,
        Event::ServiceDown {
            name: "alert-test".into(),
        },
    )
    .await?;
    println!("  ✔ delivered. Check Telegram — you should see a 🔴 alert in a few seconds.");

    // Auto-recover so the demo doesn't leave a fake outage on the dashboard.
    tokio::time::sleep(std::time::Duration::from_secs(3)).await;

    println!("→ sending service_up(\"alert-test\") to clear the demo alert");
    send_event(
        &client,
        &url,
        &cfg,
        Event::ServiceUp {
            name: "alert-test".into(),
        },
    )
    .await?;
    println!("  ✔ delivered.");

    Ok(())
}

// --- probe management -------------------------------------------------------

const COMPOSE_FILES: &[&str] = &[
    "compose.yaml",
    "compose.yml",
    "docker-compose.yaml",
    "docker-compose.yml",
];

pub fn probe_ls() -> Result<()> {
    let cfg = Config::load().context("could not load config — run `stackpatrol init` first")?;
    let path = config_path()?;

    println!(
        "{} — server: {}",
        style::header("Configured probes"),
        style::bold(&cfg.server_name)
    );
    println!();

    let docker: Vec<String> = cfg
        .docker
        .projects
        .iter()
        .map(|p| p.display().to_string())
        .collect();
    print_probe_section(
        "Docker projects",
        &docker,
        "stackpatrol probe add docker <path>",
    );

    print_probe_section(
        "Systemd units",
        &cfg.systemd.units,
        "stackpatrol probe add systemd <unit>",
    );

    print_probe_section(
        "TCP ports",
        &cfg.ports.tcp,
        "stackpatrol probe add port <host:port>",
    );

    let r = &cfg.resources;
    let load_crit = if r.load_1m_high > 0.0 {
        format!("{:.2}", r.load_1m_high)
    } else {
        "auto".into()
    };
    let load_warn = if r.load_1m_warning > 0.0 {
        format!("{:.2}", r.load_1m_warning)
    } else {
        "auto".into()
    };
    let disk_warn = effective_warning_percent(r.disk_warning_percent, r.disk_high_percent);
    let mem_warn = effective_warning_percent(r.memory_warning_percent, r.memory_high_percent);

    println!("{}", style::bold("Resources (always on)"));
    println!(
        "  disk    warn ≥{}%  / crit ≥{}%",
        disk_warn, r.disk_high_percent
    );
    println!(
        "  memory  warn ≥{}%  / crit ≥{}%",
        mem_warn, r.memory_high_percent
    );
    println!("  load1m  warn {} / crit {}", load_warn, load_crit);
    println!(
        "  {} thresholds are edited by hand for now: {}",
        style::dim(""),
        style::dim(&path.display().to_string()),
    );
    println!();
    println!("config: {}", style::dim(&path.display().to_string()));

    Ok(())
}

fn print_probe_section(title: &str, items: &[String], add_hint: &str) {
    if items.is_empty() {
        println!("{}{}", style::bold(title), style::dim("off"));
        println!("  add one with: {}", style::cyan(add_hint));
    } else {
        println!(
            "{} ({})",
            style::bold(title),
            style::dim(&format!("{} entries", items.len())),
        );
        for item in items {
            println!("  {} {}", style::ok_mark(), item);
        }
    }
    println!();
}

pub fn probe_add_docker(path: PathBuf) -> Result<()> {
    let canonical = path
        .canonicalize()
        .with_context(|| format!("path does not exist: {}", path.display()))?;
    if !canonical.is_dir() {
        bail!("not a directory: {}", canonical.display());
    }
    if !COMPOSE_FILES.iter().any(|f| canonical.join(f).is_file()) {
        bail!(
            "no compose file found in {} (looked for: {})",
            canonical.display(),
            COMPOSE_FILES.join(", ")
        );
    }

    let mut cfg = load_config_for_edit()?;
    if cfg.docker.projects.iter().any(|p| same_path(p, &canonical)) {
        println!(
            "{} already watching {}",
            style::warn_mark(),
            style::cyan(&canonical.display().to_string())
        );
        return Ok(());
    }
    cfg.docker.projects.push(canonical.clone());
    let path = cfg.save()?;

    print_added(
        &format!("docker project {}", canonical.display()),
        &path.display().to_string(),
    );
    Ok(())
}

pub fn probe_add_systemd(unit: String) -> Result<()> {
    let unit = normalize_systemd_unit(&unit)?;

    let mut cfg = load_config_for_edit()?;
    if cfg.systemd.units.iter().any(|u| u == &unit) {
        println!(
            "{} already watching {}",
            style::warn_mark(),
            style::cyan(&unit)
        );
        return Ok(());
    }
    cfg.systemd.units.push(unit.clone());
    let path = cfg.save()?;

    print_added(&format!("systemd unit {unit}"), &path.display().to_string());
    Ok(())
}

pub fn probe_add_port(target: String) -> Result<()> {
    let target = validate_port_target(&target)?;

    let mut cfg = load_config_for_edit()?;
    if cfg.ports.tcp.iter().any(|t| t == &target) {
        println!(
            "{} already watching {}",
            style::warn_mark(),
            style::cyan(&target)
        );
        return Ok(());
    }
    cfg.ports.tcp.push(target.clone());
    let path = cfg.save()?;

    print_added(&format!("TCP target {target}"), &path.display().to_string());
    Ok(())
}

pub fn probe_rm_docker(path: PathBuf) -> Result<()> {
    // Don't require the path to still exist on disk — user may be cleaning
    // up an entry whose project was already deleted. Match by canonical
    // form when possible, but fall back to literal string match.
    let canonical = path.canonicalize().ok();
    let mut cfg = load_config_for_edit()?;
    let before = cfg.docker.projects.len();
    cfg.docker.projects.retain(|p| {
        if let Some(c) = &canonical {
            !same_path(p, c)
        } else {
            p != &path
        }
    });
    if cfg.docker.projects.len() == before {
        bail!("not currently watching {}", path.display());
    }
    let saved = cfg.save()?;
    print_removed(
        &format!("docker project {}", path.display()),
        &saved.display().to_string(),
    );
    Ok(())
}

pub fn probe_rm_systemd(unit: String) -> Result<()> {
    let unit = normalize_systemd_unit(&unit)?;
    let mut cfg = load_config_for_edit()?;
    let before = cfg.systemd.units.len();
    cfg.systemd.units.retain(|u| u != &unit);
    if cfg.systemd.units.len() == before {
        bail!("not currently watching {unit}");
    }
    let saved = cfg.save()?;
    print_removed(
        &format!("systemd unit {unit}"),
        &saved.display().to_string(),
    );
    Ok(())
}

pub fn probe_rm_port(target: String) -> Result<()> {
    let mut cfg = load_config_for_edit()?;
    let before = cfg.ports.tcp.len();
    cfg.ports.tcp.retain(|t| t != &target);
    if cfg.ports.tcp.len() == before {
        bail!("not currently watching {target}");
    }
    let saved = cfg.save()?;
    print_removed(
        &format!("TCP target {target}"),
        &saved.display().to_string(),
    );
    Ok(())
}

fn load_config_for_edit() -> Result<Config> {
    Config::load().context("could not load config — run `stackpatrol init` first")
}

fn print_added(what: &str, config_path: &str) {
    println!("{} now watching {}", style::ok_mark(), style::cyan(what));
    println!("  config: {}", style::dim(config_path));
    println!(
        "  {} {}",
        style::dim(""),
        style::dim(&daemon_restart_hint()),
    );
}

fn print_removed(what: &str, config_path: &str) {
    println!(
        "{} no longer watching {}",
        style::ok_mark(),
        style::cyan(what)
    );
    println!("  config: {}", style::dim(config_path));
    println!(
        "  {} {}",
        style::dim(""),
        style::dim(&daemon_restart_hint()),
    );
}

/// Build a hint about how to make a config change take effect, tailored to
/// what's actually installed on the host. If a `stackpatrol.service` systemd
/// unit is present we suggest the exact restart command; otherwise (no
/// systemd, unit not installed, etc.) we keep it generic so we don't tell
/// users to run a command that will fail.
fn daemon_restart_hint() -> String {
    let unit_installed = std::process::Command::new("systemctl")
        .args(["cat", "stackpatrol.service"])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false);

    if unit_installed {
        "restart the daemon to pick up the change: sudo systemctl restart stackpatrol".to_string()
    } else {
        "restart the running daemon to pick up the change (or start one with `stackpatrol daemon` if it isn't running yet)".to_string()
    }
}

/// Append `.service` if no suffix was supplied — matches systemd's own behavior
/// and saves the user from having to remember the suffix for the common case.
fn normalize_systemd_unit(raw: &str) -> Result<String> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        bail!("unit name cannot be empty");
    }
    if trimmed.contains(char::is_whitespace) || trimmed.contains('/') {
        bail!("unit name contains illegal characters: {trimmed}");
    }
    if trimmed.contains('.') {
        Ok(trimmed.to_string())
    } else {
        Ok(format!("{trimmed}.service"))
    }
}

/// Accepts `host:port` where port parses as u16. We deliberately don't try to
/// resolve the host — the probe will report unreachable on its own each tick,
/// and DNS may differ between the box adding the probe and the daemon's box.
fn validate_port_target(raw: &str) -> Result<String> {
    let trimmed = raw.trim();
    let (host, port) = trimmed
        .rsplit_once(':')
        .with_context(|| format!("expected `host:port`, got `{trimmed}`"))?;
    if host.is_empty() {
        bail!("host part is empty in `{trimmed}`");
    }
    port.parse::<u16>()
        .with_context(|| format!("port part `{port}` is not a valid u16"))?;
    Ok(trimmed.to_string())
}

fn same_path(a: &Path, b: &Path) -> bool {
    match (a.canonicalize().ok(), b.canonicalize().ok()) {
        (Some(a), Some(b)) => a == b,
        _ => a == b,
    }
}

fn prompt(label: &str) -> Result<String> {
    print!("{label}: ");
    io::stdout().flush().ok();
    let mut line = String::new();
    io::stdin().lock().read_line(&mut line)?;
    Ok(line.trim().to_string())
}

fn prompt_default(label: &str, default: &str) -> Result<String> {
    print!("{label} [{default}]: ");
    io::stdout().flush().ok();
    let mut line = String::new();
    io::stdin().lock().read_line(&mut line)?;
    let trimmed = line.trim();
    if trimmed.is_empty() {
        Ok(default.to_string())
    } else {
        Ok(trimmed.to_string())
    }
}

/// Mirror of `probe_resources::resolve_warning_u8` for status display.
/// Keep in sync — both should agree on what threshold the daemon will actually use.
fn effective_warning_percent(configured: u8, critical: u8) -> u8 {
    if configured > 0 && configured < critical {
        configured
    } else {
        critical.saturating_sub(10).max(50)
    }
}

fn format_bytes(n: u64) -> String {
    const KIB: u64 = 1024;
    const MIB: u64 = KIB * 1024;
    const GIB: u64 = MIB * 1024;
    const TIB: u64 = GIB * 1024;
    if n >= TIB {
        format!("{:.1} TiB", n as f64 / TIB as f64)
    } else if n >= GIB {
        format!("{:.1} GiB", n as f64 / GIB as f64)
    } else if n >= MIB {
        format!("{:.1} MiB", n as f64 / MIB as f64)
    } else if n >= KIB {
        format!("{:.1} KiB", n as f64 / KIB as f64)
    } else {
        format!("{n} B")
    }
}

fn format_duration(secs: u64) -> String {
    let days = secs / 86_400;
    let hours = (secs % 86_400) / 3_600;
    let minutes = (secs % 3_600) / 60;
    let seconds = secs % 60;
    if days > 0 {
        format!("{days}d {hours}h {minutes}m")
    } else if hours > 0 {
        format!("{hours}h {minutes}m")
    } else if minutes > 0 {
        format!("{minutes}m {seconds}s")
    } else {
        format!("{seconds}s")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_bytes_picks_unit() {
        assert_eq!(format_bytes(0), "0 B");
        assert_eq!(format_bytes(512), "512 B");
        assert_eq!(format_bytes(2048), "2.0 KiB");
        assert_eq!(format_bytes(2 * 1024 * 1024), "2.0 MiB");
        assert_eq!(format_bytes(3 * 1024 * 1024 * 1024), "3.0 GiB");
    }

    #[test]
    fn format_duration_compacts() {
        assert_eq!(format_duration(0), "0s");
        assert_eq!(format_duration(45), "45s");
        assert_eq!(format_duration(125), "2m 5s");
        assert_eq!(format_duration(3661), "1h 1m");
        assert_eq!(format_duration(90061), "1d 1h 1m");
    }

    #[test]
    fn systemd_unit_appends_service_suffix() {
        assert_eq!(normalize_systemd_unit("nginx").unwrap(), "nginx.service");
        assert_eq!(
            normalize_systemd_unit("nginx.service").unwrap(),
            "nginx.service"
        );
        assert_eq!(
            normalize_systemd_unit("my.timer").unwrap(),
            "my.timer",
            "respects existing suffix",
        );
        assert_eq!(
            normalize_systemd_unit("  redis  ").unwrap(),
            "redis.service",
            "trims whitespace",
        );
    }

    #[test]
    fn systemd_unit_rejects_garbage() {
        assert!(normalize_systemd_unit("").is_err());
        assert!(normalize_systemd_unit("   ").is_err());
        assert!(normalize_systemd_unit("bad name").is_err());
        assert!(normalize_systemd_unit("path/in/it").is_err());
    }

    #[test]
    fn port_target_accepts_host_port() {
        assert_eq!(
            validate_port_target("localhost:5432").unwrap(),
            "localhost:5432"
        );
        assert_eq!(validate_port_target("1.1.1.1:443").unwrap(), "1.1.1.1:443");
        assert_eq!(
            validate_port_target("  example.com:80  ").unwrap(),
            "example.com:80",
            "trims whitespace",
        );
    }

    #[test]
    fn port_target_rejects_garbage() {
        assert!(validate_port_target("nope").is_err(), "missing colon");
        assert!(validate_port_target(":80").is_err(), "empty host");
        assert!(
            validate_port_target("host:abc").is_err(),
            "non-numeric port"
        );
        assert!(validate_port_target("host:99999").is_err(), "port > u16");
    }
}