Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Process Monitoring Script

by tradez (Pilgrim)
on Jan 19, 2002 at 21:48 UTC ( #140096=perlquestion: print w/ replies, xml ) Need Help??
tradez has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks, I come to you with a request for help in finding the best way to attack a problem. I am a midway sysadmin at my current job, and I am wanting to setup a daemon to run on a box and monitor certain process to make sure that key things, probably fed from a flat file or oracle, are always running and responding correctly. If there die, I need to either a) be able to spawn them again (shouldn't be too hard) or simply b) inform me or a list of people of there downness (word?). Please fill me with your knowledge perl monks, cause I am pretty close to just
$ret = `ps -ef`; $ret =~ /nastyperlretoparseoutput/; blah blah blah....
There must be a better way!!!!
tradez

Comment on Process Monitoring Script
Download Code
Re: Process Monitoring Script
by jlongino (Parson) on Jan 19, 2002 at 22:22 UTC
    There are many ways to do this but if you go with a scraping ps method, you might want to try using the -o switch which will simplify your parse step. Check the man pages for the -o switch keywords. I just used split instead of a regex:
    @outrecs = `ps -efo pid,pcpu,fname`; @matches = grep /ns-httpd/, @outrecs; foreach (@matches) { $ct++; chomp; ($process, $pcpu, $pname) = split(" "); $sttotal = $sttotal + $pcpu; }
    This is a snippet of real code in which I did use strict (omitted in the example though). I also had a related thread a while back with useful info. HTH.

    --Jim

    Update: You can also use:

    @matches = `ps -efo pid,pcpu,fname|grep httpd`;
    to eliminate the need for the Perl grep. Just realized that after I posted. Learn something all the time. :)
(jptxs) Re: Process Monitoring Script
by jptxs (Curate) on Jan 19, 2002 at 22:26 UTC
    Well, I'll preface this by saying I work for a company that sells stuff to do things like this on a grand scale. If you want to contact me privately regarding that after you see this suggestion, you may =] However, since your need seems to be small and since free is cheap, have you seen Big Brother? It does what you're asking and a whole bunch more and it's free. It's not fully functional as a full application or enterprise monitor, but it will solve your problem for sure.

    good luck.

    We speak the way we breathe. --Fugazi

      I'll second the Big Brother suggestion, and also toss in another for NetSaint. I've used both, and am leaning towards replacing an old BB instalation with NetSaint. YMMV.

      -Blake

      I'll also put in in a good word for Big Brother. It's our primary monitor and is working well. You may also want to look at Spong which was inspired by BB but is written in perl.

      Once you start down this road you'll find lots more things that you want to monitor and I'd vote for an existing framework over re-inventing the wheel. Happy Monitoring!

      Dave

Re: Process Monitoring Script
by Marcello (Hermit) on Jan 20, 2002 at 02:35 UTC
    To check whether a certain process is still running, use:
    my $alive = (kill 0 => $pid);
    I personally use this to check on running processes from the crontab, and restart them if this check fails.
Re: Process Monitoring Script
by rob_au (Abbot) on Jan 20, 2002 at 05:11 UTC
    As I have posted before, I am a great proponent of implementing within Perl rather than invoking another shell and relying on external programs. With that in mind, I would recommend the usage of Proc::ProcessTable which I have reviewed previously on this site here.

    Using Proc::ProcessTable, your code would look something like this:

    use Proc::ProcessTable; my $running = 0; my $proc = Proc::ProcessTable->new; foreach ( @{ $proc->table } ) { if ($_->cmndline =~ /nameofprocess/) { $running = 1; last; } } if (!$running) { # do something to reinvoke process }

    Much neater than using non-portable ps calls and nasty regular expressions to my mind :-)

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://140096]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-07-13 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls