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
//! > 5.2.30. /proc/uptime
//! > This file contains information detailing how long the system has been on since its last restart.
//! > The output of /proc/uptime is quite minimal:
//! > 350735.47 234388.90
//! > The first number is the total number of seconds the system has been up.
//! > The second number is how much of that time the machine has spent idle, in seconds.
//! >
//! > -- https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/5/html/deployment_guide/s1-proc-topfiles#s2-proc-kcore
//!
define_struct! {
/// Represent the content of /proc/uptime, returned by [`uptime()`](fn.uptime.html)
pub struct Uptime {
total: f64,
idle: f64,
}
}
impl Uptime {
/// Return the system usage from last restart.
pub fn usage(&self) -> f64 {
self.idle / self.total
}
}
use std::str::FromStr;
impl FromStr for Uptime {
type Err = crate::ProcErr;
fn from_str(s: &str) -> Result<Uptime, crate::ProcErr> {
let columns: Vec<&str> = s.split_ascii_whitespace().collect();
if columns.len() != 2 {
return Err("not uptime format".into())
}
let total = columns[0].parse::<f64>()?;
let idle = columns[1].parse::<f64>()?;
Ok(Uptime{
total, idle
})
}
}
instance_impl! {
uptime, "/proc/uptime", Uptime
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parse_uptime() {
let source = "2935986.36 5816188.13";
let correct = Uptime{ total: 2935986.36, idle: 5816188.13 };
assert_eq!(source.parse::<Uptime>().unwrap(), correct);
}
}