Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

limiting number of snmp requests

by dilip.renkila (Initiate)
on Nov 06, 2015 at 15:57 UTC ( [id://1147106]=perlquestion: print w/replies, xml ) Need Help??

dilip.renkila has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have an array of 370 oids and every time i had to probe only 50 oids and rest of them should be probed next time.I have no idea of implementing this concept in loops. Help me out

Replies are listed 'Best First'.
Re: limiting number of snmp requests
by choroba (Cardinal) on Nov 06, 2015 at 16:03 UTC
    I don't know how snmp works and what an oid is. But to process an array in batches of 50, you can use splice:
    #!/usr/bin/perl use warnings; use strict; my @oids = (1 .. 370); while (@oids) { print "Starting iteration\n"; my @process = splice @oids, 0, 50; print "probe $_\n" for @process; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Dilip, Here's another way of doing the loop. I am assuming the oids are the same everytime you execute the script. You did not specify if the script was executed once or many times to remember it's stopping point. I took the code from choroba and changed a few lines.
      #!/usr/bin/perl use warnings; use strict; my @oids = ( '.1.2.3.4.2.1.3.0', '.1.23.4.2.1.3.4.0'); my $oid = undef; my $ct = 0; foreach $oid (@oids) { if ($ct > 50) { print "PROBED CT: \"$ct\" OIDS. SLEEPING X\n"; sleep + 10; $ct = 0; } print "PROCESSING OID: \"$oid\" \n"; $ct += 1; }
      Joe
Re: limiting number of snmp requests
by Athanasius (Archbishop) on Nov 06, 2015 at 16:56 UTC

    Hello dilip.renkila,

    If you want to probe each OID once only, then choroba’s approach is the correct one. But if you want to repeatedly re-probe the OIDs in batches of 50, you will need a circular list. See How do I handle circular lists? Here is one method:

    #! perl use strict; use warnings; use Tie::Cycle; my @oids = (1 .. 370); tie my $oid, 'Tie::Cycle', \@oids; my $answer = 'Y'; while ($answer !~ /N/i) { print "Starting iteration\n"; print "probe $oid\n" for 1 .. 50; print "Continue? "; $answer = <STDIN>; }

    This should give you an idea of how to proceed.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: limiting number of snmp requests
by RichardK (Parson) on Nov 06, 2015 at 18:56 UTC

    Sounds like a job for natatime from List::MoreUtils

    the help says this :-

    natatime EXPR, LIST Creates an array iterator, for looping over an array in chunks of +$n items at a time. (n at a time, get it?). An example is probably a better explanation than I could give in words. Example: my @x = ('a' .. 'g'); my $it = natatime 3, @x; while (my @vals = $it->()) { print "@vals\n"; } This prints a b c d e f g

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1147106]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found