System_ProcWatch
[ class tree: System_ProcWatch ] [ index: System_ProcWatch ] [ all elements ]

Source for file ProcWatch.php

Documentation is available at ProcWatch.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: ProcWatch                                          |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at>             |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: ProcWatch.php 304228 2010-10-10 03:05:12Z clockwerx $
  15.  
  16. /**
  17. * Requires System::ProcWatch::Parser
  18. */
  19. require_once 'System/ProcWatch/Parser.php';
  20.  
  21. /**
  22. * Constants
  23. */
  24. define('PHP_PROCWATCH_PRESENCE',        100);
  25. define('PHP_PROCWATCH_PRESENCE_MIN',    101);
  26. define('PHP_PROCWATCH_PRESENCE_MAX',    102);
  27. define('PHP_PROCWATCH_IS',              103);
  28. define('PHP_PROCWATCH_ISNOT',           104);
  29. define('PHP_PROCWATCH_MIN',             105);
  30. define('PHP_PROCWATCH_MAX',             106);
  31. define('PHP_PROCWATCH_SUM',             107);
  32.  
  33. /** 
  34. * System_ProcWatch
  35. * Monitor processes
  36. * Usage:
  37. * <code>
  38. * $cf = System_ProcWatch_Config::fromXmlFile('/etc/procwatch.xml');
  39. * $pw = &new System_ProcWatch($cf);
  40. * $pw->run();
  41. * </code>
  42. @author       Michael Wallner <mike@php.net>
  43. @package      System_ProcWatch
  44. @category     System
  45. @version      $Revision: 304228 $
  46. @access       public
  47. */
  48. {
  49.     /**
  50.     * Parser
  51.     *
  52.     * @access   public
  53.     * @var      object System_ProcWatch_Parser 
  54.     */
  55.     var $parser = null;
  56.     
  57.     /**
  58.     * Patterns
  59.     *
  60.     * @access   private
  61.     * @var      array 
  62.     */
  63.     var $_patt = array();
  64.     
  65.     /**
  66.     * Conditions
  67.     *
  68.     * @access   private
  69.     * @var      array 
  70.     */
  71.     var $_cond = array();
  72.     
  73.     /**
  74.     * Executes
  75.     *
  76.     * @access   private
  77.     * @var      array 
  78.     */
  79.     var $_exec = array();
  80.     
  81.     /**
  82.     * Selected Processes
  83.     *
  84.     * @access   private
  85.     * @var      array 
  86.     */
  87.     var $_procs = array();
  88.     
  89.     /**
  90.     * Constructor
  91.     *
  92.     * @access   protected
  93.     * @return   object  System_ProcWatch 
  94.     * @param    array   $config     config array from System_ProcWatch_Config
  95.     */
  96.     function System_ProcWatch($config)
  97.     {
  98.         System_Procwatch::__construct($config);
  99.     }
  100.     
  101.     /**
  102.     * Constructor (ZE2)
  103.     *
  104.     * @access   protected
  105.     * @return   object      System_ProcWatch 
  106.     * @param    string      $config         path to configuration file
  107.     */
  108.     function __construct($config)
  109.     {
  110.         $this->parser = &new System_ProcWatch_Parser;
  111.         $this->setConfig($config);
  112.     }
  113.     
  114.     /**
  115.     * Run
  116.     *
  117.     * @access   public
  118.     * @return   void 
  119.     * @param    string  $ps_args    ps' args
  120.     */
  121.     function run($ps_args 'aux')
  122.     {
  123.         // get actual process' data
  124.         $this->_procs = array();
  125.         $procs &$this->parser->getParsedData($ps_argstrue);
  126.         
  127.         // fetch needed data
  128.         foreach ($this->_jobs as $job{
  129.         
  130.             // get the pattern to search for
  131.             $search array_shift(array_keys($this->_patt[$job]));
  132.             $pattern$this->_patt[$job][$search];
  133.             
  134.             foreach ($procs as $p{
  135.             
  136.                 // search for the line we need
  137.                 if (!preg_match($pattern@$p[$search])) {
  138.                     continue;
  139.                 }
  140.                 
  141.                 // save the data
  142.                 $this->_procs[$job][$p;
  143.             }
  144.  
  145.             // check for presence
  146.             if (isset($this->_cond[$job]['presence'])) {
  147.                 $this->_handlePresence($job);
  148.             }
  149.             
  150.             // check for attribute
  151.             if (isset($this->_cond[$job]['attr'])) {
  152.                 $this->_handleAttr($job);
  153.             }
  154.         }
  155.     }
  156.     
  157.     /**
  158.     * Daemon mode
  159.     *
  160.     * @access   public
  161.     * @return   void 
  162.     * @param    int     $interval   seconds to sleep
  163.     * @param    string  $ps_args    ps' arguments
  164.     */
  165.     function daemon($interval$ps_args 'aux')
  166.     {
  167.         while(true{
  168.             $this->run($ps_args);
  169.             sleep($interval);
  170.         }
  171.     }
  172.     
  173.     /**
  174.     * Handle presence
  175.     *
  176.     * @access   private
  177.     * @return   void 
  178.     * @param    string  $job 
  179.     */
  180.     function _handlePresence($job)
  181.     {
  182.         $presence   @count($this->_procs[$job]);
  183.         $condition  $this->_cond[$job]['presence'];
  184.         list($namearray_keys($this->_patt[$job]);
  185.  
  186.         if (!isset($condition['max']&& !isset($condition['min'])) {
  187.  
  188.             $this->_execute(
  189.                 $job
  190.                 $presence
  191.                 PHP_PROCWATCH_PRESENCE
  192.                 array('name' => $name)
  193.             );
  194.  
  195.         else {
  196.  
  197.             if (isset($condition['max']&& ($condition['max'$presence)) {
  198.                 $this->_execute(
  199.                     $job
  200.                     $presence
  201.                     PHP_PROCWATCH_PRESENCE_MAX
  202.                     array('name' => $name'max' => $condition['max'])
  203.                 );
  204.             }
  205.             
  206.             if (isset($condition['min']&& ($condition['min'$presence)) {
  207.                 $this->_execute(
  208.                     $job
  209.                     $presence
  210.                     PHP_PROCWATCH_PRESENCE_MIN,
  211.                     array('name' => $name'min' => $condition['min'])
  212.                 );
  213.             }
  214.         }
  215.         
  216.     }
  217.     
  218.     /**
  219.     * Handle attributes
  220.     *
  221.     * @access   private
  222.     * @return   void 
  223.     */
  224.     function _handleAttr($job)
  225.     {
  226.         $name array_shift(array_keys($this->_cond[$job]['attr']));
  227.         $attr $this->_cond[$job]['attr'][$name];
  228.         $proc = isset($this->_procs[$job]$this->_procs[$job: array();
  229.  
  230.         $attr['name'$name;
  231.  
  232.         // SUM
  233.         if (isset($attr['sum'])) {
  234.             $sum = 0.0;
  235.             foreach ($proc as $p{
  236.                 $sum  += @$p[$name];
  237.             }
  238.             if ($sum $attr['sum']{
  239.                 $this->_execute($job$sumPHP_PROCWATCH_SUM$attr);
  240.             }
  241.         
  242.         else {
  243.  
  244.             $sum = 0;
  245.             
  246.             // MAX
  247.             if (isset($attr['max'])) {
  248.                 $const PHP_PROCWATCH_MAX;
  249.                 foreach ($proc as $p{
  250.                     if ($p[$name$attr['max']{
  251.                         ++$sum;
  252.                     }
  253.                 }
  254.                 
  255.             // MIN
  256.             elseif (isset($attr['min'])) {
  257.                 $const PHP_PROCWATCH_MIN;
  258.                 foreach ($proc as $p{
  259.                     if ($p[$name$attr['min']{
  260.                         ++$sum;
  261.                     }
  262.                 }
  263.             
  264.             // IS
  265.             elseif (isset($attr['is'])) {
  266.                 $const PHP_PROCWATCH_IS;
  267.                 foreach ($proc as $p{
  268.                     if ($p[$name== $attr['is']{
  269.                         ++$sum;
  270.                     }
  271.                 }
  272.             
  273.             // ISNOT
  274.             elseif (isset($attr['isnot'])) {
  275.                 $const PHP_PROCWATCH_ISNOT;
  276.                 foreach ($proc as $p{
  277.                     if ($p[$name!= $attr['isnot']{
  278.                         ++$sum;
  279.                     }
  280.                 }
  281.             }
  282.             
  283.             if ($sum{
  284.                 $this->_execute($job$sum$const$attr);
  285.             }
  286.         }
  287.     }
  288.     
  289.     /**
  290.     * Execute
  291.     *
  292.     * @access   private
  293.     * @return   void 
  294.     * @param    string  $job 
  295.     * @param    mixed   $sum 
  296.     * @param    int     $mode 
  297.     * @param    mixed   $info 
  298.     */
  299.     function _execute($job$sum$mode$info = null)
  300.     {
  301.         $event_msg  $this->_getMsg($job$sum$mode$info);
  302.         $event_pids $this->_getPids($job);
  303.         $shell_exec @$this->_exec[$job]['shell'];
  304.         $php_exec   @$this->_exec[$job]['php'];
  305.         
  306.         
  307.         if (is_array($shell_exec)) {
  308.             foreach ($shell_exec as $e{
  309.                 shell_exec(
  310.                     str_replace(
  311.                         '$pids',
  312.                         $event_pids,
  313.                         str_replace('$msg'$event_msg$e)
  314.                     )
  315.                 );
  316.             }
  317.         }
  318.         
  319.         if (is_array($php_exec)) {
  320.             $procs 'unserialize(\'' serialize(@$this->_procs[$job]'\'';
  321.             foreach ($php_exec as $e{
  322.                 eval(
  323.                     str_replace(
  324.                         '$procs',
  325.                         $procs,
  326.                         str_replace(
  327.                             '$pids'
  328.                             $event_pids
  329.                             str_replace('$msg'$event_msg$e)
  330.                         )
  331.                     )
  332.                 );
  333.             }
  334.         }
  335.     }
  336.     
  337.     /**
  338.     * Get processes' PIDs of a certain job
  339.     *
  340.     * @access   public
  341.     * @return   string 
  342.     * @param    string  $job 
  343.     */
  344.     function _getPids($job)
  345.     {
  346.         $str '';
  347.         if (isset($this->_procs[$job])) {
  348.             foreach ($this->_procs[$jobas $proc{
  349.                 $str .= $proc['pid'', ';
  350.             }
  351.         }
  352.         return '\'(' trim($str', '')\'';
  353.     }
  354.     
  355.     /**
  356.     * Get alert message
  357.     *
  358.     * @access   private
  359.     * @return   string 
  360.     * @param    string  $job 
  361.     * @param    int     $c 
  362.     * @param    int     $mode 
  363.     * @param    array   $a 
  364.     */
  365.     function _getMsg($job$sum$mode$a)
  366.     {
  367.         $w $a['name'];
  368.         
  369.         switch($mode){
  370.  
  371.             case PHP_PROCWATCH_IS
  372.                 $is $a['is'];
  373.                 $m  = "$sum procs where $w is $is";
  374.                 break;
  375.  
  376.             case PHP_PROCWATCH_ISNOT
  377.                 $isnot  $a['isnot'];
  378.                 $m      = "$sum procs where $w is not $isnot";
  379.                 break;
  380.  
  381.             case PHP_PROCWATCH_MAX
  382.                 $max $a['max'];
  383.                 $m   = "$sum procs where $w exceeds $max";
  384.                 break;
  385.  
  386.             case PHP_PROCWATCH_MIN
  387.                 $min $a['min'];
  388.                 $m   = "$sum procs where $w under-runs $min";
  389.                 break;
  390.  
  391.             case PHP_PROCWATCH_PRESENCE
  392.                 $match  $this->_patt[$job][$w];
  393.                 $m      = "$sum procs where $w matches $match";
  394.                 break;
  395.                 
  396.             case PHP_PROCWATCH_PRESENCE_MIN
  397.                 $min    $a['min'];
  398.                 $match  $this->_patt[$job][$w];
  399.                 $m      = "$sum (min $min) procs where $w matches $match";
  400.                 break;
  401.  
  402.             case PHP_PROCWATCH_PRESENCE_MAX
  403.                 $max    $a['max'];
  404.                 $match  $this->_patt[$job][$w];
  405.                 $m      = "$sum (max $max) procs where $w matches $match";
  406.                 break;
  407.                 
  408.             case PHP_PROCWATCH_SUM
  409.                 $s  $a['sum'];
  410.                 $c  count($this->_procs[$job]);
  411.                 $m  = "$c procs which sum of $w ($sum) exceeds $s";
  412.                 break;
  413.         }
  414.  
  415.         return '\'' date('r'' - '$job ': Found ' 
  416.                 str_replace('\'''\\\''$m'\'';
  417.     }
  418.     
  419.     
  420.     /**
  421.     * Set configuration
  422.     *
  423.     * @access   public
  424.     * @return   void 
  425.     * @param    array   $config     config array from System_ProcWatch_Config
  426.     */
  427.     function setConfig($config)
  428.     {
  429.         $this->_jobs = array();
  430.         $this->addConfig($config);
  431.     }
  432.     
  433.     /**
  434.     * Add configuration
  435.     *
  436.     * @access   public
  437.     * @return   void 
  438.     * @param    array   $config     config array from System_ProcWatch_Config
  439.     */
  440.     function addConfig($config)
  441.     {
  442.         foreach ($config as $job => $arrays{
  443.             $this->_jobs[]      $job;
  444.             $this->_patt[$job]  $arrays['pattern'];
  445.             $this->_cond[$job]  $arrays['condition'];
  446.             $this->_exec[$job]  $arrays['execute'];
  447.         }
  448.     }
  449. }
  450. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.