Network Interface Statistics Script

1. Introduction

Monitoring network interface statistics is a quick way to spot potential issues like packet drops, high error counts, or abnormal throughput. This script lets you check key counters — packets, bytes, and errors — for any interface directly from a NetBeez agent.

Example Use Cases:

  • Quickly verify if an interface is transmitting/receiving data
  • Detect unusual traffic spikes
  • Identify packet error trends during troubleshooting

2. Overview of the Script

This Bash script:

  1. Accepts a network interface name as an argument (defaults to eth0 if none is provided)
  2. Validates that the interface exists
  3. Reads counters from /sys/class/net/<interface>/statistics/
  4. Outputs RX/TX packet counts, byte counts, and error counts in a clean format

Prerequisites:

  • Linux-based NetBeez network agent
  • Read access to /sys/class/net/
  • Bash shell

Expected Output:

  • Key metrics like packets sent/received, bytes sent/received, and error counts

3. Script Code

#!/usr/bin/env bash

# Default interface
INTERFACE="eth0"  # Use eth0 or replace with desired interface name

# Check if interface exists
if [ ! -d "/sys/class/net/$INTERFACE" ]; then
    echo "Interface $INTERFACE does not exist."
    exit 1
fi

# Read statistics
RX_BYTES=$(cat /sys/class/net/$INTERFACE/statistics/rx_bytes)
TX_BYTES=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes)
RX_PACKETS=$(cat /sys/class/net/$INTERFACE/statistics/rx_packets)
TX_PACKETS=$(cat /sys/class/net/$INTERFACE/statistics/tx_packets)
RX_ERRORS=$(cat /sys/class/net/$INTERFACE/statistics/rx_errors)
TX_ERRORS=$(cat /sys/class/net/$INTERFACE/statistics/tx_errors)

# Print results
echo "rx_pkts=$RX_PACKETS"
echo "tx_pkts=$TX_PACKETS"
echo "rx_bytes=$RX_BYTES"
echo "tx_bytes=$TX_BYTES"
echo "rx_err=$RX_ERRORS"
echo "tx_err=$TX_ERRORS"

4. Sample Output

rx_pkts=104523
tx_pkts=98762
rx_bytes=12876432
tx_bytes=11324567
rx_err=0tx_err=0 

5. Closing Remarks

This script provides a quick, no-frills way to inspect interface-level traffic counters from a NetBeez agent. It’s especially handy during incident response or when validating changes in real-time.

Have ideas to improve it? Share your modifications in the comments so others can benefit.