chore: Add logging for ss/netstat command output

This commit is contained in:
Marco Cadetg (aider)
2025-05-10 18:25:28 +02:00
parent 7151a3783a
commit dd30e159bf
2 changed files with 58 additions and 11 deletions

View File

@@ -77,10 +77,20 @@ impl NetworkMonitor {
/// Get connections from ss command
fn get_connections_from_ss(&self, connections: &mut Vec<Connection>) -> 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<Connection>) -> 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(())
}

View File

@@ -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)