🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now β†’
Menu

Categories

Network Bandwidth Monitoring with Python: Track Interface Traffic in Real-Time (Free CLI Tool)

Network Bandwidth Monitoring with Python: Track Interface Traffic in Real-Time (Free CLI Tool)

Why Bandwidth Monitoring Matters

Network bandwidth is one of the most critical resources on any Linux server. Whether you are running a web application, database server, or file storage system, understanding your network traffic patterns helps you plan capacity, detect anomalies, and troubleshoot performance issues.

Linux exposes detailed network statistics through the /proc filesystem. The /proc/net/dev file contains cumulative byte and packet counters for every network interface, updated in real-time by the kernel. By reading these counters at intervals, you can calculate throughput, identify saturated links, and detect error conditions.

dargslan-bandwidth-monitor wraps this functionality in a clean Python API and CLI tool, making it easy to integrate bandwidth monitoring into your scripts, dashboards, and alerting systems.

Install dargslan-bandwidth-monitor

pip install dargslan-bandwidth-monitor

Zero dependencies. Reads directly from /proc/net/dev. Works on any Linux system with Python 3.7+.

CLI Usage

# Full bandwidth report
dargslan-bw report

# Interface statistics
dargslan-bw stats

# Real-time throughput test (default 3 seconds)
dargslan-bw speed

# 10-second speed test on specific interface
dargslan-bw speed -i eth0 -d 10

# Total traffic counters
dargslan-bw total

# Check for interface errors and drops
dargslan-bw errors

# JSON output
dargslan-bw json

Python API

from dargslan_bandwidth_monitor import BandwidthMonitor

bm = BandwidthMonitor()

# Current interface statistics
stats = bm.get_stats()
for s in stats:
    print(f"{s[\"interface\"]}: RX={s[\"rx_human\"]}, TX={s[\"tx_human\"]}")

# Measure throughput over 5 seconds
throughput = bm.measure_throughput(duration=5)
for t in throughput:
    print(f"{t[\"interface\"]}: {t[\"rx_rate_human\"]} down, {t[\"tx_rate_human\"]} up")

# Total traffic across all interfaces
total = bm.get_total_traffic()
print(f"Total traffic: {total[\"total_human\"]}")

# Check for errors
errors = bm.check_errors()
if errors:
    print(f"WARNING: {len(errors)} interface issues detected")

Understanding /proc/net/dev

The /proc/net/dev file contains one line per network interface with these fields: interface name, RX bytes, RX packets, RX errors, RX drops, RX fifo, RX frame, RX compressed, RX multicast, TX bytes, TX packets, TX errors, TX drops, TX fifo, TX collisions, TX carrier, TX compressed.

These are cumulative counters that reset only on reboot. To measure throughput, read the counters twice with a known interval between readings and calculate the difference.

Common Alert Thresholds

Set up alerts for these conditions:

  • rx_errors > 0 β€” Check network cable, NIC driver, or switch port
  • rx_dropped > 100 β€” Increase ring buffer size with ethtool
  • tx_errors > 0 β€” Check for duplex mismatch or cable issues
  • Interface saturation > 80% β€” Plan capacity upgrade

Download the Bandwidth Monitoring Cheat Sheet

Get our Network Bandwidth Monitoring Cheat Sheet β€” covering CLI tools, /proc/net/dev fields, unit conversions, and troubleshooting commands.

Related Tools

See all Python CLI tools for Linux at dargslan.com. Our networking and DevOps eBooks cover bandwidth management, traffic shaping, and network security in depth.

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.