Skip to content

Commit

Permalink
Merge pull request #4 from navidys/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
navidys authored Jul 14, 2024
2 parents eb9c58e + ccfcae7 commit 2ec06db
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 2 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ description = "Rust library to retrieve system, kernel, and process metrics from

[dependencies]
env_logger = "0.11.3"
getset = "0.1.2"
log = "0.4.21"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
Expand Down
2 changes: 2 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Supported Features
* `/net/`
*`dev`

* `/sys/class/`
*`dmi/id`
Expand Down
16 changes: 16 additions & 0 deletions examples/net_dev.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use procsys::net_dev;

fn main() {
env_logger::init();

let net_devices = net_dev::collect();

// print all network devices information in json output
match serde_json::to_string_pretty(&net_devices) {
Ok(output) => println!("{}", output),
Err(err) => {
log::error!("{}", err);
std::process::exit(1);
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![doc = include_str!("../README.md")]

pub mod error;
pub mod net_dev;
pub mod sysfs;
pub mod utils;
134 changes: 134 additions & 0 deletions src/net_dev.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use serde::Serialize;

use crate::utils;

/// NetDev contains a network device information parsed from /proc/net/dev
#[derive(Debug, Serialize, Clone)]
pub struct NetDev {
pub name: String,
pub rx_bytes: u64,
pub rx_packets: u64,
pub rx_errors: u64,
pub rx_dropped: u64,
pub rx_fifo: u64,
pub rx_frame: u64,
pub rx_compressed: u64,
pub rx_multicast: u64,
pub tx_bytes: u64,
pub tx_packets: u64,
pub tx_errors: u64,
pub tx_dropped: u64,
pub tx_fifo: u64,
pub tx_collisions: u64,
pub tx_carrier: u64,
pub tx_compressed: u64,
}

impl NetDev {
fn new(name: String) -> Self {
NetDev {
name,
rx_bytes: 0,
rx_packets: 0,
rx_errors: 0,
rx_dropped: 0,
rx_fifo: 0,
rx_frame: 0,
rx_compressed: 0,
rx_multicast: 0,
tx_bytes: 0,
tx_packets: 0,
tx_errors: 0,
tx_dropped: 0,
tx_fifo: 0,
tx_collisions: 0,
tx_carrier: 0,
tx_compressed: 0,
}
}
}

/// collects network device information
/// # Example
/// ```
/// use procsys::net_dev;
///
/// let net_devices = net_dev::collect();
/// let json_output = serde_json::to_string_pretty(&net_devices).unwrap();
/// println!("{}", json_output);
///
/// ```
pub fn collect() -> Vec<NetDev> {
let mut net_devices = Vec::new();

let mut line_index = 0;

for line in utils::read_file_lines("/proc/net/dev") {
line_index += 1;

if line_index <= 2 {
continue;
}

let fields: Vec<&str> = line.trim().split(' ').filter(|s| !s.is_empty()).collect();

if fields.len() != 17 {
continue;
}

let mut net_device = NetDev::new(fields[0].trim_matches(':').to_string());
net_device.rx_bytes = fields[1].parse::<u64>().unwrap_or_default();
net_device.rx_packets = fields[2].parse::<u64>().unwrap_or_default();
net_device.rx_errors = fields[3].parse::<u64>().unwrap_or_default();
net_device.rx_dropped = fields[4].parse::<u64>().unwrap_or_default();
net_device.rx_fifo = fields[5].parse::<u64>().unwrap_or_default();
net_device.rx_frame = fields[6].parse::<u64>().unwrap_or_default();
net_device.rx_compressed = fields[7].parse::<u64>().unwrap_or_default();
net_device.rx_multicast = fields[8].parse::<u64>().unwrap_or_default();
net_device.tx_bytes = fields[9].parse::<u64>().unwrap_or_default();
net_device.tx_packets = fields[10].parse::<u64>().unwrap_or_default();
net_device.tx_errors = fields[11].parse::<u64>().unwrap_or_default();
net_device.tx_dropped = fields[12].parse::<u64>().unwrap_or_default();
net_device.tx_fifo = fields[13].parse::<u64>().unwrap_or_default();
net_device.tx_collisions = fields[14].parse::<u64>().unwrap_or_default();
net_device.tx_carrier = fields[15].parse::<u64>().unwrap_or_default();
net_device.tx_compressed = fields[16].parse::<u64>().unwrap_or_default();

net_devices.push(net_device);

line_index += 1;
}

net_devices
}

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

#[test]
fn net_devices() {
let ndevices = collect();
assert!(!ndevices.is_empty());

for net_dev in ndevices {
assert!(!net_dev.name.is_empty());
assert!(net_dev.rx_bytes.ge(&0));
assert!(net_dev.rx_packets.ge(&0));
assert!(net_dev.rx_errors.ge(&0));
assert!(net_dev.rx_dropped.ge(&0));
assert!(net_dev.rx_fifo.ge(&0));
assert!(net_dev.rx_frame.ge(&0));
assert!(net_dev.rx_compressed.ge(&0));
assert!(net_dev.rx_multicast.ge(&0));
assert!(net_dev.tx_bytes.ge(&0));
assert!(net_dev.tx_packets.ge(&0));
assert!(net_dev.tx_errors.ge(&0));
assert!(net_dev.tx_dropped.ge(&0));
assert!(net_dev.tx_fifo.ge(&0));
assert!(net_dev.tx_collisions.ge(&0));
assert!(net_dev.tx_carrier.ge(&0));
assert!(net_dev.tx_compressed.ge(&0));
}
}
}
25 changes: 24 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{fs, path::Path};
use std::{
fs::{self, File},
io::{BufRead, BufReader},
path::{Path, PathBuf},
};

use walkdir::WalkDir;

Expand Down Expand Up @@ -72,3 +76,22 @@ pub fn list_dir_content(

content
}

pub fn read_file_lines(filename: &str) -> Vec<String> {
let mut result = Vec::new();

match File::open(filename) {
Ok(file) => {
let reader = BufReader::new(file);
for line_result in reader.lines() {
match line_result {
Ok(line) => result.push(line),
Err(err) => log::error!("{}", err),
}
}
}
Err(err) => log::error!("{}", MetricError::IOError(PathBuf::from(filename), err)),
}

result
}

0 comments on commit 2ec06db

Please sign in to comment.