summaryrefslogtreecommitdiffstats
path: root/src/service/systemd.rs
blob: 2e3b74cf532593d800ad0d291ad63cbad6e4291a (plain)
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
use std::{fmt::Display, process::Command};

use serde::Deserialize;

use crate::{Check, Error, Status};

#[derive(Debug, Clone, Deserialize)]
pub struct Systemd {
    pub service: String,
}

impl Display for Systemd {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.service", self.service.trim_end_matches(".service"))
    }
}

impl Systemd {
    pub async fn check(&self) -> Result<Check, Error> {
        let output = Command::new("systemctl")
            .arg("is-active")
            .arg(&self.service)
            .output()?;

        let stdout = String::from_utf8_lossy(&output.stdout).to_string();
        Ok((!output.status.success())
            .then(|| Check {
                status: Status::Fail,
                output: Some(format!("Service state: {}", stdout.trim())),
            })
            .unwrap_or_default())
    }
}