marcs has asked for the wisdom of the Perl Monks concerning the following question:
My question is this -- the current code works, but it's getting HUGE numbers that just aren't possible. Am I doing this the correct way or can someone suggest something that can help me?? Thanks, Marc#!/usr/bin/perl -w use DBI; use SNMP; my( $db ) = "switch"; my( $host ) = "localhost"; my( $user ) = "switch"; my( $password ) = "fweep"; my( $community ) = "erp"; # this obviously changed # Open the Database and SNMP connections. $dbh = DBI->connect("DBI:mysql:database=$db;host=$host", $user, $password, {RaiseError => 1}) || die "Cannot connect to database.\n"; # Get the switchnames from the database :) my( $sth ) = $dbh->prepare( "SELECT ip, switchname FROM switch" ); $sth->execute(); while( my $ref = $sth->fetchrow_hashref() ) { @list = $ref->{'ip'}; for $swhost( @list ) { #print "getting SNMP for $swhost...\n"; my $sess = new SNMP::Session ( DestHost => $swhost, Community => $ +community ); # Get the number of interfaces. We subtract one from the end beca +use # the first port is 0. my $vars = new SNMP::VarList([ifNumber]); $ports = $sess->getnext($vars) -1; $count = 0; while( $count <= $ports ) { my $vars = new SNMP::VarList([ifInOctets,$count],[ifOutOctets,$c +ount]); @vals = $sess->getnext($vars); #print "$vals[0]:$vals[1]\n"; $newin = $vals[0]; $newout = $vals[1]; my $sth = $dbh->prepare( "SELECT * FROM allocation WHERE ip='$sw +host' AND port='$count'" ); $sth->execute(); if( my $ref = $sth->fetchrow_hashref() ) { $lastin = ( $ref->{'lastin'} ); $lastout = ( $ref->{'lastout'} ); $linoctet = ( $ref->{'inoct'} ); $loutoctet = ( $ref->{'outoct'} ); #$client = ( $ref->{'client'} ); } else { print( "No data for '$swhost','$count' ??\n" ); $lastin = 0; $lastout = 0; $linoctet = 0; $loutoctet = 0; #$client = "Unknown (No record)"; } if( $newin > $lastin ) { $newtotalinoctets = ( ( $newin - $lastin ) + $linoctet ); } else { $newtotalinoctets = ( $newin + $linoctet); } if( $newout > $lastout ) { $newtotaloutoctets = ( ( $newout - $lastout ) + $loutoctet ); } else { $newtotaloutoctets = ( $newout + $loutoctet ); } $inMB = ( $newtotalinoctets / ( 1024 * 1024 * 8 ) ); $outMB = ( $newtotaloutoctets / ( 1024 * 1024 * 8 ) ); $sth = $dbh->prepare( "REPLACE INTO allocation SET lastin='$newi +n', lastout='$newout', inoct='$newtotalinoctets', outoct='$newtotalou +toctets', inoctmb='$inMB', outoctmb='$outMB', ip='$swhost', port='$co +unt'" ); $sth->execute(); $count++; } #print "Got stats for $swhost\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Off Topic? Help needed...
by little (Curate) on Nov 15, 2000 at 13:14 UTC | |
|
RE: Off Topic? Help needed...
by marcs (Acolyte) on Nov 15, 2000 at 13:53 UTC |