Idea: Extended MRTG format

Every tech geek has his/her preferred tools and one of mine is without doubt Tobi Oetiker’s MRTG (Multi Router Traffic Grapher), which I’ve used to make pretty trend lines of much more than routers or traffic.

MRTG is a Perl program that grabs some measurement values (typically via SNMP) and plots them as time series. It creates ‘daily’, ‘weekly’, ‘monthly’ and ‘yearly’ graphs and the HTML page that shows the graphs and some min/max/average statistics to accompany the graphs. You’ve probably seen the typical MRTG output images before:

Example of MRTG

But MRTG can do more than just SNMP. In fact MRTG can plot any trend, as long as it gets its input in the form of 4 text lines:

[I value]
[O Value]
[uptime]
[server name]

The I value becomes the green bar graph, the O value becomes the blue line, and the rest is only used to generate the following line in the generated HTML pages:

The statistics were last updated Tuesday, 13 October 2015 at 10:40, at which time ‘[server name]’ had been up for [uptime].

I started using it with some basic local scripts that I created to give their output in this 4-line format:

Target[disk_space]: `/path/mrtg_diskspace.sh /dev/sda1`

But then I also discovered that I could use URLs as data input:

Target[nb_clients]: `curl -s "http://example.com/mrtg.php?count=nb_clients"`

And I developed mrtg_remote_sensor: a library that allow me to easily monitor CPU/RAM (which I could also do through SNMP, but this is easier), and e.g. size of a folder, number of files in a folder, ping time to a LAN or Internet server. You just call the right URL and you get back 4 lines that MRTG knows how to process.

I like MRTG a lot, but writing the config files can be tricky. The syntax is quite unforgiving. That’s why I added a config mode to the URLs: when I add &config=1 to the URL I get not only the 4 lines with the values, but also the whole config block.
21122
22677
4 days 18:27
server
#### MRTG CONFIG ping_google ####
Target[ping_google]: `curl -s "http://server/mrtg_remote_sensor/index.php?key=pingtime&param=www.google.com"`
Title[ping_google]: ping time to google.com
PageTop[ping_google]: <h1>ping time to google.com</h1>
LegendI[ping_google]: MIN ping time to google.com
LegendO[ping_google]: MAX ping time to google.com
YLegend[ping_google]: sec
PNGTitle[ping_google]: ping_google
ShortLegend[ping_google]: sec
Options[ping_google]: growright,nobanner,gauge,nopercent
MaxBytes[ping_google]: 1000000000
kMG[ping_google]: u,m,,k,M,G


Now I just need to copy/paste all lines after the #### into my MRTG config file and it just works.

This is what I call ‘Extended MRTG format‘. Happy graphing!

Leave a Reply

Your email address will not be published. Required fields are marked *