From c4c2e7ae5b6b2e98b2af36cd3345790a733e6456 Mon Sep 17 00:00:00 2001 From: xqtc Date: Tue, 1 Oct 2024 17:11:11 +0200 Subject: [PATCH] Specs --- Cargo.lock | 206 +++++++++++++++++++++++++++ yotei-nodes/Cargo.toml | 4 + yotei-nodes/src/worker.rs | 1 + yotei-nodes/src/worker_util/specs.rs | 87 +++++++++-- 4 files changed, 286 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd48e21..3e51253 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -294,6 +294,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "clap" version = "4.5.16" @@ -421,6 +427,26 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "ethtool" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8e04a35517dc77748dc04bf38152799382d3d8f85cb07cb579bb7f4d8d3b5a" +dependencies = [ + "anyhow", + "byteorder", + "futures", + "genetlink", + "log", + "netlink-packet-core", + "netlink-packet-generic", + "netlink-packet-utils", + "netlink-proto", + "netlink-sys", + "thiserror", + "tokio", +] + [[package]] name = "fastrand" version = "2.1.1" @@ -445,6 +471,21 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.30" @@ -452,6 +493,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -460,6 +502,34 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.30" @@ -478,10 +548,31 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", +] + +[[package]] +name = "genetlink" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f890076c1faa1298bf747ce3694a8d9e0d2cc4b06fe293f12dd95742bfd079f" +dependencies = [ + "futures", + "log", + "netlink-packet-core", + "netlink-packet-generic", + "netlink-packet-utils", + "netlink-proto", + "thiserror", ] [[package]] @@ -768,6 +859,15 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -801,6 +901,82 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" +[[package]] +name = "netlink-packet-core" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72724faf704479d67b388da142b186f916188505e7e0b26719019c525882eda4" +dependencies = [ + "anyhow", + "byteorder", + "netlink-packet-utils", +] + +[[package]] +name = "netlink-packet-generic" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd7eb8ad331c84c6b8cb7f685b448133e5ad82e1ffd5acafac374af4a5a308b" +dependencies = [ + "anyhow", + "byteorder", + "netlink-packet-core", + "netlink-packet-utils", +] + +[[package]] +name = "netlink-packet-utils" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ede8a08c71ad5a95cdd0e4e52facd37190977039a4704eb82a283f713747d34" +dependencies = [ + "anyhow", + "byteorder", + "paste", + "thiserror", +] + +[[package]] +name = "netlink-proto" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b33524dc0968bfad349684447bfce6db937a9ac3332a1fe60c0c5a5ce63f21" +dependencies = [ + "bytes", + "futures", + "log", + "netlink-packet-core", + "netlink-sys", + "thiserror", + "tokio", +] + +[[package]] +name = "netlink-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "416060d346fbaf1f23f9512963e3e878f1a78e707cb699ba9215761754244307" +dependencies = [ + "bytes", + "futures", + "libc", + "log", + "tokio", +] + +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", + "memoffset", +] + [[package]] name = "ntapi" version = "0.4.1" @@ -831,6 +1007,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "percent-encoding" version = "2.3.1" @@ -1260,6 +1442,26 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "thiserror" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.3.36" @@ -1802,9 +2004,13 @@ dependencies = [ "bson", "clap", "colorful", + "ethtool", + "futures", "lazy_static", "log", "lz4_flex", + "netlink-packet-generic", + "nix", "pretty_env_logger", "prost", "prost-types", diff --git a/yotei-nodes/Cargo.toml b/yotei-nodes/Cargo.toml index 3b4ad46..f354b33 100644 --- a/yotei-nodes/Cargo.toml +++ b/yotei-nodes/Cargo.toml @@ -12,6 +12,7 @@ edition = "2021" pretty_env_logger = "0.5" tokio = { version = "1.40", features = ["macros", "rt-multi-thread"]} serde = { version = "1.0", features = ["derive"] } +nix = { version = "0.29", features = ["net"] } serde_json = "1.0" anyhow = "1.0" lz4_flex = "0.11.3" @@ -28,6 +29,9 @@ prost = "0.13.2" prost-types = "0.13.2" colorful = "0.3.2" sysinfo = "0.31.4" +ethtool = "0.2.5" +futures = "0.3.30" +netlink-packet-generic = "0.3.3" [build-dependencies] tonic-build = "0.12.2" diff --git a/yotei-nodes/src/worker.rs b/yotei-nodes/src/worker.rs index 844fce8..086860d 100644 --- a/yotei-nodes/src/worker.rs +++ b/yotei-nodes/src/worker.rs @@ -57,6 +57,7 @@ async fn main() -> Result<(), Box> { std::env::set_var("RUST_LOG", CONFIG.log.level.clone()); pretty_env_logger::init(); greet(); + worker_util::specs::network().await; let server_addr = "http://[::1]:50051"; let addr: SocketAddr = "[::1]:50053".parse()?; // Start server here diff --git a/yotei-nodes/src/worker_util/specs.rs b/yotei-nodes/src/worker_util/specs.rs index ece4fa0..a924c54 100644 --- a/yotei-nodes/src/worker_util/specs.rs +++ b/yotei-nodes/src/worker_util/specs.rs @@ -1,24 +1,87 @@ +use std::{collections::HashMap, fmt}; + use crate::util::state::NodeSpecs; +use ethtool::{self, EthtoolAttr, EthtoolMessage}; + pub fn collect_specs() -> NodeSpecs { let mut sys = sysinfo::System::new_all(); NodeSpecs { threads: sys.cpus().len().try_into().unwrap(), mem: sys.total_memory(), - net_speed: 13.0, + net_speed: 13.0, } } -fn network() { - let networks = sysinfo::Networks::new_with_refreshed_list(); - for (interface_name, data) in &networks { - println!( - "{interface_name}: {} B (down) / {} B (up)", - data.total_received(), - data.total_transmitted(), - ); - // If you want the amount of data received/transmitted since last call - // to `Networks::refresh`, use `received`/`transmitted`. - } +use futures::stream::TryStreamExt; +use log::{debug, error, info}; +pub async fn network() { + let mut interfaces = vec![]; + match nix::ifaddrs::getifaddrs() { + Ok(ifaddrs) => { + debug!("{:?}", ifaddrs); + for ifaddr in ifaddrs { + if let Some(interface_name) = Some(ifaddr.interface_name) { + if let Some(flags) = Some(ifaddr.flags) { + let is_up = flags.contains(nix::net::if_::InterfaceFlags::IFF_UP); + let is_running = flags.contains(nix::net::if_::InterfaceFlags::IFF_RUNNING); + + if is_up && is_running { + interfaces.push(interface_name); + } + } + } + } + } + Err(err) => { + error!("Failed to get network interfaces: {}", err); + } + } + debug!("{:?}", interfaces); + let mut interface_speeds: Vec<(String, i64)> = vec![]; + let mut visited_interfaces = vec![]; + for interface in interfaces { + let path = format!("{}{}{}", "/sys/class/net/", interface, "/speed"); + debug!("{}", &path); + if !path.contains("lo") { + let speed = std::fs::read_to_string(path).unwrap(); + debug!("SPEED: {}", &speed); + interface_speeds.push((interface, speed.trim_end().parse::().unwrap())); + } + } + // let res = get_link_mode(Some("enp42s0")).await; + debug!("{:#?}", interface_speeds); } +// async fn get_link_mode( +// iface_name: Option<&str>, +// ) -> Vec> { +// let (connection, mut handle, _) = ethtool::new_connection().unwrap(); +// tokio::spawn(connection); +// +// let mut link_mode_handle = handle.link_mode().get(iface_name).execute().await; +// +// let mut msgs = Vec::new(); +// while let Some(msg) = link_mode_handle.try_next().await.unwrap() { +// msgs.push(msg); +// } +// assert!(!msgs.is_empty()); +// let mut lm = None; +// for msg in &msgs { +// let payl = msg.payload.clone(); +// for linkmode in payl.nlas { +// match &linkmode { +// ethtool::EthtoolAttr::LinkMode(ethtool::EthtoolLinkModeAttr::Speed(_)) => { +// lm = Some(linkmode) +// } +// _ => continue, +// }; +// } +// } +// // if lm.is_some() { +// debug!("{:#?}", lm.clone().unwrap()); +// let speed = lm.unwrap(); +// speed. +// // } +// msgs +// }