Update: Previously, I noted the time after enqueuing 10k traps into the queue. Unfortunately, I didn't factor the traps still pending in the queue and met to report the duration time after the queue is depleated. I've gone through and corrected all my posts in this thread.

Hello jvuman and welcome to the amazing monastery. Thank you for introducing Net::SNMPTrapd which I've not used before.

It is possible to run a powerful trap server using a single listener and many consumers. One might do so using threads and Thread::Queue or similarly with MCE::Flow and MCE::Queue. The latter is provided below. Here, I have each consumer sleep for 4 milliseconds to simulate work. Awaiting on the queue is a safety measure to prevent the queue from consuming gigabytes of memory in the event receiving millions of traps. Please adjust the threshold to your satisfaction.

In my testing, the server process never entered the pending if statement. MCE and MCE::Shared (not used here) involve IPC behind the scene. Fetching is faster from a BSD OS (e.g. FreeBSD, darwin) when compared to Linux. See this post for more info.

perl snmp_test.pl >/dev/null duration: 1.013 seconds

That's seems fast considering that 2 producers share CPU time with the listener process and consumers.

use strict; use warnings; use feature 'say'; use Time::HiRes qw( sleep time ); use Net::SNMPTrapd; use MCE::Flow; use MCE::Queue; my $queue = MCE::Queue->new( await => 1, fast => 1 ); my $start = time; # floating seconds # from left to right: 1 listener, 30 consumers, and 2 producers mce_flow { max_workers => [ 1, 30, 2 ] }, \&listener, \&consumer, \&pr +oducer; printf {*STDERR} "duration: %0.3f seconds\n", time() - $start; exit(0); # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # listener and consumer roles # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ sub listener { my $snmptrapd = Net::SNMPTrapd->new( ReusePort => 1 ); my $count = 0; while ( 1 ) { my $trap = $snmptrapd->get_trap(); if ( !defined $trap ) { printf {*STDERR} "$0: %s\n", Net::SNMPTrapd->error(); next; } elsif ( $trap == 0 ) { next; } # important, remove the file handle inside the object # so that serialization into the queue succeeds delete $trap->{_UDPSERVER_}; # enqueue the trap for a consumer to process $queue->enqueue($trap); # await will block if the queue has more than 2000 pending # this prevents the queue from consuming memory out of control # simply increase the number of consumers to not block # # $queue->await(2000) if ( ++$count % 4000 == 0 ); if ( ( ++$count % 4000 == 0 ) && $queue->pending() > 3000 ) { say {*STDERR} "$0: WARN: blocking temporarily"; $queue->await(2000); } # reset the counter to not overflow $count = 0 if ( $count > 2e9 ); # for benchmarking, leave the loop after 4000 traps last if ( $count == 4000 ); } # notify the manager process to stop the producer and queue MCE->do('quit_producers'); } sub consumer { while ( defined ( my $trap = $queue->dequeue() ) ) { $trap->process_trap(); # printf "[$$] %s\t%i\t%i\t%s\n", # $trap->remoteaddr, $trap->remoteport, # $trap->version, $trap->community; say "[$$] ".$trap->varbinds->[5]->{'1.3.6.1.4.1.50000.1.6'}; sleep 0.004; } } # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # producer role for generating traps # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ my @producer_pids; sub set_producer_pid { push @producer_pids, $_[0]; } sub quit_producers { kill 'QUIT', @producer_pids; $queue->end(); } sub producer { require Net::SNMP; my ( $run, $i ) = ( 1, 0 ); $SIG{QUIT} = sub { $run = 0 }; # notify the manager process my pid MCE->do('set_producer_pid', $$); my ( $session, $error ) = Net::SNMP->session( -hostname => 'localhost', -version => 2, -community => 'public', -port => 162 ); if ( !defined $session ) { printf "Error: Starting SNMP session (v2c trap) - %s\n", $erro +r; return; } while ( $run ) { my $result = $session->snmpv2_trap( -varbindlist => [ '1.3.6.1.2.1.1.3.0', 0x43, int( time() ), '1.3.6.1.6.3.1.1.4.1.0', 0x06, '1.3.6.1.4.1.50000', '1.3.6.1.4.1.50000.1.3', 0x02, 1, '1.3.6.1.4.1.50000.1.4', 0x04, 'String', '1.3.6.1.4.1.50000.1.5', 0x06, '1.2.3.4.5.6.7.8.9', '1.3.6.1.4.1.50000.1.6', 0x40, '10.10.10.'.((++$i % 1 +00) + 1), '1.3.6.1.4.1.50000.1.7', 0x41, 32323232, '1.3.6.1.4.1.50000.1.8', 0x42, 42424242, '1.3.6.1.4.1.50000.1.9', 0x43, int( time() ), '1.3.6.1.4.1.50000.1.10', 0x44, 'opaque data' ] ); } $session->close; }

In reality, the server process can handle more than 4,000 traps per second simply by running the server and consumers only.

mce_flow { max_workers => [ 1, 30 ] }, \&server, \&consumer;

Regards, Mario.


In reply to Re: worker threads - one does all the work by marioroy
in thread worker threads - one does all the work by jvuman

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.