Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Perl and SNMP

by castaway (Parson)
on Jan 23, 2003 at 08:34 UTC ( [id://229255]=perlquestion: print w/replies, xml ) Need Help??

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

A bit of background.. We have several products, which run on a number of unix servers at customer sites, sometimes more than one product runs on each machine. To be able to keep an eye on whats going on, we also provide SNMP agents. Since SNMP usually only allows one agent per machine, we're using a 3rd party software which allows sub-agents.
The main agent asks the sub-agents to check the product they are watching at certain intervals, and shows the results on some sort of monitor (and in a log.) When someone specifically asks for the status, then the main agent asks the subagent for each of its values again. These two checks can happen at the same time.

So, where does Perl come into this? The product that I work on provides a perl script which can be called with certain parameters to check if the system is running properly. The subagent for the product calls this script to get its information. For example, part of our system consists of several ftp processes which fetch statistic files from other products. Could be 10 or more of them running at the same time. The perl script which checks these uses a system call to ps and grep, called once for each ftp-process, to see if they are running.

My problem is this: If the main agent calls this script routinely, and asks about the ftp-processes, the script takes a while to run (several seconds). If a user asks about the status at the same time, the user process usually times out, as the subagent can (apparently) only do one thing at a time, while its waiting for the answer to the usual poll, it can't re-ask to get answers for the user (I will also be looking at the subagent code, to see if I can change that..)
I'm thinking that calling the script every 20 secs, which is then calling 'ps' etc. several times is quite a bit of overhead. Has anyone any ideas how I can optimize this? Maybe start the script at system-start and keep it running, and somehow pass it options/get answers while its running? Or some other methods of checking if certain processes are running? (But without too many extra-modules, if possible, though I guess we could supply those.)

I can summerize the code, if needed, though I'm more looking for generic ideas..

C.

Replies are listed 'Best First'.
Re: Perl and SNMP
by robartes (Priest) on Jan 23, 2003 at 08:58 UTC
    One optimisation you could do, is just doing one ps and searching for your processes in the output of that one. (Although you prefer not to use modules, have a look at Proc::ProcessTable for this). Some code that does this without modules:
    use strict; my $interesting_process="ftp"; # Adjust to taste my @processlist=grep(/$interesting_process/,`ps -fe`); #backtics are e +vil - consider Proc::ProcessTable
    The processlist array now contains those processes that have ftp somewhere in them (you might want to refine this, as it will also match processes by a user called daftpeter, for example). You can then browse through this list, count the processes, or do whatever you have to do to verify that everything that you expect to be running is actually running.

    Disclaimer: code is untested.

    CU
    Robartes-

Re: Perl and SNMP
by drone (Monk) on Jan 23, 2003 at 14:02 UTC
    What I do fairly often is run a separate script/program that updates some statistics file.
    When an SNMP subagent needs its data it just has to read that file for the latest statistics. To minimize the chance of a race condition (trying to read the file while its only half full or some similar misshap) i would recommend writing to a different filename and then doing a quick rename when everything is ready.
Re: Perl and SNMP
by !unlike (Beadle) on Jan 23, 2003 at 14:04 UTC
    Assuming that the IPC between your users query and the sub-agent uses either a filehandle or socket, then How do I print to more than one file at once? might be of some use.

    You could use this trick so that if the sub-agent already has a handle open (i.e. it is currently in the process of getting the required data) it could just tack on to the handle the other users query. That way the sub-agent could satify both requests with just one lookup.

    Of course I'm making assumptions about how the IPC works and so this advice could be totally useless. :)

    !unlike

    "The price if ignorance, which is of course that you must learn from those who know." Scorates (paraphrased)
      Hmm, 'fraid not, I think. The problem is that the subagent doesn't call the script for the users request, if its waiting for an answer from a previous call. It uses popen() to open a pipe to the script and then waits for it to return. (Not sure if/how I can change this yet..)

      C.

Re: Perl and SNMP
by Anonymous Monk on Jan 23, 2003 at 16:49 UTC
    I did something similar (Poll every 30 seconds). I found that using ptree took less time, especially when you know the user id you are running under. Also, the idea of reading the ps only once and doing searches on the processes instead of grep helps. Jason
Re: Perl and SNMP
by castaway (Parson) on Jan 24, 2003 at 12:26 UTC
    Thanks for the ideas..
    I'll probably try doing the 'ps' calls once, and keeping/using the results for each check of the ftp processes in that loop.
    As for keeping the results in a file, I can probably get the subagent to do that, just hand out the results of the last loop, rather than call the script again when its already running.

    I've been looking around and just come across PPerl and Persistent Perl. Does anyone use/know of these? Can I install them anywhere?

    C.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-19 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found