dae 0.1.3

A Linux daemon management TUI powered by scrin, aisling, and scrin-widgets concepts.
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
//! Linux daemon probing and control for dae.
//!
//! dae is intentionally systemd-first: it reads service state with `systemctl`,
//! opens historical context with `journalctl`, and routes management actions
//! back through systemd so cgroup/process ownership stays correct.

use std::collections::HashMap;
use std::process::Command;

use anyhow::{Context, Result, bail};

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Daemon {
    pub unit: String,
    pub description: String,
    pub load: String,
    pub active: String,
    pub sub: String,
    pub main_pid: Option<u32>,
    pub restarts: Option<u32>,
    pub exec_status: Option<i32>,
    pub memory_bytes: Option<u64>,
    pub cpu_nsec: Option<u64>,
    pub result: String,
    pub unit_file_state: String,
    pub fragment_path: String,
    pub active_enter: String,
    pub anomalies: Vec<Anomaly>,
}

impl Daemon {
    pub fn severity(&self) -> u8 {
        self.anomalies
            .iter()
            .map(|anomaly| anomaly.severity)
            .max()
            .unwrap_or(0)
    }

    pub fn anomaly_summary(&self) -> String {
        if self.anomalies.is_empty() {
            return "ok".to_string();
        }

        self.anomalies
            .iter()
            .take(3)
            .map(|anomaly| anomaly.label.as_str())
            .collect::<Vec<_>>()
            .join(", ")
    }

    pub fn state_label(&self) -> String {
        format!("{}/{}", self.active, self.sub)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Anomaly {
    pub label: String,
    pub detail: String,
    pub severity: u8,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DaemonAction {
    Start,
    Stop,
    Restart,
    Reload,
    Enable,
    Disable,
    KillTerm,
    KillKill,
}

impl DaemonAction {
    pub fn label(self) -> &'static str {
        match self {
            Self::Start => "start",
            Self::Stop => "stop",
            Self::Restart => "restart",
            Self::Reload => "reload",
            Self::Enable => "enable",
            Self::Disable => "disable",
            Self::KillTerm => "term",
            Self::KillKill => "kill",
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CommandReport {
    pub ok: bool,
    pub command: String,
    pub output: String,
}

pub fn load_daemons() -> Result<Vec<Daemon>> {
    let output = command_output(
        "systemctl",
        &[
            "list-units",
            "--type=service",
            "--all",
            "--no-legend",
            "--no-pager",
            "--plain",
        ],
    )
    .context("failed to list systemd services")?;

    let mut daemons = parse_list_units(&output);
    enrich_daemons(&mut daemons)?;

    for daemon in &mut daemons {
        daemon.anomalies = detect_anomalies(daemon);
    }

    daemons.sort_by(|a, b| {
        b.severity()
            .cmp(&a.severity())
            .then_with(|| a.unit.cmp(&b.unit))
    });
    Ok(daemons)
}

pub fn load_status(unit: &str) -> Result<Vec<String>> {
    let output = command_output(
        "systemctl",
        &["status", unit, "--no-pager", "--lines=40", "--full"],
    )
    .with_context(|| format!("failed to read status for {unit}"))?;
    Ok(output.lines().map(clean_line).collect())
}

pub fn load_journal(unit: &str, lookback_hours: u64, lines: usize) -> Result<Vec<String>> {
    let since = if lookback_hours == 1 {
        "1 hour ago".to_string()
    } else {
        format!("{lookback_hours} hours ago")
    };
    let line_count = lines.clamp(20, 2_000).to_string();
    let output = command_output(
        "journalctl",
        &[
            "-u",
            unit,
            "--since",
            &since,
            "-n",
            &line_count,
            "--no-pager",
            "--output=short-iso",
        ],
    )
    .with_context(|| format!("failed to read journal for {unit}"))?;
    let lines = output.lines().map(clean_line).collect::<Vec<_>>();
    if lines.is_empty() {
        Ok(vec![format!("no journal entries for {unit} since {since}")])
    } else {
        Ok(lines)
    }
}

pub fn apply_action(unit: &str, action: DaemonAction) -> Result<CommandReport> {
    let args = match action {
        DaemonAction::Start => vec!["start", unit],
        DaemonAction::Stop => vec!["stop", unit],
        DaemonAction::Restart => vec!["restart", unit],
        DaemonAction::Reload => vec!["reload", unit],
        DaemonAction::Enable => vec!["enable", unit],
        DaemonAction::Disable => vec!["disable", unit],
        DaemonAction::KillTerm => vec!["kill", "--signal=TERM", unit],
        DaemonAction::KillKill => vec!["kill", "--signal=KILL", unit],
    };

    let output = Command::new("systemctl")
        .args(&args)
        .output()
        .with_context(|| format!("failed to run systemctl {} {unit}", action.label()))?;
    let mut text = String::new();
    text.push_str(&String::from_utf8_lossy(&output.stdout));
    text.push_str(&String::from_utf8_lossy(&output.stderr));
    let text = text.trim().to_string();
    Ok(CommandReport {
        ok: output.status.success(),
        command: format!("systemctl {}", args.join(" ")),
        output: if text.is_empty() {
            "no output".to_string()
        } else {
            text
        },
    })
}

pub fn format_bytes(bytes: Option<u64>) -> String {
    let Some(bytes) = bytes else {
        return "-".to_string();
    };
    const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"];
    let mut value = bytes as f64;
    let mut unit = 0usize;
    while value >= 1024.0 && unit + 1 < UNITS.len() {
        value /= 1024.0;
        unit += 1;
    }
    if unit == 0 {
        format!("{} {}", bytes, UNITS[unit])
    } else {
        format!("{value:.1} {}", UNITS[unit])
    }
}

pub fn format_cpu(cpu_nsec: Option<u64>) -> String {
    let Some(nsec) = cpu_nsec else {
        return "-".to_string();
    };
    let seconds = nsec / 1_000_000_000;
    if seconds < 60 {
        format!("{seconds}s")
    } else if seconds < 3_600 {
        format!("{}m", seconds / 60)
    } else {
        format!("{}h", seconds / 3_600)
    }
}

fn enrich_daemons(daemons: &mut [Daemon]) -> Result<()> {
    if daemons.is_empty() {
        return Ok(());
    }

    let mut args = vec![
        "show".to_string(),
        "--property=Id,LoadState,ActiveState,SubState,Description,MainPID,NRestarts,ExecMainStatus,MemoryCurrent,CPUUsageNSec,Result,UnitFileState,FragmentPath,ActiveEnterTimestamp".to_string(),
        "--no-pager".to_string(),
    ];
    args.extend(daemons.iter().map(|daemon| daemon.unit.clone()));
    let arg_refs = args.iter().map(String::as_str).collect::<Vec<_>>();
    let output = command_output("systemctl", &arg_refs).context("failed to inspect services")?;
    let blocks = parse_show_blocks(&output);
    let index = daemons
        .iter()
        .enumerate()
        .map(|(idx, daemon)| (daemon.unit.clone(), idx))
        .collect::<HashMap<_, _>>();

    for block in blocks {
        let Some(unit) = block.get("Id") else {
            continue;
        };
        let Some(idx) = index.get(unit).copied() else {
            continue;
        };
        let daemon = &mut daemons[idx];
        daemon.description = block
            .get("Description")
            .cloned()
            .unwrap_or_else(|| daemon.description.clone());
        daemon.load = block
            .get("LoadState")
            .cloned()
            .unwrap_or_else(|| daemon.load.clone());
        daemon.active = block
            .get("ActiveState")
            .cloned()
            .unwrap_or_else(|| daemon.active.clone());
        daemon.sub = block
            .get("SubState")
            .cloned()
            .unwrap_or_else(|| daemon.sub.clone());
        daemon.main_pid = parse_nonzero_u32(block.get("MainPID"));
        daemon.restarts = parse_u32(block.get("NRestarts"));
        daemon.exec_status = parse_i32(block.get("ExecMainStatus"));
        daemon.memory_bytes = parse_nonzero_u64(block.get("MemoryCurrent"));
        daemon.cpu_nsec = parse_nonzero_u64(block.get("CPUUsageNSec"));
        daemon.result = block.get("Result").cloned().unwrap_or_default();
        daemon.unit_file_state = block.get("UnitFileState").cloned().unwrap_or_default();
        daemon.fragment_path = block.get("FragmentPath").cloned().unwrap_or_default();
        daemon.active_enter = block
            .get("ActiveEnterTimestamp")
            .cloned()
            .unwrap_or_default();
    }

    Ok(())
}

fn parse_list_units(output: &str) -> Vec<Daemon> {
    output
        .lines()
        .filter_map(|line| {
            let parts = line.split_whitespace().collect::<Vec<_>>();
            if parts.len() < 4 || !parts[0].ends_with(".service") {
                return None;
            }

            let description = if parts.len() > 4 {
                parts[4..].join(" ")
            } else {
                String::new()
            };

            Some(Daemon {
                unit: parts[0].to_string(),
                load: parts[1].to_string(),
                active: parts[2].to_string(),
                sub: parts[3].to_string(),
                description,
                ..Daemon::default()
            })
        })
        .collect()
}

fn parse_show_blocks(output: &str) -> Vec<HashMap<String, String>> {
    let mut blocks = Vec::new();
    let mut block = HashMap::new();

    for line in output.lines() {
        if line.trim().is_empty() {
            if !block.is_empty() {
                blocks.push(block);
                block = HashMap::new();
            }
            continue;
        }
        if let Some((key, value)) = line.split_once('=') {
            block.insert(key.to_string(), value.to_string());
        }
    }

    if !block.is_empty() {
        blocks.push(block);
    }

    blocks
}

fn detect_anomalies(daemon: &Daemon) -> Vec<Anomaly> {
    let mut anomalies = Vec::new();

    if daemon.load == "not-found" || daemon.load == "error" {
        anomalies.push(Anomaly {
            label: daemon.load.clone(),
            detail: "unit is referenced but not loadable".to_string(),
            severity: 95,
        });
    }

    if daemon.active == "failed" || daemon.result != "success" && !daemon.result.is_empty() {
        anomalies.push(Anomaly {
            label: "failed".to_string(),
            detail: format!("result={}", blank_dash(&daemon.result)),
            severity: 100,
        });
    }

    if daemon.active == "activating" || daemon.active == "deactivating" {
        anomalies.push(Anomaly {
            label: daemon.active.clone(),
            detail: "transitional service state".to_string(),
            severity: 70,
        });
    }

    if daemon.sub == "auto-restart" || daemon.sub == "start-pre" || daemon.sub == "stop-sigterm" {
        anomalies.push(Anomaly {
            label: daemon.sub.clone(),
            detail: "systemd reports a noisy sub-state".to_string(),
            severity: 80,
        });
    }

    if daemon.active == "active" && daemon.sub == "running" && daemon.main_pid.is_none() {
        anomalies.push(Anomaly {
            label: "pid-missing".to_string(),
            detail: "running service has no MainPID".to_string(),
            severity: 60,
        });
    }

    if let Some(restarts) = daemon.restarts {
        if restarts >= 10 {
            anomalies.push(Anomaly {
                label: "restart-storm".to_string(),
                detail: format!("{restarts} restarts recorded"),
                severity: 90,
            });
        } else if restarts >= 3 {
            anomalies.push(Anomaly {
                label: "restarts".to_string(),
                detail: format!("{restarts} restarts recorded"),
                severity: 65,
            });
        }
    }

    if let Some(status) = daemon.exec_status {
        if status != 0 && daemon.active != "active" {
            anomalies.push(Anomaly {
                label: "exit-code".to_string(),
                detail: format!("ExecMainStatus={status}"),
                severity: 75,
            });
        }
    }

    if let Some(bytes) = daemon.memory_bytes {
        if bytes >= 2 * 1024 * 1024 * 1024 {
            anomalies.push(Anomaly {
                label: "high-mem".to_string(),
                detail: format!("memory={}", format_bytes(Some(bytes))),
                severity: 72,
            });
        }
    }

    anomalies
}

fn command_output(program: &str, args: &[&str]) -> Result<String> {
    let output = Command::new(program)
        .args(args)
        .output()
        .with_context(|| format!("failed to execute {program}"))?;
    let mut text = String::new();
    text.push_str(&String::from_utf8_lossy(&output.stdout));
    text.push_str(&String::from_utf8_lossy(&output.stderr));
    if !output.status.success() {
        bail!("{program} {} failed: {}", args.join(" "), text.trim());
    }
    Ok(text)
}

fn parse_u32(value: Option<&String>) -> Option<u32> {
    value.and_then(|value| value.parse::<u32>().ok())
}

fn parse_nonzero_u32(value: Option<&String>) -> Option<u32> {
    parse_u32(value).filter(|value| *value != 0)
}

fn parse_i32(value: Option<&String>) -> Option<i32> {
    value.and_then(|value| value.parse::<i32>().ok())
}

fn parse_nonzero_u64(value: Option<&String>) -> Option<u64> {
    value
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value != 0)
}

fn clean_line(line: &str) -> String {
    line.chars()
        .map(|ch| if ch.is_control() { ' ' } else { ch })
        .collect::<String>()
}

fn blank_dash(value: &str) -> &str {
    if value.is_empty() { "-" } else { value }
}

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

    #[test]
    fn parses_systemctl_list_units() {
        let input = "sshd.service loaded active running OpenSSH Daemon\nmissing.service not-found inactive dead missing.service";
        let daemons = parse_list_units(input);
        assert_eq!(daemons.len(), 2);
        assert_eq!(daemons[0].unit, "sshd.service");
        assert_eq!(daemons[0].description, "OpenSSH Daemon");
        assert_eq!(daemons[1].load, "not-found");
    }

    #[test]
    fn parses_systemctl_show_blocks() {
        let input = "Id=a.service\nMainPID=42\n\nId=b.service\nMainPID=0\n";
        let blocks = parse_show_blocks(input);
        assert_eq!(blocks.len(), 2);
        assert_eq!(blocks[0]["Id"], "a.service");
        assert_eq!(blocks[1]["MainPID"], "0");
    }

    #[test]
    fn failed_daemon_is_severe() {
        let daemon = Daemon {
            unit: "bad.service".to_string(),
            active: "failed".to_string(),
            result: "exit-code".to_string(),
            ..Daemon::default()
        };
        let anomalies = detect_anomalies(&daemon);
        assert!(anomalies.iter().any(|anomaly| anomaly.label == "failed"));
    }
}