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
use anyhow::{bail, Result};
use std::collections::HashMap;
use std::fs::{read_to_string, File};
use std::io::{self, BufRead};
use std::ops::Sub;
use std::path::Path;
use std::str::FromStr;
#[derive(Default, Debug)]
pub struct Snmp {
hm: HashMap<(String, String), isize>,
}
impl FromStr for Snmp {
type Err = anyhow::Error;
fn from_str(content: &str) -> Result<Self> {
let mut snmp = Snmp::default();
let lines = content.split('\n').collect::<Vec<&str>>();
for i in 0..lines.len() / 2 {
let line1 = lines[i * 2];
let line2 = lines[i * 2 + 1];
let mut iter1 = line1.split_whitespace();
let mut iter2 = line2.split_whitespace();
let prefix;
if let Some(x) = iter1.next() {
prefix = x.to_string();
} else {
bail!("failed to parse: prefix not found")
}
iter2.next();
loop {
let k;
let v: isize;
if let Some(x) = iter1.next() {
k = x;
} else {
break;
}
if let Some(x) = iter2.next() {
v = x.parse()?;
} else {
bail!("failed to parse: number of item is not match.")
}
snmp.insert((prefix.clone(), k.to_string()), v);
}
}
Ok(snmp)
}
}
impl Sub for Snmp {
type Output = Self;
fn sub(self, other: Self) -> Self {
let mut snmp = Snmp::default();
for (k, v) in self.hm.iter() {
assert_eq!(other.hm.contains_key(k), true);
snmp.insert(k.clone(), v - other.hm[k]);
}
snmp
}
}
impl Snmp {
pub fn from_file<P>(path: P) -> Result<Snmp>
where
P: AsRef<Path>,
{
let string = read_to_string(path)?;
Snmp::from_str(&string)
}
pub fn insert(&mut self, k: (String, String), v: isize) {
self.hm.insert(k, v);
}
pub fn lookup(&self, k: &(String, String)) -> Option<&isize> {
self.hm.get(k)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_snmp_from_file() {
let snmp = Snmp::from_file("/proc/net/snmp");
assert_eq!(snmp.is_ok(), true);
}
#[test]
fn test_snmp_ops_sub() {
let snmp1 = Snmp::from_file("/proc/net/snmp").unwrap();
let snmp2 = Snmp::from_file("/proc/net/snmp").unwrap();
let delta = snmp1 - snmp2;
}
}