MRTG setup on Ubuntu 18.04
Why MRTG?
MRTG is an open source tool that’s seemingly been around forever. Since I’m replacing my Ubuntu 16.04 VM with an Ubuntu 18.04 VM at home, I’ll take this time to document the setup. MRTG isn’t the only monitoring tool I use at home, but it’s an easy to setup visual tool.
Installing MRTG
There are two things you’ll need:
- MRTG
- A webserver
- SNMP client
- SNMP daemon
In this example, we’ll just use Apache.
sudo apt install -y mrtg apache2 snmp snmpd
You’ll be prompted to respond on how you’d like your permissions to /etc/mrtg.cfg
to be setup, I just keep the default of ownership by root.
Setting up snmpd to be polled
The only thing that’s required to allow MRTG to poll the host is uncommenting the following line in /etc/snmp/snmpd.conf
:
rocommunity public localhost
Once you’ve done this, restart snmpd
:
sudo /etc/init.d/snmpd restart
A couple other edits I’ll typically make to /etc/snmp/snmpd.conf
will be to update the following lines in /etc/snmp/snmpd.conf
to be more descriptive since these values will show up on your MRTG pages once we get that configured:
sysLocation Sitting on the Dock of the Bay
sysContact Me <me@example.org>
You can use the defaults. As with the rocommunity
setup, restart snmpd
if you make changes.
Testing snmpd setup
Use snmpwalk
to test snmpd
setups on hosts. This is particularly useful when testing connectivity to remote hosts. Since you can constrain what users Origin Identifiers (OIDs) that a client can see.
To test walking your local snmpd
setup, run the following command:
snmpwalk -c public -v2c localhost
If you get results, it’s working!
Setting up MRTG
First, make a directory for files generated by MRTG to go. I’m going to put them in a folder in Apache’s default DocumentRoot
:
sudo mkdir -p /var/www/html/mrtg
Backup your original MRTG configuration just incase you make a mistake and need to start over:
sudo cp /etc/mrtg.cfg /etc/mrtg.cfg.orig
Now generate your MRTG configuration:
sudo cfgmaker --global "WorkDir: /var/www/html/mrtg" \
--global "Options[_]: growright,bits" \
public@localhost | grep -v "/var/www/mrtg" > /etc/mrtg.cfg
The default WorkDir
path that Ubuntu comes configured with doesn’t exist, so this is the best way to override it. The grep -v
line is to exclude the default working path, assuming you changed it, from /etc/mrtg.cfg
. This is important since it will trigger directory does not exist error.
Additionally, /etc/mrtg.cfg
will include a comment at the top with the command that was run to generate the configuration. This is helpful if you want to add other machines to your setup.
Setting up the index page
By default, MRTG will just dump all the graphs and html files for the individual system interfaces into the WorkDir
as setup in /etc/mrtg.cfg
. There is a tool called indexmaker
that makes it easy to generate an index page that will include they day view of all all OIDs that are monitored. When you click that view, it will give you the full view of the day, week, month, and year graphs of that OID.
By default indexmaker
prints to stdout, but you can specify the output file. Here’s the syntax of how built my index:
sudo indexmaker --output=/var/www/html/mrtg/index.html /etc/mrtg.cfg
If you add additional systems and routers to your MRTG configuration, you’ll need to re-run this, since the index isn’t dynamically generated.