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

Hello everyone,

I am having some trouble with perl threads and snmp. What i am trying to do is to create a pool of threads in that each thread in the pool is making the same snmpget on a different host. Each thread establishes its own SNMP session naturally and has its own SNMP variables. The threads on the pool are waiting for ip addresses to work with. The thing is when the first ip address is available and the first thread starts to work it can't do the snmpget and it says "thread failed to start" because of it. I have made print on every variable possible and still don't get it, why can't each thread do an snmp get individually. Notice that as i have said each thread is creating it's own session and hence doing it's own snmpget.

To invoke the threads i do "use threads;" and to invoke snmp libraries i do "use SNMP;"

help ?

Cheers

Replies are listed 'Best First'.
Re: Problem using threads and snmp
by BrowserUk (Patriarch) on Apr 15, 2008 at 16:09 UTC

      s{line 11}{line 11i}. Although you'll need Math::Complex for that.

      
      -- 
      Human history becomes more and more a race between education and catastrophe. -- HG Wells
      
Re: Problem using threads and snmp
by NetWallah (Canon) on Apr 15, 2008 at 21:06 UTC
    You will only get abstract (update: or Imaginary) advice, such as the one from Browseruk, until you post some code.

    Your narrative is conflicting, in that you say the threads are started, then obtain IP's. Later your error message says the "thread failed to start".

    FYI - if you need working thread code, it is available here.

         "How many times do I have to tell you again and again .. not to be repetitive?"

      Hello, Sorry if you didn't understand but i couldn't show you code at that time. Now i can. First of all i have this variables:
      use SNMP; use Net::MAC; my $ip_address : shared= ""; my $pool : shared = 0; my $nthreads = 10; # Number of threads in pool
      Then i have the code to create a pool of threads like this:
      foreach (1..$nthreads) { threads->create(\&getIP); }
      Then i have a main thread, the main program if you will, that passes, from a list of ip addresses, an ip to each of the threads on the poool. The code is something like this:
      foreach $ip (@ip_addresses) { $ip_address = $ip; if ($pool==0) { print "Main thread has no service threads available, yielding\ +n"; threads->yield until $pool>0; } print "Main thread has $pool service threads available\n"; # signal that a new line is ready { lock $pool; cond_signal $pool; } print "Main thread sent signal, waiting to be signaled\n"; # wait for whichever thread wakes up to signal us { lock $ip_address; cond_wait $ip_address; } print "Main thread received signal, reading next line\n"; }
      Then i have my sub for each thread which is something like this:
      sub getIP { while (1) { print "Thread ",$self->tid," waiting\n"; { lock $pool; $pool++; print "Pool incrementada : $pool\n"; cond_wait $pool; #all threads wait here for si +gnal $pool--; print "Pool decrementadas : $pool \n"; } # Here i get the ip address my $t_ip = $ip_address; sleep 1; # now let's tell main that we already have the ip addre +ss print "Thread ",$self->tid," retrieved data, signaling + main\n"; { lock ($ip_address); cond_signal ($ip_address); } #now i do the snmp session in the thread plus a snmpget my $sess = new SNMP::Session (DestHost => $t_ip, Commun +ity => ..., Version => '2c', UseSprintValue => '1') or die "couldn't open a session"; #The oid my $itens = new SNMP::VarList(['sometextthing', $oid]) +or die "couldn't create a varlist"; $info = $sess->get($itens) or die "couldn't get info"; ... } }
      The threads start ok but when they try to do the snmpget it appears something like "thread failed to start at "line of the get". Couldn't get info" blablabla. I printed every variable that there was to print. I tried testing the snmpget in another script with the same oid and it worked. I don't know if this has anything to do with snmpsession not being thread safe because i create a session inside each thread. Sorry if all of this seems confusing but it's my first post in here. I usually find the solution to my problems by browsing this site. All problems except this one. Also i had to arrange a similar code to exemplify the problem since i can't post here the real code. Cheers.

        The "thread failed to start" is a red-herring/badly chosen error message. It simple means that the thread died.

        The reason the thread dies is because you are asking it to:

        $info = $sess->get($itens) or die "couldn't get info";

        There are many reasons why the get() could fail. The first thing to do is get some more information than your error message gives. Appending $sess->error to your message would be a good start.

        If that doesn't help you resolve the problem, try enabling debug:

        $session->debug( DEBUG_ALL );

        See the POD for details.

        Once you know why that call is failing, you have a chance to do something about it.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.