Skip to content

netstat compatibility issue - #3

Open
WajeehJ wants to merge 1 commit into
open-power-sdk:masterfrom
WajeehJ:issue_netstat
Open

netstat compatibility issue#3
WajeehJ wants to merge 1 commit into
open-power-sdk:masterfrom
WajeehJ:issue_netstat

Conversation

@WajeehJ

@WajeehJ WajeehJ commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Cross-distro network statistics compatibility

Problem:
LPCPU called netstat -in, netstat -v, and netstat -s directly and unconditionally during
profiling. On modern distributions such as SLES 15, net-tools (which provides netstat) is not
installed by default, causing those collection steps to silently produce empty or error output.

Goal:
Keep netstat as the preferred tool for backwards compatibility with users who have it installed,
while automatically falling back to modern iproute2 equivalents (ss, nstat, ip) on
distributions where netstat is absent.

Changes: lpcpu/lpcpu.sh

  1. New helper functions — tool detection

Four boolean helper functions were added so each collection function can probe for tool
availability at runtime without duplicating command -v logic:

function has_netstat() { command -v netstat > /dev/null 2>&1; }
function has_ss() { command -v ss >/dev/null 2>&1; }
function has_nstat() { command -v nstat >/dev/null 2>&1; }
function has_iproute2() { command -v ip >/dev/null 2>&1 && ip -V >/dev/null 2>&1; }
2. New function — collect_active_socket_statistics()

Replaces the bare netstat -v call. Prefers netstat -v for backwards compatibility; falls back
to plain ss if netstat is not installed; writes a message to the output file if neither is
available (no exit 1).

function collect_active_socket_statistics() {
local output_file="$1"
if has_netstat; then
netstat -v > "$output_file" 2>&1
elif has_ss; then
ss > "$output_file" 2>&1
else
echo "netstat nor ss not available" > "$output_file"
fi
}
3. New function — kernel_network_protocol_statistics()

Replaces the bare netstat -s call. Prefers netstat -s; falls back to nstat -az (part of
iproute2); writes a message if neither is present.

function kernel_network_protocol_statistics() {
local output_file="$1"
if has_netstat; then
netstat -s > "$output_file" 2>&1
elif has_nstat; then
nstat -az > "$output_file" 2>&1
else
echo "netstat nor nstat not available" > "$output_file"
fi
}
4. New function — kernel_interface_table()

Replaces the bare netstat -in call. Prefers netstat -in; falls back to ip -s link piped
through a formatting script (tools/ip_to_netstat.py) that normalises the output to match the
netstat -in column layout; writes a message if neither is present.

function kernel_interface_table() {
local output_file="$1"
local formatting_script="${LPCPUDIR}/tools/ip_to_netstat.py"
local temp_file="${output_file}.tmp"
if has_netstat; then
netstat -in > "$output_file" 2>&1
elif has_iproute2; then
ip -s link > "$temp_file" 2>&1
python3 "$formatting_script" "$temp_file" > "$output_file" 2>&1
rm -f "$temp_file"
else
echo "netstat nor ip not available" > "$output_file"
fi
}
5. Call sites updated — before/after snapshot blocks

The three direct netstat calls in both the before and after snapshot sections were replaced with
calls to the new functions. Output filenames are unchanged so existing post-processing is
unaffected.

  • netstat -in > $LOGDIR/netstat-in.before 2>&1
  • netstat -v > $LOGDIR/netstat-v.before 2>&1
  • netstat -s > $LOGDIR/netstat-s.before 2>&1
  • kernel_interface_table "$LOGDIR/netstat-in.before"
  • collect_active_socket_statistics "$LOGDIR/netstat-v.before"
  • kernel_network_protocol_statistics "$LOGDIR/netstat-s.before"

(identical replacement in the "after" block)

@WajeehJ WajeehJ changed the title finished issue netstat compatibility issue Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant