Home

Search this site:
 

Horshack’s
very
own site

 

This is Bavaria, Germany!
You will find links to Perl,
bootdisks for system administration,
a lot of Linux,
Windows administration
and some personal belongings.
Configuring MRTG, and working with it

Configuring MRTG, and working with it

I use MRTG in a company environment with Debian Linux. Most things to get a view of are switched, but there are some special devices too which I can only deal with when letting some perl-programs run which deliver the numbers.

MRTG is not good for

MRTG is good for MRTG and Nagios are a good team.

This is what you will find here:


Installing

Installing debian-package is this, you will find a pretty empty configuration file.
echolon:~# apt-get install mrtg
echolon:~# cat /etc/mrtg.cfg
WorkDir: /var/www/mrtg
WriteExpires: Yes

Title[^]: Traffic Analysis for
You will find a crontab running every 5 minutes as user root:
echolon:~# cat /etc/cron.d/mrtg
0-55/5 *        * * *   root    if [ -x /usr/bin/mrtg ] && [ -r /etc/mrtg.cfg ]; then env LANG=C /usr/bin/mrtg /etc/mrtg.cfg >> /var/log/mrtg/mrtg.log 2>&1; fi

Creating a home for your scripts

I prefer to have a home-directory for mrtg and perl-programs running as user root because mrtg is running via cron as user root.
echolon:/etc# mkdir /home/mrtg
echolon:/etc# mkdir /home/mrtg/bin
echolon:/etc# chown -R root.root /home/mrtg
echolon:/etc# chmod 700 /home/mrtg
echolon:/etc# chmod 700 /home/mrtg/bin

How does MRTG gets its numbers? Running a program

There are two ways: user root is running a program, getting data by snmp.

Create a perlscript like this:

mrtg@echolon:~/bin$ chmod 700 /home/mrtg/bin/ping.pl
mrtg@echolon:~/bin$ cat /home/mrtg/bin/ping.pl
#!/usr/bin/perl
use strict;
use warnings;
use Net::Ping;
use Time::HiRes;

my $host = shift;

my $p = Net::Ping->new("icmp");
$p->hires();
my ($ret, $duration, $ip) = $p->ping($host, 5.5);
if ( $ret ) {
        printf "%.0f\n", 1000 * $duration;
} else {
        print "0\n";
}

# Value "in" for mrtg
print "0\n";
# Value "out" for mrtg
print "\n";
# A comment for mrt
print "$host\n";
$p->close();
Let it run and see the output:
echolon:/home/mrtg/bin# ./ping.pl www.google.de
62
0

www.google.de
Now, let's tell mrtg about this program! For every graph which gets processed we need a "section" in /etc/mrtg/mrtg.cfg. For ping to google it looks like this:
#
# Defaults for some ping-checks into the internet
#
MaxBytes[_]: 10000
Options[_]: gauge,growright
LegendI[_]: Ping in ms
ShortLegend[_]: ms
YLegend[_]: ms
Factor[_]: 1

# every "section" muss have a unique name
# I prefer this: [$SERVICE$_$HOST$]
Target[ping_www.amazon.de]: `perl /home/mrtg/bin/ping.pl www.amazon.de`
Title[ping_www.amazon.de]: Ping www.amazon.de
PageTop[ping_www.amazon.de]: <h1>Ping www.amazon.de</h1>
LegendO[ping_www.amazon.de]:

Target[ping_www.google.de]: `perl /home/mrtg/bin/ping.pl www.google.de`
Title[ping_www.google.de]: Ping www.google.de
PageTop[ping_www.google.de]: <h1>Ping www.google.de</h1>
LegendO[ping_www.google.de]:
If you made this let mrtg run three times, the first three times you do this with new sections you will get errormessages because the rrd-datafiles do not exist and some prerequisites are not there. Simply ignore this.
echolon:~# mrtg
Rateup WARNING: /usr/bin/rateup could not read the primary log file for ping_www.google.de
Rateup WARNING: /usr/bin/rateup The backup log file for ping_www.google.de was invalid as well
Rateup WARNING: /usr/bin/rateup Can't remove ping_www.google.de.old updating log file
Rateup WARNING: /usr/bin/rateup Can't rename ping_www.google.de.log to ping_www.google.de.old updating log file

echolon:~# mrtg
Rateup WARNING: /usr/bin/rateup Can't remove ping_www.google.de.old updating log file
echolon:~# mrtg
Now try to find the /var/www/mrtg with your web-browser, I have to navigate to http://my.server.local/mrtg and see this:


For every "section" there is an html-file which you can select. Navigate to ping_www.google.de.html and you see this:


How does MRTG gets its numbers? Getting data by SNMP

SNMP has a simple in it's name, but it's not so simple as it looks. Getting a value you know from commandline:

# getting ambient temperature from my HP-server
# which has an snmp-agent installed
VERSION=1
COMMUNITY=public
HOST=my-hp.server.local
OID_TEMPERATURE=.1.3.6.1.4.1.232.6.2.6.8.1.4.0.2
snmpwalk -v$VERSION -c$COMMUNITY $HOST $OID_TEMPERATURE

A typical case is that there is a network-switch I want to get data from. The mrtg-tool cfgmaker helps me to create the mrtg.cfg-file for such a device:

echolon:~# cfgmaker public@10.95.81.223 >/tmp/x

Now edit /tmp/x and pick out the lines you are interested in, I took this line, because H5 is the port I am interested in (this is where a large server is).

But instead of [10.95.81.223_173] I decided to take a more descriptive name and changed it to [hydra.switch_h5_server_walkuere]

### Interface 173 >> Descr: 'H5' | Name: 'H5' | Ip: '' | Eth: '00-15-60-82-e7-53' ###

Target[hydra.switch_h5_server_walkuere]: 173:public@10.95.81.223:
SetEnv[hydra.switch_h5_server_walkuere]: MRTG_INT_IP="" MRTG_INT_DESCR="H5"
MaxBytes[hydra.switch_h5_server_walkuere]: 125000000
Title[hydra.switch_h5_server_walkuere]: Traffic Analysis for 173 -- HP ProCurve Switch 4108GL
PageTop[hydra.switch_h5_server_walkuere]: <H1>hydra.switch_h5_server_walkuere</H1>
 <TABLE>
   <TR><TD>System:</TD>     <TD>HP ProCurve Switch 4108GL in </TD></TR>
   <TR><TD>Maintainer:</TD> <TD></TD></TR>
   <TR><TD>Description:</TD><TD>H5  </TD></TR>
   <TR><TD>ifType:</TD>     <TD>ethernetCsmacd (6)</TD></TR>
   <TR><TD>ifName:</TD>     <TD>H5</TD></TR>
   <TR><TD>Max Speed:</TD>  <TD>125.0 MBytes/s</TD></TR>
 </TABLE>

Trimming apache-httpd to show wide columns

Before:


After:


Do this to let your columns be wide in your apache-config:

Alias /mrtg /var/www/mrtg
<Directory "/var/www/mrtg">
    # Make columns as far as needed in Apache directory-view
    IndexOptions NameWidth=*
</Directory>

Trimming apache-httpd to show only html-files in mrtg-dir

You need this config to let Apache execute a perl-script (shown later here):
Alias /mrtg /var/www/mrtg
<Directory "/var/www/mrtg">
    # Make columns as far as needed in Apache directory-view
    IndexOptions NameWidth=*
    <Files "*.pl">
        SetHandler cgi-script
        Options ExecCGI SymLinksIfOwnerMatch
    </Files>
</Directory>
I use a little perl-script /var/www/mrtg/_index.pl which is shown on first place in a browser-listing of this directory:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my $wf = CGI->new;

print $wf->header;
my $title = "MRTG graphs for Horshack";
print $wf->start_html( -title => $title );
print $wf->h1($title);

# extract html-files from directory
opendir( MRTG, "." );
my @fn = sort
  grep { $_ =~ /\.html$/ }
  readdir(MRTG);
closedir MRTG;

# print out li-list of html-files
print "<ul>";
print join "\n",
  map { "<li><a href=\"$_\">$_</a></li>" } 
  @fn;
print "</ul>";

print $wf->end_html;


Last modified:
08.02.2008 09:41
Impressum
Wozu Witze ausdenken? Das Leben schreibt sie selbst:
Das haben wir jetzt 40 Jahre so gemacht, warum sollten wir das ändern?
Why inventing jokes? Real life does a better job:
We are doing it this way for 40 years, why should we change it?