mirror of
https://codeberg.org/icewind/palantir.git
synced 2026-06-03 18:24:08 +02:00
sync network stats
This commit is contained in:
parent
f0a4482ec5
commit
f076776b50
2 changed files with 40 additions and 12 deletions
|
|
@ -11,8 +11,7 @@ use std::collections::HashSet;
|
|||
use std::fmt::Write;
|
||||
|
||||
pub async fn get_metrics() -> Result<String> {
|
||||
let (network, disks, disk_usage) = try_join! {
|
||||
network_stats(),
|
||||
let (disks, disk_usage) = try_join! {
|
||||
disk_stats(),
|
||||
disk_usage(),
|
||||
}?;
|
||||
|
|
@ -21,6 +20,7 @@ pub async fn get_metrics() -> Result<String> {
|
|||
let memory = memory()?;
|
||||
let temperatures = temperatures()?;
|
||||
let pools = pools();
|
||||
let network = network_stats()?;
|
||||
pin_mut!(network);
|
||||
pin_mut!(disks);
|
||||
pin_mut!(disk_usage);
|
||||
|
|
@ -58,7 +58,7 @@ pub async fn get_metrics() -> Result<String> {
|
|||
)
|
||||
.ok();
|
||||
}
|
||||
while let Some(network) = network.next().await {
|
||||
while let Some(network) = network.next() {
|
||||
let network: IOStats = network;
|
||||
if network.bytes_received > 0 || network.bytes_sent > 0 {
|
||||
writeln!(
|
||||
|
|
|
|||
|
|
@ -127,15 +127,43 @@ pub fn cpu_time() -> Result<u64> {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn network_stats() -> Result<impl Stream<Item = IOStats>> {
|
||||
let networks = heim::net::io_counters().await?;
|
||||
Ok(networks
|
||||
.filter_map(|network| future::ready(network.ok()))
|
||||
.filter(|network| future::ready(network.interface().starts_with("enp")))
|
||||
.map(|network| IOStats {
|
||||
interface: network.interface().into(),
|
||||
bytes_sent: network.bytes_sent().get::<information::byte>(),
|
||||
bytes_received: network.bytes_recv().get::<information::byte>(),
|
||||
pub fn network_stats() -> Result<impl Iterator<Item = IOStats>> {
|
||||
let stat = BufReader::new(File::open("/proc/net/dev")?);
|
||||
Ok(stat
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.filter(|line: &String| line.starts_with("enp"))
|
||||
.filter_map(|line: String| {
|
||||
let mut parts = line.split_ascii_whitespace();
|
||||
if let (
|
||||
Some(interface),
|
||||
Some(bytes_received),
|
||||
_err,
|
||||
_drop,
|
||||
_fifo,
|
||||
_frame,
|
||||
_compressed,
|
||||
_multicast,
|
||||
Some(bytes_sent),
|
||||
) = (
|
||||
parts.next(),
|
||||
parts.next(),
|
||||
parts.next(),
|
||||
parts.next(),
|
||||
parts.next(),
|
||||
parts.next(),
|
||||
parts.next(),
|
||||
parts.next(),
|
||||
parts.next(),
|
||||
) {
|
||||
Some(IOStats {
|
||||
interface: interface.trim_end_matches(':').into(),
|
||||
bytes_sent: bytes_sent.parse().ok()?,
|
||||
bytes_received: bytes_received.parse().ok()?,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue