home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / ProcWatch.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  12.0 KB  |  455 lines

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