crep has asked for the wisdom of the Perl Monks concerning the following question:
My below code is basically a scaled down version on MRTG (multi router traffic grapher). It grabs values by snmp, then updates a database and puts thoses values in a file I can include with php. However, these values are drastically wrong. See, I grab "ifInOctets", then snmp value for bytes that came in. But then, when I divide the delta over the time since last check, the value I get is way too high (up to 40 Megs.) That is impossible, the max for 100MBit network is 12.5 MBytes. So I thought that perhaps it actually was bits, not bytes. So I devided it by 8, right? Well, the value I get back is too small. MRTG reports about 400 bytes, I get about 70.
#!/usr/bin/perl -w my $etime=0; my $eth0in=""; my $eth0out=""; my $lo=""; my $petime=0; my $peth0in=0; my $peth0out=0; my $plo=0; my $tmp=0; my $i=0; my $n=1; #NOTE!!! snmp counters reset at 4,294,967,295! for (;;) { $i=$i+1; $etime=time; system("snmpwalk localhost public interfaces.ifTable.ifEntry.ifInOc +tets.3 >/tmp/eth0in"); system("snmpwalk localhost public interfaces.ifTable.ifEntry.ifOutO +ctets.3 >/tmp/eth0out"); system("snmpwalk localhost public interfaces.ifTable.ifEntry.ifOutO +ctets.1 >/tmp/loout"); open(fileIN,"/tmp/eth0in") or die("Cannot open /tmp/eth0in: $!"); my @fileData = <fileIN>; close(fileIN); foreach $line (@fileData) { chomp($line); $eth0in=substr($line,54,10); } open(fileIN,"/tmp/eth0out") or die("Cannot open /tmp/eth0out: $!"); @fileData = <fileIN>; close(fileIN); foreach $line (@fileData) { chomp($line); $eth0out=substr($line,54,10); } open(fileIN,"/tmp/loout") or die("Cannot open /tmp/loout: $!"); @fileData = <fileIN>; close(fileIN); foreach $line (@fileData) { chomp($line); $lo=substr($line,54,10); } $eth0out = 0 + $eth0out; $eth0in = 0 + $eth0in; $lo = 0 + $lo; #time $tmp=$etime; $etime=$etime-$petime; $petime=$tmp; #eth0in if ($eth0in<$peth0in) { #the counter was reset $tmp=$eth0in; $eth0in=$eth0in + 4294967295; $eth0in=$eth0in-$peth0in; $eth0in=$eth0in/$etime; $eth0in=$eth0in/8; #bytes per second $peth0in=$tmp; } else { $tmp=$eth0in; $eth0in=$eth0in-$peth0in; $peth0in=$tmp; $eth0in=$eth0in/$etime; $eth0in=$eth0in/8; #bytes per second } if ($eth0out<$peth0out) { #the counter was reset $tmp=$eth0out; $eth0out=$eth0out + 4294967295; $eth0out=$eth0out-$peth0out; $eth0out=$eth0out/$etime; $eth0out=$eth0out/8; #bytes per second $peth0out=$tmp; } else { $tmp=$eth0out; $eth0out=$eth0out-$peth0out; $peth0out=$tmp; $eth0out=$eth0out/$etime; $eth0out=$eth0out/8; #bytes per second } #lo if ($lo<$plo) { #the counter was reset $tmp=$lo; $lo=$lo + 4294967295; $lo=$lo-$plo; $lo=$lo/$etime; $lo=$lo/8; #bytes per second $plo=$tmp; } else { $tmp=$lo; $lo=$lo-$plo; $plo=$tmp; $lo=$lo/$etime; $lo=$lo/8; #bytes per second } open(fileOUT, ">int.php") or dienice("Can't open log.txt for writin +g: $!"); print fileOUT "<?php\n\$eth0in=\"$eth0in\";\n\$eth0out=\"$eth0ou +t\";\n\$lo=\"$lo\";\n?>\n"; close(fileOUT); $etime=time; if ($n!=1) {system("rrdupdate sysgraphint.rrd $etime:$eth0in:$eth0o +ut:$lo");} $n=$n+1; sleep(30); }
So, great geniuses of the world, where have a gone wrong???
Thank you very much,
Jack C
jack@crepinc.com
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bad calculations?
by Eimi Metamorphoumai (Deacon) on Jul 26, 2004 at 17:09 UTC | |
by crep (Novice) on Jul 26, 2004 at 17:37 UTC | |
|
Re: Bad calculations?
by waswas-fng (Curate) on Jul 26, 2004 at 17:29 UTC | |
by crep (Novice) on Jul 26, 2004 at 17:38 UTC | |
|
Re: Bad calculations?
by graff (Chancellor) on Jul 27, 2004 at 03:35 UTC |