From dd30e159bfe431896a6429ab58107e20502c0a88 Mon Sep 17 00:00:00 2001 From: "Marco Cadetg (aider)" Date: Sat, 10 May 2025 18:25:28 +0200 Subject: [PATCH] chore: Add logging for ss/netstat command output --- src/network/linux.rs | 55 +++++++++++++++++++++++++++++++++++++------- src/network/mod.rs | 14 ++++++++--- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/network/linux.rs b/src/network/linux.rs index 324ee0b..155fce9 100644 --- a/src/network/linux.rs +++ b/src/network/linux.rs @@ -77,10 +77,20 @@ impl NetworkMonitor { /// Get connections from ss command fn get_connections_from_ss(&self, connections: &mut Vec) -> Result<()> { - let output = Command::new("ss").args(["-tupn"]).output()?; + debug!("Executing 'ss -tupn' to get TCP/UDP connections."); + let cmd_output = Command::new("ss").args(["-tupn"]).output(); - if output.status.success() { - let text = String::from_utf8_lossy(&output.stdout); + match cmd_output { + Ok(output) => { + if output.status.success() { + let text = String::from_utf8_lossy(&output.stdout); + let line_count = text.lines().count(); + debug!("'ss -tupn' command successful. Output lines: {}", line_count); + if line_count < 5 && line_count > 0 { // Log short output + debug!("'ss -tupn' output (first {} lines):\n{}", line_count, text); + } else if line_count == 0 { + debug!("'ss -tupn' produced no output."); + } for line in text.lines().skip(1) { // Skip header @@ -157,17 +167,37 @@ impl NetworkMonitor { connections.push(conn); } } + } else { + let stderr_text = String::from_utf8_lossy(&output.stderr); + error!("'ss -tupn' command failed with status {}. Stderr: {}", output.status, stderr_text); + // Proceeding, as netstat might provide data or this is a transient issue. + } + } + Err(e) => { + error!("Failed to execute 'ss -tupn' command: {}", e); + return Err(e.into()); // Propagate the error to stop further processing in get_platform_connections for this call + } } - + debug!("Finished processing 'ss' output. Current connections vec size: {}", connections.len()); Ok(()) } /// Get connections from netstat command fn get_connections_from_netstat(&self, connections: &mut Vec) -> Result<()> { - let output = Command::new("netstat").args(["-tupn"]).output()?; + debug!("Executing 'netstat -tupn' as supplementary/fallback."); + let cmd_output = Command::new("netstat").args(["-tupn"]).output(); - if output.status.success() { - let text = String::from_utf8_lossy(&output.stdout); + match cmd_output { + Ok(output) => { + if output.status.success() { + let text = String::from_utf8_lossy(&output.stdout); + let line_count = text.lines().count(); + debug!("'netstat -tupn' command successful. Output lines: {}", line_count); + if line_count < 5 && line_count > 0 { // Log short output + debug!("'netstat -tupn' output (first {} lines):\n{}", line_count, text); + } else if line_count == 0 { + debug!("'netstat -tupn' produced no output."); + } for line in text.lines().skip(2) { // Skip headers @@ -230,8 +260,17 @@ impl NetworkMonitor { connections.push(conn); } } + } else { + let stderr_text = String::from_utf8_lossy(&output.stderr); + error!("'netstat -tupn' command failed with status {}. Stderr: {}", output.status, stderr_text); + } + } + Err(e) => { + error!("Failed to execute 'netstat -tupn' command: {}", e); + return Err(e.into()); + } } - + debug!("Finished processing 'netstat' output. Current connections vec size after netstat: {}", connections.len()); Ok(()) } diff --git a/src/network/mod.rs b/src/network/mod.rs index 683d7eb..d5f359f 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -420,9 +420,17 @@ impl NetworkMonitor { // Get connections from system methods (ss, netstat) let mut platform_conns_vec = Vec::new(); - log::debug!("NetworkMonitor::get_connections - Calling get_platform_connections (ss/netstat)"); - self.get_platform_connections(&mut platform_conns_vec)?; - log::debug!("NetworkMonitor::get_connections - get_platform_connections returned {} connections", platform_conns_vec.len()); + log::debug!("NetworkMonitor::get_connections - Attempting to populate platform_conns_vec via get_platform_connections."); + match self.get_platform_connections(&mut platform_conns_vec) { + Ok(_) => log::debug!("NetworkMonitor::get_connections - get_platform_connections call completed. platform_conns_vec now has {} entries.", platform_conns_vec.len()), + Err(e) => { + log::error!("NetworkMonitor::get_connections - Error from get_platform_connections: {}. platform_conns_vec might be empty or partially filled.", e); + // Continue with whatever platform_conns_vec contains. + } + } + if platform_conns_vec.is_empty() { + log::warn!("NetworkMonitor::get_connections - platform_conns_vec is empty after get_platform_connections call."); + } // Use a HashMap to merge, ensuring packet data (especially rate_history) is prioritized. // Key: String representation of (protocol, local_addr, remote_addr)