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

Source for file Parser.php

Documentation is available at Parser.php

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

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