ps-watcher - monitors various processes based on ps-like information.
ps-watcher [options...] [--config
] config-file
Periodically a list of processes names via ps
is obtained. A configuration file specifies a list of Perl
regular-expression patterns to match the processes against. For each match,
a Perl expression specified for that pattern is evaluated. The evaluated
expression can refer to variables which are set by ps and pertain to the
matched process(es),
for example the amount memory consumed by
the process, or the total elapsed time. Some other variables are set by the
program, such as the number of times the process is running. If the Perl
expression for a matched pattern evaluates true, then an action can be run
such as killing the program, restarting it, or mailing an alert.
This program can be used to ensure a daemon hasn't died or ensure it is not running too many times. It might be used to determine when a process has consumed too many resources (for example due to a memory leak).
Depending on options specfied, this program can be run as a daemon, run
once (which is suitable as a cron
job), or run not as a daemon but still continuously (which may be handy in
testing the program or your configuration).
Print a usage message on standard error and exit with a return code of 100.
Print the version release on standard output and exit with a return code of 10.
Specify configuration file. .
See CONFIGURATION FILE FORMAT below for information on the format of the configuration file and EXAMPLE CONFIGURATION for a complete example of a configuration file.
Send or don't send error and debugging output to a log file. If option is given but no logfile is specified, then use STDERR. The default is no error log file. See also --syslog below.
Send or don't send error and debugging output to syslog. The default is to syslog error and debug output.
Run or don't as a daemon.
One can specify the command that gives ps information. By default, the command is /bin/ps.
It is expected that one might want to run ps-watcher over and over again. In such instances one can specify the amount of time between iterations with this option.
If a negative number is specified the program is run only once.
Periodically ps-watcher checks to see if the configuration file that it was run against has changed. If so, the program rereads the configuration file.
More precisely, the checks are done after waking up from a slumber. If the sleep interval is long (or if you are impatient), you can probably force the program to wake up using a HUP signal.
At any time you can increase the level of debug output by sending a USR1 signal to the ps-watcher process. Similarly you can decrease the level of debug output by sending the process a USR2 signal.
It is recommended that you terminate ps-watcher via an INT, TERM, or QUIT signal.
The format of a configuration file is a series of fully qualified filenames enclosed in square brackets followed by a number of parameter lines. Each parameter line has a parameter names followed by an ``equal'' sign and finally value. That is:
# This is a comment line ; So is this. [process-pattern1] parameter1 = value1 parameter2 = value2
[process-pattern2] parameter1 = value3 parameter2 = value4
Comments start with # or ; and take effect to the end of the line.
This should be familiar to those who have worked with text-readible
Microsoft .INI
files.
Note process patterns, (process-pattern1 and process-pattern2 above) must be unique. If there are times when you may want to refer to the same process, one can be creative to make these unique. e.g. cron and [c]ron which refer to the same process even though they appear to be different.
As quoted directly from the IniConf manual page:
Multiline or multivalued fields may also be defined ala UNIX ``here document'' syntax:
Parameter=<<EOT value/line 1 value/line 2 EOT
You may use any string you want in place of ``EOT''. Note that what follows the ``<<`` and what appears at the end of the text must match exactly, including any trailing whitespace.
A description of parameters names, their meanings and potential values follows.
This parameter specifies the condition on which a process action is fired.
The condition is evaluated with Perl eval()
and should
therefore return something which is equivalent to ``true'' in a Perl
expression.
If no trigger is given in a section, true or 1 is assumed and the action is unconditionally triggered.
Example:
# Fire if /usr/sbin/syslogd is not running. # Since the program matches against the command names not commands and # arguments something like: # ps -ef | grep /usr/sbin/syslogd # won't match the below. [(/usr/sbin/)?syslogd] trigger = $count != 1
This parameter specifies how many times an action should be performed on processes matching the section trigger. Acceptable values are ``every'' and ``first''. If this parameter is not specified, ``first'' is assumed.
Examples:
[] occurs = first action = echo "You have $count processes running"
[.] trigger = $vsz > 1000 occurs = every action = echo "Large program $command matches $ps_pat: $vsz KB"
This specifies the action when the trigger condition is evaluated to be true.
Example:
action = /etc/init.d/market_loader.init restart
Any variables defined in the program can be used in pattern or action
parameters. For example, $program
can be used to refer to the name of this program ps-watcher.
The following variables can be used in either the pattern or action fields.
A string containing the text of the action to run.
The Perl regular expression specified in the beginning of the section.
The command that matched $ps_pat.
The Perl regular expression specified in the beginning of the section.
The number of times the pattern matched. Presumably the number of processes of this class running.
A string containing the text of the trigger.
A list of variables specific to this program or fields commonly found in
ps
output is listed below followed by a description of the more common ones.
See also ps
for a more complete description of the meaning of the field.
alarm blocked bsdstart bsdtime c caught cputime drs dsiz egid egroup eip esp etime euid euser f fgid fgroup flag flags fname fsgid fsgroup fsuid fsuser fuid fuser gid group ignored intpri lim longtname m_drs m_trs maj_flt majflt min_flt minflt ni nice nwchan opri pagein pcpu pending pgid pgrp pmem ppid pri rgid rgroup rss rssize rsz ruid ruser s sess session sgi_p sgi_rss sgid sgroup sid sig sig_block sig_catch sig_ignore sig_pend sigcatch sigignore sigmask stackp start start_stack start_time stat state stime suid suser svgid svgroup svuid svuser sz time timeout tmout tname tpgid trs trss tsiz tt tty tty4 tty8 uid uid_hack uname user vsize vsz wchan comm
The parent process id.
The start time of the process.
The end time of the process.
The process memory.
The percent CPU utilization.
The controlling tty.
Virtual memory size of the process
To make testing against elapsed time easier, a function elapse2sec()
has been written to parse and convert elapsed time strings in the format dd-hh:mm:ss
and a number of seconds.
Some constants for the number of seconds in a minute, hour, or day have
also been defined. These are referred to as MINS
, HOURS
, and DAYS
respectively and they have the expected definitions:
use constant MINS => 60; use constant HOURS => 60*60; use constant DAYS => HOURS * 24;
Here is an example of the use of elapsed2sec()
:
# Which processes have been running for more than 3 hours? # Also note use of builtin-function elapsed2secs, variable $etime # and builtin-function HOURS [] trigger = elapsed2secs('$etime') > 1*DAYS action = echo "$command has been running more than 1 day ($etime)" occurs = every
Please note the quotes around '$etime'.
# Comments start with # or ; and go to the end of the line.
# The format for each entry is in Microsoft .INI form: # [process-pattern] # trigger = perl-expression # action = program-and-arguments-to-run
[(/usr/sbin/)?cron$] trigger = $count != 1 action = echo "$trigger fired -- You have $count cron sessions."
[] trigger = $vsz > 10 action = echo "Looks like you have a big $command program: $vsz KB"
[.] trigger = $pcpu > 70 occurs = every action = <<EOT echo "$command used $pcpu% CPU" | /bin/mail root kill -TERM $pid EOT
See also ps(1)
and syslogd
.
Another cool program doing ps-like things is xps
. Well okay, it's another program I distributed. It shows the process tree
dynamically updated using X Motif and tries to display the output
``attractively'' but fast. You can the find the homepage at
http://www.netwinder.org/~rocky/xps-home
and it is available via anonymous FTP at ftp://ftp.netwinder.org/users/r/rocky/xps.tar.gz
Rocky Bernstein (rocky@panix.com)
Copyright (C) 2000 Rocky Bernstein, email: rocky@panix.com. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.