Running MRTG cfgmaker across your entire subnet?

I realized recently that I had a bunch of newly-provisioned VMs that weren’t being monitored by MRTG (one of the tools we use to monitor network usage and other fun stats). Rather than manually run cfgmaker against all the new machines, I decided to script my way out of this.

Step 1: Build a list of hosts to monitor

For this I used an nmap ping test to generate a list of “up” hosts:

nmap -sP -oG

This creates a list of IPs that are up in a format like this:

Host: () Status: Up
Host: () Status: Up
Host: () Status: Up
Host: () Status: Up
Host: () Status: Up

Step 2: Parse out the IP address and run cfgmaker

Here’s the command I ran:

$ for i in `perl -ne 'chomp; next if $_ =~ /^#/; my @a = split(/ /); print "$a[1]\n";'`; do /usr/bin/cfgmaker --global 'LogFormat: rrdtool' --global 'WorkDir: /mrtg/data/mrtg' --global 'Options[_]: growright,bits' --global 'XSize[_]: 600' --global 'YSize[_]: 400' public@$i > /etc/mrtg/mrtg-$i ; done

This parses out the IP address from the input file and for each address, runs cfgmaker and puts the output in /etc/mrtg/mrtg-[ipaddress] .

Taking this a step further, I used gethostbyaddr to resolve the IPs to hostnames, so the MRTG files have the proper hostnames in them:

$ for i in `perl -MSocket -ne 'chomp; next if $_ =~ /^#/; my @a = split(/ /); my $ip = inet_aton($a[1]); my $host = gethostbyaddr($ip, AF_INET); print "$host\n";'`; do /usr/bin/cfgmaker --global 'LogFormat: rrdtool' --global 'WorkDir: /mrtg/data/mrtg' --global 'Options[_]: growright,bits' --global 'XSize[_]: 600' --global 'YSize[_]: 400' public@$i > /etc/mrtg/mrtg-$i ; done

Then all of the mrtg .cfg config files are in /etc/mrtg/, but who wants to put in all those cron entries to run each one? Not me. Simple fix?

cat /etc/mrtg/*.cfg > /etc/mrtg-all.cfg

Then you only have one cron entry.

2 Replies to “Running MRTG cfgmaker across your entire subnet?”

  1. Have you ever tried to get MRTG to read anything off of the Actiontec router, by chance? 🙂

    Seems that there’s a script for “misterhouse” that’s supposed to do it but I could never get it working..

    “David Norwood :: Tue, 30 Sep 2008 – added a new script and web page to monitor and graph internet traffic from an Actiontec MI424-WR used by Verizon FIOS”

  2. Well, I don’t know if the Fios Actiontec router supports SNMP. If it does I don’t know how to get into it. With my Cablevision cable modem it was pretty easy – public@ I think was all it took. But I haven’t found anything indicating that the Actiontec router is SNMP-queryable, so I don’t know how MRTG would work.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: