| Title: How to account systemd services bandwidth usage on NixOS
Author: Solène
Date: 20 July 2022
Tags: nixos bandwidth monitoring
Description: This article shows how to easily record the bandwidth
usage of specific systemd services on NixOS, but this can be applied to
Linux in general
# Introduction
Did you ever wonder how many bytes a system service is daily receiving
from the network? Thanks to systemd, we can easily account this.
This guide targets NixOS, but the idea could be applied on any Linux
system using systemd.
|
|
In this article, we will focus on the nix-daemon service.
# Setup
We will enable the attribute IPAccounting on the systemd service
nix-daemon, this will make systemd to account bytes and packets that
received and sent by the service. However, when the service is
stopped, the counters are reset to zero and the information logged into
the systemd journal.
In order to efficiently gather the network information over time into a
database, we will run a script just before the service stops using the
preStop service hook.
The script checks the existence of a sqlite database
/var/lib/service-accounting/nix-daemon.sqlite, creates it if required,
and then inserts the received bytes information of the nix-daemon
service about to stop. The script uses the service attribute
InvocationID and the current day to ensure that a tuple won't be
recorded more than once, because if we restart the service multiple
times a day, we need to distinguish all the nix-daemon instances.
Here is the code snippet to add to your `/etc/nixos/configuration.nix`
file before running `nixos-rebuild test` to apply the changes.
```nix
systemd.services.nix-daemon = {
serviceConfig.IPAccounting = "true";
path = with pkgs; [ sqlite busybox systemd ];
preStop = ''
#!/bin/sh
SERVICE="nix-daemon"
DEST="/var/lib/service-accounting"
DATABASE="$DEST/$SERVICE.sqlite"
mkdir -p "$DEST"
# check if database exists
if ! dd if="$DATABASE" count=15 bs=1 2>/dev/null | grep -Ea "^SQLite format.[0-9]$" >/dev/null
then
cat < |