Saturday, July 4, 2015

Getting MRTG to work in ArchLinux

It's been a long time since I ran MRTG, the Multi Router Traffic Grapher.  So, I figured it was time to run it again.  Using Arch Linux, it's pretty simple to get it installed and configured.  However, the current model does not come with a mrtg.service file.

In fact, no one has written one!  It's supposed to be ran by hand.  This kind of dulls things, since a server ought to be able to be brought back online and it should "just work" with little human intervention.  So, I'm a bit of a perfectionist when it comes to making things work just right, out of the box.

Creating service files for systemd is not very difficult.  I just used another .service file as a template to work from.

The only other type of service file that I could find was a CentOS oriented control file, and by the looks of it, someone didn't realize that you could run MRTG in daemon mode and not have to worry about rerunning the process every so often.  Still, it was helpful to remember that daemonizing isn't always the necessary way.


[Unit]

Description=Multi Router Traffic Grapher

After=network.target

[Service]

PIDFile=/run/mrtg/mrtg.pid

User=mrtg

Group=mrtg

ExecStart=/usr/lib/systemd/scripts/mrtg.sh

ExecReload=/usr/bin/kill -USR2 $MAINPID

KillSignal=SIGQUIT

KillMode=mixed

[Install]

WantedBy=multi-user.target

You'll notice that I generated a shell script to handle actually starting the daemon.  I was getting errors while trying to load an environment variable into it, so I set it to run its own process:

#!/bin/sh
LANG=C /usr/bin/mrtg --pid-file=/run/mrtg/mrtg.pid --user=mrtg --group=mrtg --daemon /srv/http/mrtg/html/mrtg.cfg

Create /run/mrtg:

# mkdir /run/mrtg

Set the appropriate permissions:

# chown mrtg.mrtg /run/mrtg

When all is said and done, MRTG will run in the background, unattended, and produce beautiful graphs in Arch Linux.