mprober_lib/uptime/
mod.rs1use std::{
2 io::ErrorKind,
3 time::{Duration, SystemTime},
4};
5
6use chrono::prelude::*;
7
8use crate::scanner_rust::{generic_array::typenum::U24, ScannerAscii, ScannerError};
9
10#[derive(Default, Debug, Clone)]
11pub struct Uptime {
12 pub total_uptime: Duration,
13 pub all_cpu_idle_time: Duration,
14}
15
16impl Uptime {
17 #[inline]
28 pub fn get_btime(&self) -> DateTime<Utc> {
29 (SystemTime::now() - self.total_uptime).into()
30 }
31}
32
33#[inline]
43pub fn get_uptime() -> Result<Uptime, ScannerError> {
44 let mut sc: ScannerAscii<_, U24> = ScannerAscii::scan_path2("/proc/uptime")?;
45
46 let uptime = sc.next_f64()?.ok_or(ErrorKind::UnexpectedEof)?;
47 let idle_time = sc.next_f64()?.ok_or(ErrorKind::UnexpectedEof)?;
48
49 Ok(Uptime {
50 total_uptime: Duration::from_secs_f64(uptime),
51 all_cpu_idle_time: Duration::from_secs_f64(idle_time),
52 })
53}