[mrtg] Re: MRTG Config

Daniel J McDonald dan.mcdonald at austinenergy.com
Fri Feb 27 14:49:05 MET 2004


On Thu, 2004-02-26 at 20:09, Jeff Cook wrote:
> I'm looking at using a database to create the MRTG config file.  I have most
> if not all of the needed info in a database used for other network
> monitoring and management.

Right.
>  It is a pain to have to update MRTG after I
> update the main database.  I have already started to figure out how to do
> this, and have it working on a small scale.

Good!  I've done something similar.
> I was wondering does any one else do something like this? 

I generate cfgmaker commands, and make copious use of templates.  My
templates and scripts are designed to integrate mrtg with big brother
and routers.cgi.  I take advantage of a lot of standards that I have set
for this to automate nicely - for example, every device is in DNS, with 
My script is:
#!/usr/bin/perl -w
#
use DBI;
use lib qw(/usr/local/mrtg-2/lib/mrtg2/);
use SNMP_util;
use Net::DNS;
open GENERATE, ">$ARGV[0]";
open MRTG, ">$ARGV[1]";
open STRUCT, ">bb-hosts-network-pages";
open HOSTS, ">bb-hosts-network";
print MRTG "Include: /var/mrtg/cfg/common.cfg\n";
print GENERATE "#!/bin/sh\n";
my $criteria = $ARGV[2];
$criteria = '.' if $criteria =~ /^$/;
my $dsn="DBI:mysql:database=mrtg";
my $username="amysqluser";
my $password="somethingWierd";
my $base = "/var/mrtg/cfg";
my (%nrefs,%refs);
my $db = DBI->connect($dsn,$username,$password);
my $routers = $db->prepare("select host, rstring, dir, iftemplate,
interfaces,
        hosttemplate, ifref, options, iffilter
        from snmp inner join classes using (classid)
        order by dir, host
        ");
$routers->execute;
while (my $rtr = $routers->fetchrow_hashref) {
        $refs{$$rtr{host}}++;
        $$rtr{'host'} =~ /^(.+?)\./;
        my $shorthost = $1;
        $shorthost =~ /^(.+?)-/;
        my $campus = $1;
        $nrefs{$$rtr{dir}}{$campus}++;
        my $cfg = join("/",$base,$$rtr{'dir'},$shorthost.".cfg");
        print MRTG "Include: $cfg\n";
        if ($$rtr{dir} !~ /server/) {
                my @bbline;
                push @bbline, "0.0.0.0";
                push @bbline, $$rtr{host};
                push @bbline, "# nodisp";
                if ($$rtr{host} !~ /ups|probe|ercot|-550/ and $$rtr{dir}
!~/firewall/) {
                        push @bbline, "ntp";
                }
                if ($$rtr{host} =~ /css/ and $$rtr{dir} =~ /firewall/) {
                        push @bbline,"noconn";
                }
                push @bbline, "NETMAP:$$rtr{dir}-$campus";
                print HOSTS join("\t", at bbline)."\n";
        }
        next unless $shorthost =~ /$criteria/;
        my @line;
        $$rtr{'rstring'} =~ s/'/\\'/g;
        $$rtr{'options'} .= '';
        push @line, "/usr/local/mrtg-2/bin/cfgmaker";
        push @line, "--if-template=$$rtr{'iftemplate'}"
                if ($$rtr{'interfaces'} eq 'interfaces')
                        and ($$rtr{'iftemplate'} =~ /\w+/);
        push @line, "--ifref=$$rtr{'ifref'}"
                if defined($$rtr{'ifref'});
        push @line, '--'.$$rtr{'interfaces'}
                if defined($$rtr{'interfaces'});
        push @line, "--host-template=$$rtr{'hosttemplate'}"
                if defined($$rtr{'hosttemplate'});
        my $subdir = join("/",$$rtr{'dir'},"HOSTNAME");
        push @line, "--subdir=$subdir";
        push @line, $$rtr{'iffilter'}
                if defined($$rtr{'iffilter'});
        push @line, '--output='.$cfg;
        push @line,
"'".$$rtr{'rstring'}."'".'@'.$$rtr{'host'}.$$rtr{'options'};
        print GENERATE join(" ", at line)."\n";
}
$db->disconnect;
close GENERATE;
close MRTG;
chmod 0750, $ARGV[0];
close HOSTS;
foreach my $device ( sort keys %nrefs) {
        print STRUCT "netmappage $device $device\n";
        foreach my $slot (sort keys %{ $nrefs{$device} }) {
                my $tag = join("-",$device,$slot);
                print STRUCT "netmapsubpage $tag $slot\n";
        }
}
close STRUCT;
open RMHOST, ">rmhosts.sh";
print RMHOST "#!/bin/bash\n";
print RMHOST 'BBHOME=/var/bb/bb19c'."\n";
open OLD, "</var/bb/bb19c/etc/bb-hosts-network";
while (my $line = <OLD>) {
        my ($host) = ($line =~ /^.+?\s+(.+?)\s/);
        if (not exists($refs{$host})) {
                print RMHOST "\$BBHOME/bin/bbrm $host\n";
        }
}
close RMHOST;
chmod 0755, "rmhosts.sh";
close OLD;


-- 
Daniel J McDonald, CCIE 2495, CNX
Austin Energy


--
Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Archive     http://www.ee.ethz.ch/~slist/mrtg
FAQ         http://faq.mrtg.org    Homepage     http://www.mrtg.org
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the mrtg mailing list