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

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