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

hey,
I'm trying to write a traffic checker of a Switch port via SNMP.

I wrote it using AnyEvent::SNMP timer, and OID that gives the bytes out of the required port.
My problem is when the previous result is bigger than the current result.
As I understood, I need to add 0xffffffff to the negative result,
but I get a wrong result, something that is definitely not the result I calculated... What am I doing wrong?

That's my code:

use AnyEvent; use AnyEvent::SNMP; use Net::SNMP; use Data::Dumper; my $cv = AnyEvent->condvar; my @watchers; my $OID_BytesOut = '.1.3.6.1.2.1.2.2.1.16.10101'; my ($previousResult, $currentResult) = 0; my $currentMBitsPerSecond = 0; my ($session, $error) = Net::SNMP->session ( -hostname => "127.0.0.1", -community => "yada", -version => 2, -nonblocking => 1, ); if (!defined $session) { print "ERROR creating session: $error.\n"; exit 1; } push @watchers, AnyEvent->timer( after => 0, interval => 1.0, cb => sub { $session->get_request( -varbindlist => [ $OID_BytesOut ], -callback => [ \&SNMP_get_handler ] ); if (defined $previousResult && defined $currentResult) { if ( $previousResult < $currentResult) { $currentMBitsPerSecond = ((($currentResult - $previousResult) /1024) / 1024 +) * 8; } else { my $tempCalculation = ($currentResult - $previousResult) + 0xffffffff; $currentMBitsPerSecond = ((($tempCalculation) / 1024) / 1024) * 8; } } $currentMBitsPerSecond = int($currentMBitsPerSecond); if ( $currentMBitsPerSecond > 0) { print "$currentMBitsPerSecond Mbit/s\n"; } } ); sub SNMP_get_handler { my $session = shift; my $host = $session->hostname; my $results = $session->var_bind_list; if(!defined $results) { printf("ERROR getting result: %s.\n", $session->error()); } if (defined $currentResult) { $previousResult = $currentResult; } $currentResult = $results->{$OID_BytesOut}; } $cv->wait;