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
use crate::util::read_to_string_mut;
use std::{fs, io};
use std::path::Path;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Uptime {
raw: String
}
impl Uptime {
fn path() -> &'static Path {
Path::new("/proc/uptime")
}
#[cfg(test)]
fn from_string(raw: String) -> Self {
Self {raw}
}
pub fn read() -> io::Result<Self> {
Ok(Self {
raw: fs::read_to_string(Self::path())?
})
}
pub fn reload(&mut self) -> io::Result<()> {
read_to_string_mut(Self::path(), &mut self.raw)
}
pub fn all_infos<'a>(&'a self) -> impl Iterator<Item=Duration> + 'a {
self.raw.split(' ')
.filter_map(|v| v.trim().parse().ok())
.map(Duration::from_secs_f64)
}
pub fn uptime(&self) -> Option<Duration> {
self.all_infos().next()
}
pub fn idletime(&self) -> Option<Duration> {
self.all_infos().nth(1)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hostname {
raw: String
}
impl Hostname {
fn path() -> &'static Path {
Path::new("/proc/sys/kernel/hostname")
}
#[cfg(test)]
fn from_string(raw: String) -> Self {
Self {raw}
}
pub fn read() -> io::Result<Self> {
Ok(Self {
raw: fs::read_to_string(Self::path())?
})
}
pub fn reload(&mut self) -> io::Result<()> {
read_to_string_mut(Self::path(), &mut self.raw)
}
pub fn hostname(&self) -> &str {
self.raw.trim()
}
pub fn into_string(self) -> String {
self.raw
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OsRelease {
raw: String
}
impl OsRelease {
fn path() -> &'static Path {
Path::new("/proc/sys/kernel/osrelease")
}
#[cfg(test)]
fn from_string(raw: String) -> Self {
Self {raw}
}
pub fn read() -> io::Result<Self> {
Ok(Self {
raw: fs::read_to_string(Self::path())?
})
}
pub fn reload(&mut self) -> io::Result<()> {
read_to_string_mut(Self::path(), &mut self.raw)
}
pub fn full_str(&self) -> &str {
self.raw.trim()
}
pub fn into_string(self) -> String {
self.raw
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoadAvg {
raw: String
}
impl LoadAvg {
fn path() -> &'static Path {
Path::new("/proc/loadavg")
}
#[cfg(test)]
fn from_string(raw: String) -> Self {
Self {raw}
}
pub fn read() -> io::Result<Self> {
Ok(Self {
raw: fs::read_to_string(Self::path())?
})
}
pub fn reload(&mut self) -> io::Result<()> {
read_to_string_mut(Self::path(), &mut self.raw)
}
pub fn values<'a>(&'a self) -> impl Iterator<Item=&'a str> {
self.raw.split(' ')
.map(str::trim)
}
pub fn average(&self) -> Option<(f32, f32, f32)> {
let mut vals = self.values()
.take(3)
.map(|v| v.parse().ok());
Some((vals.next()??, vals.next()??, vals.next()??))
}
pub fn threads(&self) -> Option<(usize, usize)> {
let mut vals = self.values()
.nth(3)?
.split('/')
.map(|v| v.parse().ok());
Some((vals.next()??, vals.next()??))
}
pub fn newest_pid(&self) -> Option<u32> {
self.values().last()?
.parse().ok()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn uptime() -> Uptime {
Uptime::from_string("220420.83 5275548.45\n".into())
}
#[test]
fn uptime_methods() {
assert_eq!(uptime().uptime().unwrap().as_secs(), 220420);
assert_eq!(uptime().idletime().unwrap().as_secs(), 5275548);
}
#[test]
fn hostname() {
let name = Hostname::from_string("test-hostname\n".into());
assert_eq!(name.hostname(), "test-hostname");
}
#[test]
fn os_release() {
let name = OsRelease::from_string("test-hostname\n".into());
assert_eq!(name.full_str(), "test-hostname");
}
#[test]
fn load_avg() {
let s = LoadAvg::from_string("13.37 15.82 16.64 14/1444 436826\n".into());
assert_eq!(s.average().unwrap(), (13.37, 15.82, 16.64));
assert_eq!(s.threads().unwrap(), (14, 1444));
assert_eq!(s.newest_pid().unwrap(), 436826);
}
}