Skip to content

emigcm98/packets-stats

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

packets-stats

A tiny Linux kernel module + Rust CLI that provides per-interface IPv4 traffic statistics in time buckets (default: 60s) with 48h retention.

The kernel exports a parse-friendly stream via:

  • /proc/packets_stats

The CLI turns that raw data into human-readable summaries (text) or machine-friendly output (CSV / JSON), supporting queries between dates.


Overview

This project consists of:

  • A Linux kernel module (via DKMS) that tracks IPv4 packets and bytes per interface, stored in rolling time buckets.
  • A Rust CLI that reads /proc/packets_stats and aggregates the data over arbitrary time ranges.

The design is intentionally simple, fast, and human-readable, while remaining machine-friendly when needed.


Features

  • Per-interface statistics
  • RX / TX split + totals
  • Bucketed history (rolling window)
    • Default bucket size: 60 seconds
    • Default retention: 48 hours
  • Query by time range:
    • --from "YYYY-MM-DD HH:MM" (or epoch seconds)
    • --to "YYYY-MM-DD HH:MM" (or epoch seconds)
  • Output formats:
    • --format text (default, human-readable)
    • --format csv
    • --format json

Dependencies

Runtime (end users)

Debian-based

  • dkms
  • linux-headers-$(uname -r) (or linux-headers-generic)
  • build-essential (to compile the module with dkms)

Arch

  • dkms
  • linux-headers (for your kernel)

Fedora/RPM

  • dkms
  • kernel-devel (for your kernel)
  • Toolchain: gcc, make

Build (packaging reproducible)

For building packages with scripts you will need:

  • docker

Scripts scripts/build-*.sh build inside Docker, you don't need any local toolchains to package.

Development (if compiling in your host)

  • gcc, make
  • Kernel headers: /lib/modules/$(uname -r)/build or linux-headers-$(uname -r)
  • rust + cargo (>= 1.79)

Components & Packaging

The project is packaged as three packages per distro:

packets-stats-dkms

  • Installs the kernel module sources under: /usr/src/packets-stats-<version>/
  • Registers and builds the module via DKMS
  • Automatically rebuilds on kernel upgrades

packets-stats-cli

  • Installs the Rust CLI as: /usr/bin/packets-stats (Debian/RPM) or /usr/bin/packets-stats (Arch too)
  • Reads /proc/packets_stats and formats the output

packets-stats (meta-package)

  • Depends on both:
    • packets-stats-dkms
    • packets-stats-cli
  • Allows installing everything with a single package (still usually installed together when using local files)

Repository layout

  • kernel/ — kernel module sources + dkms.conf
  • cli/packets-stats/ — Rust CLI
  • packaging/ — packaging templates + Docker builder scripts
    • packaging/deb/
    • packaging/arch/
    • packaging/rpm/
  • scripts/ — host entrypoints (build from Docker)
    • scripts/build-deb.sh
    • scripts/build-arch.sh
    • scripts/build-rpm.sh
  • dist/ — build output (generated)
    • dist/deb/
    • dist/arch/
    • dist/rpm/

Developer workflow

Kernel module (manual build)

cd kernel
make -C /lib/modules/$(uname -r)/build M=$PWD modules
sudo insmod packets_stats.ko
head -n 10 /proc/packets_stats
sudo rmmod packets_stats
make -C /lib/modules/$(uname -r)/build M=$PWD clean

CLI (manual run)

cargo run --manifest-path cli/packets-stats/Cargo.toml -- \
  --from "2025-12-15 14:00" \
  --to   "2025-12-15 14:10" \
  --format text

Reproducible builds (Docker builders)

All packaging is built inside Docker for reproducibility. Output is always written to dist/.

A common rule used in builders:

  • Rust builds write to an isolated CARGO_TARGET_DIR (e.g. /tmp/cargo-target) to avoid permission issues and keeping your repo clean.
  • When templates are rendered with envsubst, it is done with a whitelist of variables and with needed variables exported, to avoid accidentally replacing ${pkgdir} / $PKG / etc. with empty strings.

Debian/Ubuntu (.deb)

Build

From the repository root:

./scripts/build-deb.sh

This generates:

  • dist/deb/packets-stats-dkms_<version>_all.deb
  • dist/deb/packets-stats-cli_<version>_<arch>.deb
  • dist/deb/packets-stats_<version>_all.deb

Install

Recommended (resolves deps) — copy to /tmp to avoid _apt sandbox permission warnings:

cp dist/deb/*.deb /tmp/
sudo apt install /tmp/packets-stats*.deb

If DKMS didn’t build automatically (e.g., missing headers), install prerequisites and rebuild:

sudo apt install -y dkms build-essential linux-headers-$(uname -r)
sudo dkms install -m packets-stats -v "$(cat VERSION)" --force
sudo modprobe packets_stats

Verify

head -n 10 /proc/packets_stats
packets-stats --from "2025-12-15 14:00" --to "2025-12-15 14:10" --format text

Uninstall

sudo apt purge -y packets-stats packets-stats-cli packets-stats-dkms
sudo apt autoremove -y

Arch Linux (.pkg.tar.zst)

Build

./scripts/build-arch.sh

This generates (examples):

  • dist/arch/packets-stats-dkms-<ver>-1-any.pkg.tar.zst
  • dist/arch/packets-stats-cli-<ver>-1-x86_64.pkg.tar.zst
  • dist/arch/packets-stats-<ver>-1-any.pkg.tar.zst
  • Optionally: dist/arch/packets-stats-cli-debug-...

Install

Since these are local files (not a repo), install all three explicitly:

sudo pacman -U dist/arch/packets-stats-dkms-*.pkg.tar.zst \
             dist/arch/packets-stats-cli-*.pkg.tar.zst \
             dist/arch/packets-stats-*.pkg.tar.zst

Ensure you have DKMS and kernel headers for your kernel:

sudo pacman -S --needed dkms linux-headers

Then load the module:

sudo modprobe packets_stats

Uninstall

sudo pacman -Rns packets-stats packets-stats-cli packets-stats-dkms

RPM (Fedora/RHEL-like) (.rpm)

Build

./scripts/build-rpm.sh

This generates RPMs in:

  • dist/rpm/*.rpm

Note about versions on RPM

RPM packaging is stricter about Version:. If you use pre-release versions like 0.1.0-rc1, you typically need to map -rc1 to ~rc1 for correct ordering and to keep Version valid. The builder handles this conversion.

Install (Fedora)

sudo dnf install ./dist/rpm/*.rpm

If your distro doesn’t provide dkms by default, you may need to enable the appropriate repository or install DKMS/kernel-devel first.

Uninstall

sudo dnf remove packets-stats packets-stats-cli packets-stats-dkms

Troubleshooting

/proc/packets_stats missing

  • The kernel module is not loaded.
  • Or DKMS did not build for your running kernel yet.

Steps:

dkms status | grep packets-stats || true
sudo modprobe packets_stats
dmesg | tail -n 50

On Debian/Ubuntu, check DKMS logs:

sudo cat /var/lib/dkms/packets-stats/$(cat VERSION)/build/make.log

License

GPL-2.0-only. See License.

About

Kernel module + Rust CLI for IPv4 traffic statistics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages