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 / Parser.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  4.3 KB  |  169 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: ProcWatch :: Parser                                |
  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: Parser.php,v 1.5 2004/06/12 13:40:14 mike Exp $
  15.  
  16. /**
  17. * System_ProcWatch_Parser
  18. * Fetches output from `ps` and parses it into an associative array
  19. * Usage:
  20. * <code>
  21. * $ps = &new System_ProcWatch_Parser();
  22. * $pd = &$ps->getParsedData();
  23. * </code>
  24. * @author       Michael Wallner <mike@php.net>
  25. * @package      System_ProcWatch
  26. * @category     System
  27. *
  28. * @version      $Revision: 1.5 $
  29. * @access       public
  30. */
  31. class System_ProcWatch_Parser
  32. {
  33.     /**
  34.     * ps' args
  35.     *
  36.     * @access   private
  37.     * @var      string
  38.     */
  39.     var $_args = 'aux';
  40.     
  41.     /**
  42.     * Processes
  43.     *
  44.     * @access   private
  45.     * @var      array
  46.     */
  47.     var $_procs = array();
  48.     
  49.     /**
  50.     * Constructor
  51.     *
  52.     * @access   protected
  53.     * @return   object  System_ProcWatch_Parser
  54.     * @param    string  $ps_args    ps' args
  55.     */
  56.     function System_ProcWatch_Parser($ps_args = 'aux')
  57.     {
  58.         System_ProcWatch_Parser::__construct($ps_args);
  59.     }
  60.     
  61.     /**
  62.     * Constructor (ZE2)
  63.     *
  64.     * @access   protected
  65.     * @return   object  System_ProcWatch_Parser
  66.     * @param    string  $ps_args    ps' args
  67.     */
  68.     function __construct($ps_args = 'aux')
  69.     {
  70.         $this->_args = $ps_args;
  71.     }
  72.     
  73.     /**
  74.     * Fetch ps' data
  75.     *
  76.     * @access   public
  77.     * @return   string  ps' output
  78.     * @param    string  $ps_args    ps' args
  79.     */
  80.     function fetch($ps_args = '')
  81.     {
  82.         if (empty($ps_args)) {
  83.             $ps_args = $this->_args;
  84.         }
  85.         putenv('COLUMNS=1000');
  86.         return shell_exec("ps $ps_args");
  87.     }
  88.     
  89.     /**
  90.     * Parse
  91.     *
  92.     * @access   public
  93.     * @return   array
  94.     * @param    string  $data
  95.     */
  96.     function &parse($data)
  97.     {
  98.         $lines = explode("\n", trim($data));
  99.         $heads = preg_split('/\s+/', strToLower(trim(array_shift($lines))));
  100.         $count = count($heads);
  101.         $procs = array();
  102.  
  103.         foreach($lines as $i => $line){
  104.             $parts = preg_split('/\s+/', trim($line), $count);
  105.             foreach ($heads as $j => $head) {
  106.                 $procs[$i][$head] = str_replace('"', '\"', $parts[$j]);
  107.             }
  108.         }
  109.  
  110.         return $procs;        
  111.     }
  112.     
  113.     /**
  114.     * Get parsed data
  115.     *
  116.     * @access   public
  117.     * @return   array
  118.     * @param    string  $ps_args    ps' arguments
  119.     * @param    bool    $refresh    whether to refresh our data
  120.     */
  121.     function &getParsedData($ps_args = 'aux', $refresh = false)
  122.     {
  123.         if ($refresh || empty($this->_procs)) {
  124.             $this->_procs = &$this->parse($this->fetch($ps_args));
  125.         }
  126.         
  127.         return $this->_procs;
  128.     }
  129.  
  130.     /**
  131.     * Get info about a process by its PID
  132.     *
  133.     * @access   public
  134.     * @return   array
  135.     * @param    int     $pid    the PID of the process
  136.     */
  137.     function getProcByPid($pid)
  138.     {
  139.         foreach ($this->getParsedData() as $proc) {
  140.             if ($proc['pid'] == $pid) {
  141.                 return $proc;
  142.             }
  143.         }
  144.         return array();
  145.     }
  146.  
  147.     /**
  148.     * Get information about processes
  149.     *
  150.     * @access   public
  151.     * @return   array
  152.     * @param    string  $pattern    PCRE to match for process
  153.     * @param    string  $search     the ps field to search for
  154.     */
  155.     function getProcInfo($pattern, $search)
  156.     {
  157.         $result = array();
  158.         foreach ($this->getParsedData() as $p) {
  159.             if (preg_match($pattern, @$p[$search])) {
  160.                 $result[] = $p;
  161.             }
  162.         }
  163.         return $result;
  164.     }    
  165. }
  166. ?>