[mrtg] Re: inserting log in database

Paul Chvostek paul+mrtg at it.ca
Wed Oct 30 06:18:06 MET 2002




On Tue, Oct 29, 2002 at 09:02:05PM +0100, Christian Rasmussen wrote:
> 
> Where do you get the data in "%.0f,%.0f,%.0f,%.0f,%.0f,%.0f" from?

That's merely formatting for the numbers supplied in the printf line in
the awk script.  "%.0f" means "a floating point number with zero decimal
places".  Most awk implementations will also let you use a "%i" to
indicate an integer, but that's doesn't work everywhere, and %f does.

> I understand that I can do this via the source, but Im not very experienced
> with perl.. so I have a hard time locating the correct lines in the code..

This isn't Perl.  It's awk, an interpreted language that predates Perl, and
is still supported in pretty much every flavour of Unix around.

> But if someone could tell me where to look or what function/variable name or
> something to look for, it would be really helpful!

Try reading the man pages for awk.  That is, from a unix shell, type
"man awk" and read the results.  If you're coming from an OS that's
missing man pages, http://www.freebsd.org/cgi/man.cgi?query=awk has what
you're missing.  (GAWK is the GNU version of AWK, btw.)

p


> > Check recent list archives for table definitions I'm using.  With them,
> > along with simple shell script stuff like:
> > 
> >  #!/bin/sh
> >  mrtgspool=/var/spool/mrtg
> >  mysql=/usr/local/bin/mysql
> >  sqluser="myuser"
> >  sqlpass="mypw"
> >  sqldb="bandwidth"
> >  sqlhost="dbhost"
> >  q="SELECT date FROM mrtglog WHERE item=$1 ORDER BY -date LIMIT 1;"
> >  oldend="`$mysql -h$sqlhost -u$sqluser -p$sqlpass -e\"$q\" $sqldb 2>/dev/null | grep '^[0-9]'`"
> >  if [ -z "$oldend" ]; then
> >    oldend=0
> >  fi
> >  fmt='INSERT INTO mrtglog (item,date,avgin,avgout,peakin,peakout) VALUES (%.0f,%.0f,%.0f,%.0f,%.0f,%.0f);\n'
> >  if echo "$oldend" | grep -q '^[0-9][0-9]*$' && test -s $mrtgspool/$2.log ; then
> >    awk -v fmt="$fmt" -v oldend="$oldend" -v itemid="$1" 'BEGIN{ getline; }
> >      $1 <= oldend { nextfile; }
> >      $4 || $5 { printf(fmt, itemid, $1, 8*$2, 8*$3, 8*$4, 8*$5); }
> >    ' $mrtgspool/$2.log | sort -dn >> $tmpfile
> >  fi
> >  if [ -f $tmpfile ]; then
> >    $mysql -h$sqlhost -u$sqluser -p$sqlpass $sqldb < $tmpfile
> >  fi
> > 
> > You could probably do it a little more cleanly in Perl, but I'm a shell
> > weenie at heart.
> > 
> > Run this once an hour, and you can stick with stock MRTG code.  Verify
> > the usage of `sort` for your OS.  I use a slightly larger wrapper that
> > pulls the $1 and $2 pieces from another table, but you get the idea.
> > I've written PHP scripts which build PNG files on-the-fly from the data.

-- 
  Paul Chvostek                                             <paul at it.ca>
  Operations / Abuse / Whatever                          +1 416 598-0000
  it.canada - hosting and development                  http://www.it.ca/

--
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