home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / procwatch < prev    next >
Text File  |  2008-07-02  |  6KB  |  251 lines

  1. #!H:\release167\NEU\xampp\php\.\php.exe
  2. <?php
  3. // +----------------------------------------------------------------------+
  4. // | procwatch                                                            |
  5. // +----------------------------------------------------------------------+
  6. // | This source file is subject to version 3.0 of the PHP license,       |
  7. // | that is available at http://www.php.net/license/3_0.txt              |
  8. // | If you did not receive a copy of the PHP license and are unable      |
  9. // | to obtain it through the world-wide-web, please send a note to       |
  10. // | license@php.net so we can mail you a copy immediately.               |
  11. // +----------------------------------------------------------------------+
  12. // | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at>             |
  13. // +----------------------------------------------------------------------+
  14. //
  15. // $Id: system-procwatch.php,v 1.4 2004/03/21 17:21:41 mike Exp $
  16.  
  17. /**
  18. * Shell script for System_ProcWatch
  19. * @author       Michael Wallner <mike@php.net>
  20. * @link         http://pear.php.net/package/System_ProcWatch
  21. * @package      System_ProcWatch
  22. * @category     System
  23. * @version      $Revision: 1.4 $
  24. */
  25.  
  26. /**
  27. * Requires System::ProcWatch
  28. */
  29. require_once('System/ProcWatch.php');
  30.  
  31. /**
  32. * Requires System::ProcWatch::Config
  33. */
  34. require_once('System/ProcWatch/Config.php');
  35.  
  36. /**
  37. * Requires Console::Getopt
  38. */
  39. require_once('Console/Getopt.php');
  40.  
  41. /**
  42. * Set error handling
  43. */
  44. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, '_show_usage');
  45.  
  46. /**
  47. * Command line options
  48. */
  49. $options = array(
  50.     'x:'    => 'xml=',
  51.     'i:'    => 'ini=',
  52.     'h'     => 'help',
  53.     'd'     => 'daemon',
  54.     's:'    => 'sleep=',
  55.     'a:'    => 'args=',
  56.     'p:'    => 'php=',
  57. );
  58.  
  59. $arglist = Console_Getopt::readPHPArgv();
  60. $srcname = array_shift($arglist);
  61.  
  62. /**
  63. * Get command line arguments
  64. */
  65. $cl_args = _prepare_args(
  66.     array_shift(
  67.         Console_Getopt::getopt(
  68.             $arglist,                           // ARGV
  69.             implode('', array_keys($options)),  // short options
  70.             array_values($options)              // long options
  71.         )
  72.     )
  73. );
  74.  
  75. /**
  76. * Show usage if:
  77. *   o requested with -h or --help
  78. *   o one of -x, -i, --xml or --ini was not specified
  79. */
  80. if (
  81.     isset($cl_args['h'])    ||
  82.     isset($cl_args['help']) ||
  83.     (
  84.         (!isset($cl_args['x']) && !isset($cl_args['xml'])) &&
  85.         (!isset($cl_args['i']) && !isset($cl_args['ini']))
  86.     )
  87. )
  88. {
  89.     _show_usage();
  90. }
  91.  
  92. /**
  93. * Get path to config file
  94. */
  95. $xmlfile =  (isset($cl_args['x'])   ? $cl_args['x']     : 
  96.             (isset($cl_args['xml']) ? $cl_args['xml']   : false));
  97.  
  98. $inifile =  (isset($cl_args['i'])   ? $cl_args['i']     :
  99.             (isset($cl_args['ini']) ? $cl_args['ini']   : false));
  100.  
  101. /**
  102. * Get ps' args if set
  103. */
  104. $ps_args =  (isset($cl_args['a'])       ? $cl_args['a'] :
  105.             (isset($cl_args['args'])    ? $cl_args['args'] : 'aux'));
  106.  
  107. /**
  108. * Include a PHP script if wanted
  109. */
  110. $php_inc =  (isset($cl_args['p'])   ? $cl_args['p'] :
  111.             (isset($cl_args['php']) ? $cl_args['php'] : false));
  112.  
  113. if ($php_inc) {
  114.  
  115.     /**
  116.     * DONT USE THE FOLLOWING PHP VARIABLES
  117.     * 
  118.     *   $procwatch
  119.     *   $interval
  120.     *   $ps_args
  121.     *   $cl_args
  122.     *   $xmlfile
  123.     *   $srcname
  124.     *   $arglist
  125.     *   $options
  126.     * 
  127.     * unless you KNOW what you're doing!
  128.     */
  129.  
  130.     if (!@include_once($php_inc)) {
  131.         PEAR::raiseError("File '$php_inc' doesn't exist");
  132.         // exit
  133.     }
  134. }
  135.  
  136. /**
  137. * Show usage with error message if path to config file is invalid
  138. * PEAR::raiseError() will call _show_usage() and exit
  139. */
  140. if ($xmlfile) {
  141.  
  142.     if (is_file($xmlfile)) {
  143.         $config = System_ProcWatch_Config::fromXmlFile($xmlfile);
  144.     } else {
  145.         PEAR::raiseError("File '$xmlfile' doesn't exist");
  146.         // exit
  147.     }
  148.  
  149. } elseif ($inifile) {
  150.  
  151.     if (is_file($inifile)) {
  152.         $config = System_ProcWatch_Config::fromIniFile($inifile);
  153.     } else {
  154.         PEAR::raiseError("File '$inifile' doesn't exist");
  155.         // exit
  156.     }
  157.  
  158. }
  159.  
  160. /**
  161. * Finally init System_ProcWatch
  162. */
  163. $procwatch = &new System_ProcWatch($config);
  164.  
  165. /**
  166. * Now run either once or in daemon mode
  167. */
  168. if (isset($cl_args['d']) || isset($cl_args['daemon'])) {
  169.  
  170.     // run in daemon mode
  171.     $interval = (int)   (isset($cl_args['s']) ? $cl_args['s'] : 
  172.                         (isset($cl_args['sleep']) ? $cl_args['sleep'] :
  173.                         1800)); // default = 30 min
  174.  
  175.     $procwatch->daemon($interval, $ps_args);
  176.  
  177. } else {
  178.  
  179.     // run once
  180.     $procwatch->run($ps_args);
  181.  
  182. }
  183.  
  184. // ------------------ script end ------------------ //
  185.  
  186. /**
  187. * Prepare args array from Console_Getopt::getopt()
  188. * @return   array
  189. * @param    array
  190. */
  191. function _prepare_args($array)
  192. {
  193.     $args = array();
  194.     foreach ($array as $option) {
  195.         $key        = preg_replace('/^-+/', '', $option[0]);
  196.         $args[$key] = empty($option[1]) ?  1 : $option[1];
  197.     }
  198.     return $args;
  199. }
  200.  
  201. /**
  202. * Function which shows usage of this script
  203. * @return   void
  204. * @param    object PEAR_Error
  205. */
  206. function _show_usage($error = null)
  207. {
  208.     if (isset($error)) {
  209.  
  210.         fputs(STDERR, "\nError: " . $error->getMessage() . "\n\n");
  211.         exit(-1);
  212.  
  213.     } else {
  214.  
  215.         echo <<<_SHOW_USAGE
  216. #
  217. # procwatch v0.4.2, 2004-06-12 by Michael Wallner <mike@php.net>
  218. # For further information visit http://pear.php.net/package/System_ProcWatch
  219. #
  220.  
  221. USAGE:
  222. \$ {$GLOBALS['srcname']} (-x|-i) <file> [-d [-s <sec>]] [-a <args>] [-p <file>]
  223.  
  224. OPTIONS:
  225.     -x | --xml=         path to XML configuration file
  226.     -i | --ini=         path to INI configuration file
  227.  
  228.     -d | --daemon       run procwatch in daemon mode
  229.     -s | --sleep=       seconds to sleep in daemon mode (default=1800)
  230.  
  231.     -a | --args=        arguments that should be passed to ps (default=aux)
  232.     -p | --php=         php file that should be included
  233.  
  234.     -h | --help         this help message
  235.  
  236. EXAMPLE:
  237.     \$ {$GLOBALS['srcname']} -x /etc/procwatch.xml -d -s 3600
  238.  
  239.     This command will run procwatch in daemon mode with an interval
  240.     of an hour using the configuration file '/etc/procwatch.xml'
  241.  
  242. _SHOW_USAGE;
  243.     
  244.         exit(0);
  245.  
  246.     }
  247. }
  248. ?>