marcs has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I'm currently trying to write a small perl program that uses the DBI and SNMP modules. The job of the script is the following: o To collect stats from the switch on a per port basis o To record that data to a mySQL database o To tally up the amount of traffic on that port in MB Here's the code:
#!/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"; } }
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

Replies are listed 'Best First'.
RE: Off Topic? Help needed...
by little (Curate) on Nov 15, 2000 at 13:14 UTC
    I guess the single quotes are doing something different than you expected. Better compose a $statement string which you then prepare and execute, otherwise you are entering text (ASCII chains) cause the single quoted var names won't be evaluated, or did I get something wrong now?

    Have a nice day
    All decision is left to your taste
RE: Off Topic? Help needed...
by marcs (Acolyte) on Nov 15, 2000 at 13:53 UTC
    removal requested -- this is a duplicate