Bandwidth Monitor Server Scripts

December 20, 2025 · View on GitHub

Shell scripts for bandwidth monitoring on AsusWRT-Merlin routers.

Files

FilePurpose
bwmon.shCore monitoring script - uses iptables to count traffic
S80bwmonInit script - manages cron jobs for scheduled updates
install.shInstaller - sets up dependencies and copies files
uninstall.shClean removal of all installed files
sample-lighttpd-index.htmlSample landing page for lighttpd

Requirements

  • AsusWRT-Merlin router
  • Entware package manager
  • lighttpd web server (installed automatically)
  • bc calculator (installed automatically)

How It Works

Data Collection

  1. iptables chain (RRDIPT) counts bytes per IP address
  2. Cron job runs every 30 minutes to collect data
  3. ARP table (/proc/net/arp) identifies connected devices
  4. User resolution maps MAC/IP to usernames via:
    • /etc/hosts.dnsmasq
    • /etc/dnsmasq/hosts/hosts
    • /etc/hosts
    • NVRAM custom_clientlist

Data Flow

Router Traffic

      ▼ (iptables RRDIPT chain)
bwmon.sh update

      ▼ (writes)
usage.db ──► lighttpd ──► React Frontend

Commands

bwmon.sh

bwmon.sh setup <path>    # Create iptables rules
bwmon.sh update <path>   # Collect data and publish
bwmon.sh publish <path>  # Publish without collecting

S80bwmon (Service)

/opt/etc/init.d/S80bwmon start    # Start cron jobs
/opt/etc/init.d/S80bwmon stop     # Stop cron jobs
/opt/etc/init.d/S80bwmon restart  # Restart service
/opt/etc/init.d/S80bwmon check    # List active cron jobs

Cron Schedule

JobFrequencyPurpose
bwmon_setupEvery 1 minuteEnsure iptables rules exist
bwmon_updateEvery 30 minutesCollect and publish data

Data Format

Input Database (bwmonUsage.db)

YYYY-MM,MAC,IP,USAGE_IN,USAGE_OUT,CREATE_TIME,UPDATE_TIME

Output Database (usage.db)

Served to the React frontend:

YYYY-MM,IP,MAC,USER,USAGE_IN,USAGE_OUT,CREATE_TIME,UPDATE_TIME
  • Usage values are in kilobytes
  • Times are Unix timestamps

Development Constraints

  • POSIX shell only - No bash-isms (runs on router's busybox shell)
  • Use bc for math - Shell has no built-in arithmetic for large numbers
  • File locking - Uses /tmp/bwmon.lock to prevent concurrent updates
  • Flow Cache - Disabled automatically for accurate packet counting

Installation Paths

SourceDestination
bwmon.sh/opt/share/bwmon/
S80bwmon/opt/etc/init.d/
react/build/*/opt/share/www/bwmon/
sample-lighttpd-index.html/opt/share/www/

Logs

Log file: /opt/var/log/bwmon.log


For installation instructions, see the main README.md.