Add network interface collection and display
Some checks failed
Build and Release / build-and-release (push) Failing after 1m32s
Some checks failed
Build and Release / build-and-release (push) Failing after 1m32s
Extend NixOS collector to gather network interfaces using ip command JSON output. Display all interfaces with IPv4 and IPv6 addresses in Network section above CPU metrics. Filters out loopback and link-local addresses. Version bump to 0.1.161
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use async_trait::async_trait;
|
||||
use cm_dashboard_shared::AgentData;
|
||||
use cm_dashboard_shared::{AgentData, NetworkInterfaceData};
|
||||
use std::fs;
|
||||
use std::process::Command;
|
||||
use tracing::debug;
|
||||
@@ -32,6 +32,9 @@ impl NixOSCollector {
|
||||
// Set NixOS build/generation information
|
||||
agent_data.build_version = self.get_nixos_generation().await;
|
||||
|
||||
// Collect network interfaces
|
||||
agent_data.system.network.interfaces = self.get_network_interfaces().await;
|
||||
|
||||
// Set current timestamp
|
||||
agent_data.timestamp = chrono::Utc::now().timestamp() as u64;
|
||||
|
||||
@@ -101,6 +104,72 @@ impl NixOSCollector {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get network interfaces and their IP addresses
|
||||
async fn get_network_interfaces(&self) -> Vec<NetworkInterfaceData> {
|
||||
let mut interfaces = Vec::new();
|
||||
|
||||
// Use ip command with JSON output for easier parsing
|
||||
match Command::new("ip").args(["-j", "addr"]).output() {
|
||||
Ok(output) if output.status.success() => {
|
||||
let json_str = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
// Parse JSON output
|
||||
if let Ok(json_data) = serde_json::from_str::<serde_json::Value>(&json_str) {
|
||||
if let Some(ifaces) = json_data.as_array() {
|
||||
for iface in ifaces {
|
||||
let name = iface["ifname"].as_str().unwrap_or("").to_string();
|
||||
|
||||
// Skip loopback and empty names
|
||||
if name.is_empty() || name == "lo" {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut ipv4_addresses = Vec::new();
|
||||
let mut ipv6_addresses = Vec::new();
|
||||
|
||||
// Extract IP addresses
|
||||
if let Some(addr_info) = iface["addr_info"].as_array() {
|
||||
for addr in addr_info {
|
||||
if let Some(family) = addr["family"].as_str() {
|
||||
if let Some(local) = addr["local"].as_str() {
|
||||
match family {
|
||||
"inet" => ipv4_addresses.push(local.to_string()),
|
||||
"inet6" => {
|
||||
// Skip link-local IPv6 addresses (fe80::)
|
||||
if !local.starts_with("fe80:") {
|
||||
ipv6_addresses.push(local.to_string());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only add interfaces that have at least one IP address
|
||||
if !ipv4_addresses.is_empty() || !ipv6_addresses.is_empty() {
|
||||
interfaces.push(NetworkInterfaceData {
|
||||
name,
|
||||
ipv4_addresses,
|
||||
ipv6_addresses,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Failed to execute ip command: {}", e);
|
||||
}
|
||||
Ok(output) => {
|
||||
debug!("ip command failed with status: {}", output.status);
|
||||
}
|
||||
}
|
||||
|
||||
interfaces
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
||||
Reference in New Issue
Block a user