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

In reply to Off Topic? Help needed... by marcs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.