Source for file ProcWatch.php
Documentation is available at ProcWatch.php
// +----------------------------------------------------------------------+
// | PEAR :: System :: ProcWatch |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is available at http://www.php.net/license/3_0.txt |
// | If you did not receive a copy of the PHP license and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at> |
// +----------------------------------------------------------------------+
// $Id: ProcWatch.php 304228 2010-10-10 03:05:12Z clockwerx $
* Requires System::ProcWatch::Parser
require_once 'System/ProcWatch/Parser.php';
define('PHP_PROCWATCH_PRESENCE', 100 );
define('PHP_PROCWATCH_PRESENCE_MIN', 101 );
define('PHP_PROCWATCH_PRESENCE_MAX', 102 );
define('PHP_PROCWATCH_IS', 103 );
define('PHP_PROCWATCH_ISNOT', 104 );
define('PHP_PROCWATCH_MIN', 105 );
define('PHP_PROCWATCH_MAX', 106 );
define('PHP_PROCWATCH_SUM', 107 );
* $cf = System_ProcWatch_Config::fromXmlFile('/etc/procwatch.xml');
* $pw = &new System_ProcWatch($cf);
* @author Michael Wallner <mike@php.net>
* @package System_ProcWatch
* @version $Revision: 304228 $
* @var object System_ProcWatch_Parser
* @return object System_ProcWatch
* @param array $config config array from System_ProcWatch_Config
System_Procwatch ::__construct ($config);
* @return object System_ProcWatch
* @param string $config path to configuration file
* @param string $ps_args ps' args
function run($ps_args = 'aux')
// get actual process' data
foreach ($this->_jobs as $job) {
// get the pattern to search for
$pattern= $this->_patt[$job][$search];
// search for the line we need
$this->_procs[$job][] = $p;
if (isset ($this->_cond[$job]['presence'])) {
$this->_handlePresence ($job);
if (isset ($this->_cond[$job]['attr'])) {
$this->_handleAttr ($job);
* @param int $interval seconds to sleep
* @param string $ps_args ps' arguments
function daemon($interval, $ps_args = 'aux')
function _handlePresence ($job)
$presence = @count($this->_procs[$job]);
$condition = $this->_cond[$job]['presence'];
if (!isset ($condition['max']) && !isset ($condition['min'])) {
if (isset ($condition['max']) && ($condition['max'] < $presence)) {
array ('name' => $name, 'max' => $condition['max'])
if (isset ($condition['min']) && ($condition['min'] > $presence)) {
array ('name' => $name, 'min' => $condition['min'])
function _handleAttr ($job)
$attr = $this->_cond[$job]['attr'][$name];
$proc = isset ($this->_procs[$job]) ? $this->_procs[$job] : array ();
if (isset ($attr['sum'])) {
if ($sum > $attr['sum']) {
if (isset ($attr['max'])) {
if ($p[$name] > $attr['max']) {
} elseif (isset ($attr['min'])) {
if ($p[$name] < $attr['min']) {
} elseif (isset ($attr['is'])) {
if ($p[$name] == $attr['is']) {
} elseif (isset ($attr['isnot'])) {
if ($p[$name] != $attr['isnot']) {
$this->_execute ($job, $sum, $const, $attr);
function _execute ($job, $sum, $mode, $info = null )
$event_msg = $this->_getMsg ($job, $sum, $mode, $info);
$shell_exec = @$this->_exec[$job]['shell'];
$php_exec = @$this->_exec[$job]['php'];
foreach ($shell_exec as $e) {
$procs = 'unserialize(\'' . serialize(@$this->_procs[$job]) . '\'';
foreach ($php_exec as $e) {
* Get processes' PIDs of a certain job
if (isset ($this->_procs[$job])) {
foreach ($this->_procs[$job] as $proc) {
$str .= $proc['pid'] . ', ';
return '\'(' . trim($str, ', ') . ')\'';
function _getMsg ($job, $sum, $mode, $a)
$m = " $sum procs where $w is $is";
$m = " $sum procs where $w is not $isnot";
$m = " $sum procs where $w exceeds $max";
$m = " $sum procs where $w under-runs $min";
$match = $this->_patt[$job][$w];
$m = " $sum procs where $w matches $match";
$match = $this->_patt[$job][$w];
$m = " $sum (min $min) procs where $w matches $match";
$match = $this->_patt[$job][$w];
$m = " $sum (max $max) procs where $w matches $match";
$c = count($this->_procs[$job]);
$m = " $c procs which sum of $w ($sum) exceeds $s";
return '\'' . date('r') . ' - '. $job . ': Found ' .
* @param array $config config array from System_ProcWatch_Config
* @param array $config config array from System_ProcWatch_Config
foreach ($config as $job => $arrays) {
$this->_patt[$job] = $arrays['pattern'];
$this->_cond[$job] = $arrays['condition'];
$this->_exec[$job] = $arrays['execute'];
Documentation generated on Mon, 11 Mar 2019 15:39:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|