netstat compatibility issue - #3
Open
WajeehJ wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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.
(identical replacement in the "after" block)