Solved

PHP Thread?

Posted on 2004-09-23
10
385 Views
Last Modified: 2012-06-27
Hi,
I am performin an SNMP get a couple hundred times and right now it takes about 12 seconds to come back/connection. I was hoping to have several SNMP connections go out at once. Any help?
0
Comment
Question by:maxb
  • 5
  • 3
10 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 12137123
Create a list of targets for the SNMP get, then each time the SNMP procedure runs, read the top name off the list, and delete it from the list. Then, just run the script multiple times.

- J
0
 

Author Comment

by:maxb
ID: 12137143
Are you saying PHP does not support threads?
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 12137449
Hmmm, not in the sense that you are thinking of, probably.

If you wanted, you could use sockets and turn off blocking mode so that the script continues to process while the data is being sent/returned, but I imagine that could be some rather complex and confusing scripting, depending on what your script does. That might not even work, since you're probably using the SNMP extension instead of raw sockets, and turning off blocking probably wouldn't affect the sockets that SNMP uses.

However, since most web servers are capable of threading, the much easier solution would be to run the script multiple times.

Here's an article on non-blocking/blocking sockets:
http://www.developerfusion.com/show/28/8/

In any event, there's one more thing you could do. If you're familiar with how to talk to a device's SNMP using sockets instead of the SNMP library, then you could do something like this:

// Create Socket 1 to SNMP port on device
// Create Socket 2 to SNMP port on device
// Create Socket 3 to SNMP port on device

// Set non-blocking mode on Socket 1
// Set non-blocking mode on Socket 2
// Set non-blocking mode on Socket 3

// Query SNMP on Socket 1
// Query SNMP on Socket 2
// Query SNMP on Socket 3

// Process the results

// Close all sockets

// If you have more devices, repeat with t he next set.

- Jonathan
0
 

Author Comment

by:maxb
ID: 12193676
Sounds like there is no simple solution. Hmm...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 34

Expert Comment

by:gr8gonzo
ID: 12210775
Is there a problem with running multiple instances of the script? You can easily set up something that calls the script multiple times automatically and repeats until finished - completely in PHP, too.

Here's a combined way of doing it:

Create a simple script that accepts the device's SNMP name via query string, like:
http://www.domain.com/getsnmp.php?device=computer1

Then, have getsnmp.php use the standard SNMP library to do the dirty work. Now create a master SNMP running script that uses non-blocking sockets to call the script several times. That master script can query a database, and loop through a list of device names, running 5 or so instances of getsnmp.php at a time, passing the device names through the URL. You only need to open the socket in non-blocking mode, and send the HTTP commands to fetch the page, which is easy to do. Just open a socket to the host on port 80 and send the following 2 lines of text, each line followed by a carriage return (\r\n):

GET /path/to/getsnmp.php?device=devicenamehere HTTP/1.0
Host: www.domain.com
<then one more carriage return \r\n for a blank line>

That will start a HTTP request to the server, which will begin running the getsnmp.php script, and you can fread() the results. At that point, you're basically allowing the web server to perform the threading, so you get the benefit of multi-threading and no difference in actual results.

But like I said, if there's a problem with this solution, let us know. Threading isn't really a simple issue to begin with, so this is relatively pretty simple.

- J
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 12281616
The author hasn't provided any reason as to why my solution would not work for him. If it doesn't work, please let us know why, so others can benefit from reading this later.

Threading isn't an easy issue in any language, so the answer will be tough. That's no reason to try to back out of it or ignore the answer.

- J
0
 

Author Comment

by:maxb
ID: 12281887
The solution required running a single instance of a script with several threads calling the snmpget command. I ended up going with Perl instead of PHP because I did not wish to make my own direct SNMP calls and Perl supports multithreading and using the SNMP library.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 12283028
Okay, good to know that it worked out. Thanks!

- J
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12321384
Question answered by asker or dialog valuable.
Closed, 125 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now