Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /proc/net/dev parser #843

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions althea_kernel_interface/src/interface_tools.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
use crate::file_io::get_lines;
use crate::KernelInterface;
use crate::KernelInterfaceError as Error;
use althea_types::InterfaceUsageStats;
use regex::Regex;
use std::fs::read_dir;
use std::net::IpAddr;
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use std::str::from_utf8;

/// Utility function for get_per_interface_usage that makes options ? compatible
fn get_helper(input: Option<&&str>) -> Result<String, Error> {
match input {
Some(v) => Ok(v.to_string()),
None => Err(Error::ParseError(
"Missing field in /proc/net/dev!".to_string(),
)),
}
}

impl dyn KernelInterface {
/// Gets usage data from all interfaces from /proc/net/dev, note that for wireguard interfaces
/// updating the interface on the fly (like we do with wg_exit) will reset the usage
/// counter on the wireguard side, but not on in proc which this code pulls from
pub fn get_per_interface_usage(&self) -> Result<Vec<InterfaceUsageStats>, Error> {
let lines = get_lines("/proc/net/dev")?;
// all lines represent an interface, except the first line which is a header
let mut lines = lines.iter();
// skip the first and second lines
lines.next();
lines.next();
let mut ret = Vec::new();
for line in lines {
println!("line ins {}", line);
let parts: Vec<&str> = line.split_ascii_whitespace().collect();
ret.push(InterfaceUsageStats {
interface_name: get_helper(parts.first())?.trim_end_matches(':').to_string(),
recieve_bytes: get_helper(parts.get(1))?.parse()?,
recieve_packets: get_helper(parts.get(2))?.parse()?,
recieve_errors: get_helper(parts.get(3))?.parse()?,
recieve_dropped: get_helper(parts.get(4))?.parse()?,
recieve_fifo_errors: get_helper(parts.get(5))?.parse()?,
recieve_frame_errors: get_helper(parts.get(6))?.parse()?,
recieve_multicast_erorrs: get_helper(parts.get(8))?.parse()?,
transmit_bytes: get_helper(parts.get(9))?.parse()?,
transmit_packets: get_helper(parts.get(10))?.parse()?,
transmit_errors: get_helper(parts.get(11))?.parse()?,
transmit_fifo_errors: get_helper(parts.get(12))?.parse()?,
transmit_collission_erorrs: get_helper(parts.get(13))?.parse()?,
tranmist_carrier_errors: get_helper(parts.get(14))?.parse()?,
})
}
Ok(ret)
}

/// Returns all existing interfaces
pub fn get_interfaces(&self) -> Result<Vec<String>, Error> {
let links = read_dir("/sys/class/net/")?;
Expand Down Expand Up @@ -345,3 +390,9 @@ fn test_get_ip_addresses_linux() {
let val = ("192.168.1.203".parse().unwrap(), 32);
assert!(interfaces.contains(&val))
}

#[test]
fn test_get_interface_usage() {
use crate::KI;
let _ = KI.get_per_interface_usage().unwrap();
}
20 changes: 20 additions & 0 deletions althea_types/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,26 @@ pub fn has_packet_loss(sample: u16) -> bool {
lost_packets > 0
}

/// Represents various usage data and statistics for a given interface
/// parsed from /proc/net/dev
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct InterfaceUsageStats {
pub interface_name: String,
pub recieve_bytes: u64,
pub transmit_bytes: u64,
pub recieve_packets: u64,
pub transmit_packets: u64,
pub recieve_errors: u64,
pub transmit_errors: u64,
pub recieve_dropped: u64,
pub recieve_fifo_errors: u64,
pub transmit_fifo_errors: u64,
pub recieve_frame_errors: u64,
pub recieve_multicast_erorrs: u64,
pub transmit_collission_erorrs: u64,
pub tranmist_carrier_errors: u64,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading