public function top($parse = true)
    {
        if (!file_exists($this->tmp_path.'/system')) {
            mkdir($this->tmp_path.'/system', 0755, true);
        }
        shell_exec('top -n 1 -b > '.$this->tmp_path.'/system/top-output');
        usleep(25000);
        $result = trim(file_get_contents($this->tmp_path.'/system/top-output'));

        if ($parse) {
            $lines = explode(PHP_EOL, $result);

            // detect start by empty line space
            $start = 0;
            foreach ($lines as $no => $line) {
                if (trim($line) == '') {
                    $start = $no;
                    break;
                }
            }
            // filter out header lines
            foreach (range(0, $start) as $key) {
                unset($lines[$key]);
            }

            //remove column line
            unset($lines[$start+1]);

            // define columns
            $columns = [
                'PID',
                'USER',
                'PR',
                'NI',
                'VIRT',
                'RES',
                'SHR',
                'S',
                '%CPU',
                '%MEM',
                'TIME+',
                'COMMAND'
            ];

            // match rows for columns and set into return
            $result = [];
            foreach ($lines as $row => $line) {
                $column = array_values(array_filter(explode(' ', $line), 'strlen'));
                foreach ($columns as $col => $key) {
                    $result[$row][$key] = @$column[$col];
                }
            }
            $result = array_values($result);
        }
        return $result;
    }
public function top($parse = true)
    {
        if (!file_exists($this->tmp_path.'/system')) {
            mkdir($this->tmp_path.'/system', 0755, true);
        }
        shell_exec('top -n 1 -b > '.$this->tmp_path.'/system/top-output');
        usleep(25000);
        $result = trim(file_get_contents($this->tmp_path.'/system/top-output'));

        if ($parse) {
            $lines = explode(PHP_EOL, $result);

            // detect start by empty line space
            $start = 0;
            foreach ($lines as $no => $line) {
                if (trim($line) == '') {
                    $start = $no;
                    break;
                }
            }
            // filter out header lines
            foreach (range(0, $start) as $key) {
                unset($lines[$key]);
            }

            //remove column line
            unset($lines[$start+1]);

            // define columns
            $columns = [
                'PID',
                'USER',
                'PR',
                'NI',
                'VIRT',
                'RES',
                'SHR',
                'S',
                '%CPU',
                '%MEM',
                'TIME+',
                'COMMAND'
            ];

            // match rows for columns and set into return
            $result = [];
            foreach ($lines as $row => $line) {
                $column = array_values(array_filter(explode(' ', $line), 'strlen'));
                foreach ($columns as $col => $key) {
                    $result[$row][$key] = @$column[$col];
                }
            }
            $result = array_values($result);
        }
        return $result;
    }
public function top ( $ parse = true ) { if ( ! file_exists ( $ this -> tmp_path . '/system' ) ) { mkdir ( $ this -> tmp_path . '/system' , 0755 , true ) ; } shell_exec ( 'top -n 1 -b > ' . $ this -> tmp_path . '/system/top-output' ) ; usleep ( 25000 ) ; $ result = trim ( file_get_contents ( $ this -> tmp_path . '/system/top-output' ) ) ; if ( $ parse ) { $ lines = explode ( PHP_EOL , $ result ) ; // detect start by empty line space $ start = 0 ; foreach ( $ lines as $ no => $ line ) { if ( trim ( $ line ) == '' ) { $ start = $ no ; break ; } } // filter out header lines foreach ( range ( 0 , $ start ) as $ key ) { unset ( $ lines [ $ key ] ) ; } //remove column line unset ( $ lines [ $ start + 1 ] ) ; // define columns $ columns = [ 'PID' , 'USER' , 'PR' , 'NI' , 'VIRT' , 'RES' , 'SHR' , 'S' , '%CPU' , '%MEM' , 'TIME+' , 'COMMAND' ] ; // match rows for columns and set into return $ result = [ ] ; foreach ( $ lines as $ row => $ line ) { $ column = array_values ( array_filter ( explode ( ' ' , $ line ) , 'strlen' ) ) ; foreach ( $ columns as $ col => $ key ) { $ result [ $ row ] [ $ key ] = @ $ column [ $ col ] ; } } $ result = array_values ( $ result ) ; } return $ result ; }
Get system top output

@param string
Get system top output