From d2981e2738595b6dfa28a9cf0b091a4a211e9785 Mon Sep 17 00:00:00 2001 From: Christian Ebner Date: Fri, 12 Apr 2019 13:21:22 +0200 Subject: [PATCH] src/tools/procfs.rs: implement read_proc_net_dev() Signed-off-by: Christian Ebner --- src/tools/procfs.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/tools/procfs.rs b/src/tools/procfs.rs index 9ca5f32a..f1f09637 100644 --- a/src/tools/procfs.rs +++ b/src/tools/procfs.rs @@ -233,3 +233,33 @@ pub fn read_memory_usage() -> Result { _ => bail!("Error while parsing '{}'", path), } } + +#[derive(Debug)] +pub struct ProcFsNetDev { + pub device: String, + pub receive: u64, + pub send: u64, +} + +pub fn read_proc_net_dev() -> Result, Error> { + let path = "/proc/net/dev"; + let file = OpenOptions::new().read(true).open(&path)?; + + let mut result = Vec::new(); + for line in BufReader::new(&file).lines().skip(2) { + let content = line?; + let mut iter = content.split_whitespace(); + match (iter.next(), iter.next(), iter.skip(7).next()) { + (Some(device), Some(receive), Some(send)) => { + result.push(ProcFsNetDev { + device: device[..device.len()-1].to_string(), + receive: receive.parse::()?, + send: send.parse::()?, + }); + }, + _ => bail!("Error while parsing '{}'", path), + } + } + + Ok(result) +}