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;

In reply to SNMP Traffic Checker by Adamba

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.