use crate::error::{IronError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkInterfaceInfo {
pub name: String,
pub is_up: Option<bool>,
pub is_running: Option<bool>,
pub rx_bytes: u64,
pub rx_packets: u64,
pub rx_errors: u64,
pub rx_drops: u64,
pub tx_bytes: u64,
pub tx_packets: u64,
pub tx_errors: u64,
pub tx_drops: Option<u64>,
pub speed_mbps: Option<u32>,
pub mtu: Option<u32>,
pub mac_address: Option<String>,
pub ipv4_addresses: Option<Vec<String>>,
pub ipv6_addresses: Option<Vec<String>>,
}
impl NetworkInterfaceInfo {
pub fn total_bytes(&self) -> u64 {
self.rx_bytes + self.tx_bytes
}
pub fn total_packets(&self) -> u64 {
self.rx_packets + self.tx_packets
}
pub fn total_errors(&self) -> u64 {
self.rx_errors + self.tx_errors
}
pub fn total_drops(&self) -> Option<u64> {
self.tx_drops.map(|tx| self.rx_drops + tx)
}
pub fn rx_mb(&self) -> f64 {
self.rx_bytes as f64 / (1024.0 * 1024.0)
}
pub fn tx_mb(&self) -> f64 {
self.tx_bytes as f64 / (1024.0 * 1024.0)
}
pub fn total_mb(&self) -> f64 {
self.total_bytes() as f64 / (1024.0 * 1024.0)
}
pub fn is_active(&self) -> bool {
self.is_up == Some(true) && self.is_running == Some(true)
}
}
#[derive(Debug, Clone)]
pub struct BandwidthStats {
pub prev_rx_bytes: u64,
pub prev_tx_bytes: u64,
pub prev_time: std::time::Instant,
}
pub struct NetworkMonitor {
prev_stats: HashMap<String, BandwidthStats>,
last_update: std::time::Instant,
}
impl NetworkMonitor {
pub fn new() -> Result<Self> {
Ok(Self {
prev_stats: HashMap::new(),
last_update: std::time::Instant::now(),
})
}
pub fn interfaces(&mut self) -> Result<Vec<NetworkInterfaceInfo>> {
Self::enumerate_interfaces()
}
pub fn active_interfaces(&mut self) -> Result<Vec<NetworkInterfaceInfo>> {
let interfaces = self.interfaces()?;
Ok(interfaces
.into_iter()
.filter(|iface| iface.is_active())
.collect())
}
pub fn interface_by_name(&mut self, name: &str) -> Result<Option<NetworkInterfaceInfo>> {
let interfaces = self.interfaces()?;
Ok(interfaces.into_iter().find(|iface| iface.name == name))
}
pub fn bandwidth_rate(
&mut self,
name: &str,
current: &NetworkInterfaceInfo,
) -> Option<(f64, f64)> {
let now = std::time::Instant::now();
let previous = self.prev_stats.insert(
name.to_string(),
BandwidthStats {
prev_rx_bytes: current.rx_bytes,
prev_tx_bytes: current.tx_bytes,
prev_time: now,
},
);
self.last_update = now;
let prev = previous?;
let elapsed = now.duration_since(prev.prev_time).as_secs_f64();
if elapsed <= 0.0 {
return None;
}
Some((
current.rx_bytes.saturating_sub(prev.prev_rx_bytes) as f64 / elapsed,
current.tx_bytes.saturating_sub(prev.prev_tx_bytes) as f64 / elapsed,
))
}
pub fn interface_count(&mut self) -> Result<usize> {
Ok(self.interfaces()?.len())
}
pub fn active_interface_count(&mut self) -> Result<usize> {
Ok(self.active_interfaces()?.len())
}
#[cfg(target_os = "linux")]
fn enumerate_interfaces() -> Result<Vec<NetworkInterfaceInfo>> {
linux::enumerate_interfaces()
}
#[cfg(target_os = "windows")]
fn enumerate_interfaces() -> Result<Vec<NetworkInterfaceInfo>> {
windows::enumerate_interfaces()
}
#[cfg(target_os = "macos")]
fn enumerate_interfaces() -> Result<Vec<NetworkInterfaceInfo>> {
macos::enumerate_interfaces()
}
}
impl Default for NetworkMonitor {
fn default() -> Self {
Self::new().unwrap_or_else(|_| Self {
prev_stats: HashMap::new(),
last_update: std::time::Instant::now(),
})
}
}
#[cfg(any(target_os = "linux", test))]
const IFF_RUNNING: u32 = 0x40;
#[cfg(any(target_os = "linux", test))]
fn parse_operstate(text: &str) -> Option<bool> {
let state = text.trim().to_ascii_lowercase();
if state == "unknown" {
None
} else {
Some(state.contains("up"))
}
}
#[cfg(any(target_os = "linux", test))]
fn parse_iff_running(flags: &str) -> Option<bool> {
let hex = flags.trim().strip_prefix("0x")?;
let bits = u32::from_str_radix(hex, 16).ok()?;
Some(bits & IFF_RUNNING != 0)
}
#[cfg(target_os = "linux")]
mod linux {
use super::*;
use std::fs;
use std::path::Path;
pub fn enumerate_interfaces() -> Result<Vec<NetworkInterfaceInfo>> {
let mut interfaces = Vec::new();
let sys_net = Path::new("/sys/class/net");
if !sys_net.exists() {
return Err(IronError::UnsupportedPlatform(
"/sys/class/net not available".to_string(),
));
}
for entry in fs::read_dir(sys_net)? {
let entry = entry?;
let iface_name = entry.file_name().to_string_lossy().to_string();
if let Ok(iface_info) = read_interface_info(&iface_name) {
interfaces.push(iface_info);
}
}
Ok(interfaces)
}
fn read_interface_info(name: &str) -> Result<NetworkInterfaceInfo> {
let base_path = format!("/sys/class/net/{}", name);
let is_up = read_operstate(&base_path);
let is_running = read_flags(&base_path);
let stats_path = format!("{}/statistics", base_path);
let rx_bytes = read_stat(&stats_path, "rx_bytes")?;
let rx_packets = read_stat(&stats_path, "rx_packets")?;
let rx_errors = read_stat(&stats_path, "rx_errors")?;
let rx_drops = read_stat(&stats_path, "rx_dropped")?;
let tx_bytes = read_stat(&stats_path, "tx_bytes")?;
let tx_packets = read_stat(&stats_path, "tx_packets")?;
let tx_errors = read_stat(&stats_path, "tx_errors")?;
let tx_drops = read_stat(&stats_path, "tx_dropped")?;
let mtu = read_file_u32(&format!("{}/mtu", base_path));
let speed_mbps = read_file_u32(&format!("{}/speed", base_path));
let mac_address = read_file_string(&format!("{}/address", base_path));
let (ipv4_addresses, ipv6_addresses) = read_ip_addresses(name);
Ok(NetworkInterfaceInfo {
name: name.to_string(),
is_up,
is_running,
rx_bytes,
rx_packets,
rx_errors,
rx_drops,
tx_bytes,
tx_packets,
tx_errors,
tx_drops: Some(tx_drops),
speed_mbps,
mtu,
mac_address,
ipv4_addresses,
ipv6_addresses,
})
}
fn read_operstate(base_path: &str) -> Option<bool> {
let path = format!("{}/operstate", base_path);
fs::read_to_string(&path)
.ok()
.and_then(|s| parse_operstate(&s))
}
fn read_flags(base_path: &str) -> Option<bool> {
let path = format!("{}/flags", base_path);
fs::read_to_string(&path)
.ok()
.and_then(|s| parse_iff_running(&s))
}
fn read_stat(stats_path: &str, stat_name: &str) -> Result<u64> {
let path = format!("{}/{}", stats_path, stat_name);
let content = fs::read_to_string(&path)?;
content
.trim()
.parse()
.map_err(|e| IronError::Parse(format!("Failed to parse {}: {}", stat_name, e)))
}
fn read_file_u32(path: &str) -> Option<u32> {
fs::read_to_string(path)
.ok()
.and_then(|s| s.trim().parse().ok())
}
fn read_file_string(path: &str) -> Option<String> {
fs::read_to_string(path)
.ok()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
}
fn read_ip_addresses(iface_name: &str) -> (Option<Vec<String>>, Option<Vec<String>>) {
let mut ipv6_addrs = Vec::new();
if let Ok(content) = fs::read_to_string("/proc/net/if_inet6") {
for line in content.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 6 && parts[5] == iface_name {
let hex_addr = parts[0];
if hex_addr.len() == 32 {
let ipv6 = format!(
"{:0>4}:{:0>4}:{:0>4}:{:0>4}:{:0>4}:{:0>4}:{:0>4}:{:0>4}",
&hex_addr[0..4],
&hex_addr[4..8],
&hex_addr[8..12],
&hex_addr[12..16],
&hex_addr[16..20],
&hex_addr[20..24],
&hex_addr[24..28],
&hex_addr[28..32]
);
ipv6_addrs.push(ipv6);
}
}
}
}
(None, Some(ipv6_addrs))
}
}
#[cfg(target_os = "windows")]
mod windows {
use super::*;
use std::ptr;
#[repr(C)]
struct MIB_IF_ROW2 {
interface_luid: u64,
interface_index: u32,
interface_guid: [u8; 16],
alias: [u16; 257], description: [u16; 257],
phys_addr_length: u32,
phys_addr: [u8; 32],
permanent_phys_addr: [u8; 32],
mtu: u32,
if_type: u32,
tunnel_type: u32,
media_type: u32,
phys_medium_type: u32,
access_type: u32,
direction_type: u32,
interface_and_oper_status_flags: u8,
oper_status: u32,
admin_status: u32,
media_connect_state: u32,
network_guid: [u8; 16],
connection_type: u32,
transmit_link_speed: u64,
receive_link_speed: u64,
in_octets: u64,
in_ucast_pkts: u64,
in_nucast_pkts: u64,
in_discards: u64,
in_errors: u64,
in_unknown_protos: u64,
in_ucast_octets: u64,
in_mcast_octets: u64,
in_bcast_octets: u64,
out_octets: u64,
out_ucast_pkts: u64,
out_nucast_pkts: u64,
out_discards: u64,
out_errors: u64,
out_ucast_octets: u64,
out_mcast_octets: u64,
out_bcast_octets: u64,
out_qlen: u64,
}
#[repr(C)]
struct MIB_IF_TABLE2 {
num_entries: u32,
table: [MIB_IF_ROW2; 1], }
const IF_OPER_STATUS_UP: u32 = 1;
#[allow(dead_code)]
const IF_OPER_STATUS_DOWN: u32 = 2;
#[allow(dead_code)]
const IF_OPER_STATUS_TESTING: u32 = 3;
const IF_OPER_STATUS_DORMANT: u32 = 5;
const MEDIA_CONNECT_STATE_CONNECTED: u32 = 1;
const IF_TYPE_SOFTWARE_LOOPBACK: u32 = 24;
const IF_TYPE_TUNNEL: u32 = 131;
#[link(name = "iphlpapi")]
extern "system" {
fn GetIfTable2(table: *mut *mut MIB_IF_TABLE2) -> u32;
fn FreeMibTable(memory: *mut std::ffi::c_void);
}
const NO_ERROR: u32 = 0;
pub fn enumerate_interfaces() -> Result<Vec<NetworkInterfaceInfo>> {
let mut interfaces = Vec::new();
let mut table: *mut MIB_IF_TABLE2 = ptr::null_mut();
unsafe {
let result = GetIfTable2(&mut table);
if result != NO_ERROR {
return Err(IronError::System(format!("GetIfTable2 failed: {}", result)));
}
if table.is_null() {
return Ok(interfaces);
}
let num_entries = (*table).num_entries as usize;
let table_ptr = &(*table).table as *const MIB_IF_ROW2;
for i in 0..num_entries {
let row = &*table_ptr.add(i);
if row.if_type == IF_TYPE_SOFTWARE_LOOPBACK || row.if_type == IF_TYPE_TUNNEL {
continue;
}
let alias_len = row.alias.iter().position(|&c| c == 0).unwrap_or(257);
let alias = String::from_utf16_lossy(&row.alias[..alias_len]);
let desc_len = row.description.iter().position(|&c| c == 0).unwrap_or(257);
let description = String::from_utf16_lossy(&row.description[..desc_len]);
let name = if !alias.is_empty() {
alias
} else if !description.is_empty() {
description
} else {
format!("Interface {}", row.interface_index)
};
if name.contains("Loopback")
|| name.contains("isatap")
|| name.contains("Teredo")
|| name.contains("6TO4")
|| name.contains("-WFP Native MAC Layer")
|| name.contains("-WFP 802.3 MAC Layer")
|| name.contains("-Npcap Packet Driver")
|| name.contains("-QoS Packet Scheduler")
|| name.contains("-Native WiFi Filter Driver")
|| name.contains("-Virtual Filtering Platform")
|| name.contains("-Hyper-V Virtual Switch")
{
continue;
}
let mac_address = if row.phys_addr_length > 0 {
let len = row.phys_addr_length as usize;
Some(
row.phys_addr[..len.min(6)]
.iter()
.map(|b| format!("{:02X}", b))
.collect::<Vec<_>>()
.join(":"),
)
} else {
None
};
let speed_mbps = if row.transmit_link_speed > 0 {
Some((row.transmit_link_speed / 1_000_000) as u32)
} else {
None
};
let is_up = Some(
row.oper_status == IF_OPER_STATUS_UP
|| (row.oper_status == IF_OPER_STATUS_DORMANT
&& row.media_connect_state == MEDIA_CONNECT_STATE_CONNECTED),
);
let is_running = Some(
row.admin_status == 1
&& row.media_connect_state == MEDIA_CONNECT_STATE_CONNECTED,
);
interfaces.push(NetworkInterfaceInfo {
name,
is_up,
is_running,
rx_bytes: row.in_octets,
rx_packets: row.in_ucast_pkts + row.in_nucast_pkts,
rx_errors: row.in_errors,
rx_drops: row.in_discards,
tx_bytes: row.out_octets,
tx_packets: row.out_ucast_pkts + row.out_nucast_pkts,
tx_errors: row.out_errors,
tx_drops: Some(row.out_discards),
speed_mbps,
mtu: Some(row.mtu),
mac_address,
ipv4_addresses: None,
ipv6_addresses: None,
});
}
FreeMibTable(table as *mut std::ffi::c_void);
}
Ok(interfaces)
}
}
#[cfg(target_os = "macos")]
mod macos {
use super::*;
use libc::{
freeifaddrs, getifaddrs, ifaddrs, sockaddr_in, sockaddr_in6, AF_INET, AF_INET6, AF_LINK,
};
use std::ffi::CStr;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::ptr;
#[repr(C)]
struct IfData {
ifi_type: u8,
ifi_typelen: u8,
ifi_physical: u8,
ifi_addrlen: u8,
ifi_hdrlen: u8,
ifi_recvquota: u8,
ifi_xmitquota: u8,
ifi_unused1: u8,
ifi_mtu: u32,
ifi_metric: u32,
ifi_baudrate: u32,
ifi_ipackets: u32,
ifi_ierrors: u32,
ifi_opackets: u32,
ifi_oerrors: u32,
ifi_collisions: u32,
ifi_ibytes: u32,
ifi_obytes: u32,
ifi_imcasts: u32,
ifi_omcasts: u32,
ifi_iqdrops: u32,
ifi_noproto: u32,
ifi_recvtiming: u32,
ifi_xmittiming: u32,
ifi_lastchange: libc::timeval,
}
#[repr(C)]
#[allow(dead_code)]
struct IfData64 {
ifi_type: u8,
ifi_typelen: u8,
ifi_physical: u8,
ifi_addrlen: u8,
ifi_hdrlen: u8,
ifi_recvquota: u8,
ifi_xmitquota: u8,
ifi_unused1: u8,
ifi_mtu: u32,
ifi_metric: u32,
ifi_baudrate: u64,
ifi_ipackets: u64,
ifi_ierrors: u64,
ifi_opackets: u64,
ifi_oerrors: u64,
ifi_collisions: u64,
ifi_ibytes: u64,
ifi_obytes: u64,
ifi_imcasts: u64,
ifi_omcasts: u64,
ifi_iqdrops: u64,
ifi_noproto: u64,
ifi_recvtiming: u32,
ifi_xmittiming: u32,
ifi_lastchange: libc::timeval,
}
pub fn enumerate_interfaces() -> Result<Vec<NetworkInterfaceInfo>> {
let mut interfaces = Vec::new();
let mut seen_interfaces: std::collections::HashMap<String, NetworkInterfaceInfo> =
std::collections::HashMap::new();
let mut with_link_stats: std::collections::HashSet<String> =
std::collections::HashSet::new();
unsafe {
let mut addrs: *mut ifaddrs = ptr::null_mut();
if getifaddrs(&mut addrs) != 0 {
return Err(IronError::System("getifaddrs failed".to_string()));
}
let mut current = addrs;
while !current.is_null() {
let ifa = &*current;
let name = if !ifa.ifa_name.is_null() {
CStr::from_ptr(ifa.ifa_name).to_string_lossy().into_owned()
} else {
current = ifa.ifa_next;
continue;
};
if name == "lo0" {
current = ifa.ifa_next;
continue;
}
let entry =
seen_interfaces
.entry(name.clone())
.or_insert_with(|| NetworkInterfaceInfo {
name: name.clone(),
mac_address: None,
ipv4_addresses: Some(Vec::new()),
ipv6_addresses: Some(Vec::new()),
is_up: Some((ifa.ifa_flags as i32 & libc::IFF_UP) != 0),
is_running: Some((ifa.ifa_flags as i32 & libc::IFF_RUNNING) != 0),
speed_mbps: None,
mtu: None,
rx_bytes: 0,
tx_bytes: 0,
rx_packets: 0,
tx_packets: 0,
rx_errors: 0,
tx_errors: 0,
rx_drops: 0,
tx_drops: None,
});
if !ifa.ifa_addr.is_null() {
let sa = &*ifa.ifa_addr;
match i32::from(sa.sa_family) {
AF_INET => {
let sin = &*(ifa.ifa_addr as *const sockaddr_in);
let ip = Ipv4Addr::from(u32::from_be(sin.sin_addr.s_addr));
entry
.ipv4_addresses
.get_or_insert_with(Vec::new)
.push(ip.to_string());
}
AF_INET6 => {
let sin6 = &*(ifa.ifa_addr as *const sockaddr_in6);
let ip = Ipv6Addr::from(sin6.sin6_addr.s6_addr);
if !ip.is_loopback() {
entry
.ipv6_addresses
.get_or_insert_with(Vec::new)
.push(ip.to_string());
}
}
AF_LINK => {
if !ifa.ifa_data.is_null() {
with_link_stats.insert(name.clone());
let if_data = &*(ifa.ifa_data as *const IfData);
entry.rx_bytes = if_data.ifi_ibytes as u64;
entry.tx_bytes = if_data.ifi_obytes as u64;
entry.rx_packets = if_data.ifi_ipackets as u64;
entry.tx_packets = if_data.ifi_opackets as u64;
entry.rx_errors = if_data.ifi_ierrors as u64;
entry.tx_errors = if_data.ifi_oerrors as u64;
entry.rx_drops = if_data.ifi_iqdrops as u64;
entry.mtu = Some(if_data.ifi_mtu);
if if_data.ifi_baudrate > 0 {
entry.speed_mbps =
Some((if_data.ifi_baudrate / 1_000_000) as u32);
}
}
#[repr(C)]
struct SockaddrDl {
sdl_len: u8,
sdl_family: u8,
sdl_index: u16,
sdl_type: u8,
sdl_nlen: u8,
sdl_alen: u8,
sdl_slen: u8,
sdl_data: [u8; 46],
}
let sdl = &*(ifa.ifa_addr as *const SockaddrDl);
if sdl.sdl_alen == 6 {
let nlen = sdl.sdl_nlen as usize;
let mac = &sdl.sdl_data[nlen..nlen + 6];
entry.mac_address = Some(format!(
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
));
}
}
_ => {}
}
}
current = ifa.ifa_next;
}
freeifaddrs(addrs);
}
seen_interfaces.retain(|name, _| with_link_stats.contains(name));
interfaces.extend(seen_interfaces.into_values());
interfaces.sort_by(|a, b| a.name.cmp(&b.name));
Ok(interfaces)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_test_iface() -> NetworkInterfaceInfo {
NetworkInterfaceInfo {
name: "eth0".to_string(),
is_up: Some(true),
is_running: Some(true),
rx_bytes: 1024 * 1024 * 100,
rx_packets: 5000,
rx_errors: 2,
rx_drops: 1,
tx_bytes: 1024 * 1024 * 50,
tx_packets: 3000,
tx_errors: 1,
tx_drops: Some(0),
speed_mbps: Some(1000),
mtu: Some(1500),
mac_address: Some("aa:bb:cc:dd:ee:ff".to_string()),
ipv4_addresses: Some(vec!["192.168.1.10".to_string()]),
ipv6_addresses: Some(vec![]),
}
}
#[test]
fn test_total_bytes() {
let iface = make_test_iface();
assert_eq!(iface.total_bytes(), iface.rx_bytes + iface.tx_bytes);
}
#[test]
fn test_total_packets() {
let iface = make_test_iface();
assert_eq!(iface.total_packets(), 5000 + 3000);
}
#[test]
fn test_total_errors() {
let iface = make_test_iface();
assert_eq!(iface.total_errors(), 3);
}
#[test]
fn test_total_drops() {
let iface = make_test_iface();
assert_eq!(iface.total_drops(), Some(1));
}
#[test]
fn test_rx_mb() {
let iface = make_test_iface();
assert!((iface.rx_mb() - 100.0).abs() < 0.01);
}
#[test]
fn test_tx_mb() {
let iface = make_test_iface();
assert!((iface.tx_mb() - 50.0).abs() < 0.01);
}
#[test]
fn test_total_mb() {
let iface = make_test_iface();
assert!((iface.total_mb() - 150.0).abs() < 0.01);
}
#[test]
fn test_is_active() {
let mut iface = make_test_iface();
assert!(iface.is_active());
iface.is_up = Some(false);
assert!(!iface.is_active());
iface.is_up = Some(true);
iface.is_running = Some(false);
assert!(!iface.is_active());
iface.is_running = None;
assert!(!iface.is_active());
}
#[test]
fn operstate_unknown_is_not_reported_as_down() {
assert_eq!(parse_operstate("up\n"), Some(true));
assert_eq!(parse_operstate("down\n"), Some(false));
assert_eq!(parse_operstate("unknown\n"), None);
assert_eq!(parse_operstate("UNKNOWN"), None);
}
#[test]
fn iff_running_is_read_from_the_hex_flags_file() {
assert_eq!(parse_iff_running("0x1003\n"), Some(false));
assert_eq!(parse_iff_running("0x11043\n"), Some(true));
assert_eq!(parse_iff_running("0x1003"), Some(false));
assert_eq!(parse_iff_running("running\n"), None);
assert_eq!(parse_iff_running(""), None);
}
#[test]
fn test_zero_stats_interface() {
let iface = NetworkInterfaceInfo {
name: "lo".to_string(),
is_up: Some(true),
is_running: Some(true),
rx_bytes: 0,
rx_packets: 0,
rx_errors: 0,
rx_drops: 0,
tx_bytes: 0,
tx_packets: 0,
tx_errors: 0,
tx_drops: Some(0),
speed_mbps: None,
mtu: None,
mac_address: None,
ipv4_addresses: Some(vec![]),
ipv6_addresses: Some(vec![]),
};
assert_eq!(iface.total_bytes(), 0);
assert_eq!(iface.total_packets(), 0);
assert_eq!(iface.total_errors(), 0);
assert_eq!(iface.total_drops(), Some(0));
let uncounted = NetworkInterfaceInfo {
tx_drops: None,
..iface.clone()
};
assert_eq!(
uncounted.total_drops(),
None,
"an uncounted direction is not zero drops"
);
assert_eq!(iface.rx_mb(), 0.0);
assert_eq!(iface.tx_mb(), 0.0);
}
#[test]
fn test_bandwidth_rate_no_prev() {
let mut monitor = NetworkMonitor {
prev_stats: HashMap::new(),
last_update: std::time::Instant::now(),
};
let iface = make_test_iface();
assert_eq!(monitor.bandwidth_rate("eth0", &iface), None);
}
#[test]
fn test_bandwidth_rate_uses_the_previous_sample() {
let mut monitor = NetworkMonitor {
prev_stats: HashMap::new(),
last_update: std::time::Instant::now(),
};
let mut iface = make_test_iface();
iface.rx_bytes = 1_000;
iface.tx_bytes = 2_000;
assert_eq!(monitor.bandwidth_rate("eth0", &iface), None);
std::thread::sleep(std::time::Duration::from_millis(50));
iface.rx_bytes = 3_000;
iface.tx_bytes = 2_000;
let (rx, tx) = monitor
.bandwidth_rate("eth0", &iface)
.expect("a second sample has something to difference against");
assert!(rx > 0.0, "rx should reflect the 2000 new bytes, got {rx}");
assert_eq!(tx, 0.0, "tx counters did not move, so the rate is zero");
}
}