MRTG without SNMP: graphs from your own scripts
Summary
From 2007 on, bk99.de showed MRTG graphs of DSL, LAN, Wi-Fi, VPN, temperatures, memory and load of my machines, produced not via SNMP but by small shell scripts. I never wrote the promised guide; instead a reader shortened my script in the comments to a fraction of the program calls. This post catches up on both, explains how external scripts work and shows what has taken MRTG’s place today.
What MRTG is
MRTG, the Multi Router Traffic Grapher, was created by Tobias Oetiker in the mid-1990s. According to Oetiker’s LISA 1998 paper, it all started in 1994 at De Montfort University: more than a thousand machines shared one slow internet link, and people wanted to see how busy it was. MRTG queried the router’s byte counters via SNMP every five minutes and drew graphs for day, week, month and year. Its typical green and blue graphs could be seen in many data centres for years.
My graphs from 2007
mrtg.bk99.de showed graphs for ADSL, LAN, Wi-Fi and the VPN tunnel home, for the temperatures of the hard disks and for memory, swap, load, processes, uptime and open TCP connections of the server. I did not need SNMP for that: instead of a router, MRTG can also query any program. The program only has to print four lines, two numbers for “in” and “out”, an uptime and a name.
# mrtg.hddtemp.cfg
Target[hddtemp]: `/etc/mrtg/scripts/bash.sh hddtemp`
Title[hddtemp]: HDD temp for Hödur
PageTop[hddtemp]: Temperatur
YLegend[hddtemp]: Temperatur
ShortLegend[hddtemp]:
LegendI[hddtemp]: /dev/sda
LegendO[hddtemp]: /dev/sdbThe backticks around the command matter. For the hard disks, “in” meant the temperature of /dev/sda and “out” that of /dev/sdb; MRTG does not care what the two numbers mean.
The guide that never came
The blog said “a detailed guide will follow here soon”. In early 2008 the first request came in the comments, I promised the scripts for the same week, and in April the next request followed. In September 2008 someone else took matters into their own hands and wrote a comment headed “Wurstelei” (roughly “fiddling about”) that was better than anything I had planned.
My script fetched every value with a chain of cat, grep, sed and cut. The comment showed that awk does it all at once:
# vorher: vier Programme für einen Wert
INDATA=`cat /proc/loadavg | cut -d " " -f2 | sed "s/\.//g" | sed "s/^0*//g"`
# nachher: eines
awk '{print $2*100"\n"$3*100}' /proc/loadavgThe suggestion combined all measurement types in one script using case. Here is a lightly cleaned-up version: the network interface is matched by its exact name so that eth0 does not also match veth0, and the script sets LC_ALL=C. That is no detail: when testing on a Debian system with a German locale, mawk returned 0 instead of 15 for the load because it expected a comma as the decimal separator in “0.15”.
#!/bin/sh
# mrtg_check.sh TYP [PARAMETER] - gibt vier Zeilen für MRTG aus
# Punkt als Dezimaltrenner, sonst liest awk unter deutscher Locale 0.15 als 0
export LC_ALL=C
TYPE=$1
PARAM=$2
[ -z "$TYPE" ] && { echo 'Kein TYP angegeben.'; exit 1; }
case $TYPE in
load) # Last der letzten 5 und 15 Minuten, mal 100
awk '{print $2*100"\n"$3*100}' /proc/loadavg ;;
processes) # gesamte und laufende Prozesse
awk -F'[/ ]' '{print $(NF-1)"\n"$(NF-2)}' /proc/loadavg ;;
network) # empfangene und gesendete Bytes einer Schnittstelle
[ -z "$PARAM" ] && { echo 'Keine Schnittstelle angegeben.'; exit 1; }
awk -F'[: ]+' -v ifc="$PARAM" '$2 == ifc {print $3"\n"$11}' /proc/net/dev ;;
*)
echo "Unbekannter TYP: $TYPE"; exit 1 ;;
esac
uptime | awk -F, '{print $1","$2}'
echo "$TYPE"My reply back then: I just wanted to make graphs, but respect, very nicely shortened. Eighteen years later I admit that the short version is also the better guide.
What has changed since
MRTG still exists; the latest version 2.17.10 dates from 2022. As early as 1998 Oetiker described RRDtool as the core of the next generation; Cacti, Munin and many others later built on it. In new installations a different model has prevailed: Prometheus scrapes targets, the Node Exporter delivers hundreds of metrics of a Linux machine out of the box, and Grafana draws the graphs.
The idea of my scripts lives on there. The Node Exporter’s textfile collector reads files that any script writes regularly, just as MRTG used to read the four lines. Hard disk temperatures today usually come from smartctl or the kernel driver drivetemp. And a comment that shortens a script from seven program calls to one is still the nicest feedback you can get.
Numbers at a glance
- MRTG grew out of a 1994 problem at De Montfort University and polled counters every five minutes.
- External scripts for MRTG print four lines: two numbers, an uptime and a name.
- The 2008 comment replaced up to seven program calls with a single awk call.
- The latest MRTG version 2.17.10 was released in January 2022.
References
- bk99.de 2007: MRTG Statistiken with comments (Web Archive, German)
- Tobias Oetiker: MRTG, The Multi Router Traffic Grapher (LISA 1998)
- MRTG project page
- Prometheus Node Exporter (with textfile collector)
Remarks
- This post was originally published on 3 February 2007 in my old Plone blog and last edited in 2008. In 2026 I retrieved it from the Web Archive together with the reader comments and revised it; I leave out the commenters’ names and addresses. Commands and comments in the code are shown in German as in the original.
- My own script bash.sh has not survived in the Web Archive. The graphs on mrtg.bk99.de no longer exist.
Original post in the Web Archive
Links to the original source and the Web Archive open in a new tab.