Linux, PHP getting server status CPU, MEM usage, disk usage, IP address acquisition, MAC address and other information to obtain records

Acquiring server status record is used to obtain an output manner SHELL command string is then split processing results and other ways to obtain useful information themselves

Obtaining posted and commonly used treatment

? < The PHP
     $ FP = popen ( 'execute SHELL command', "R & lt" );
     $ RS = '' ;
     the while (! Feof ( $ FP )) {
        $ RS . = Fread ( $ FP , 1024 );   
    } 
    pclose ( FP $ );
     // acquired output RS 
    $ info = the explode ( "\ n-", $ RS ); // handling a wrap split 
    $ INF02 = the explode ( ",", $ info [0]); / / treatment comma 2 split 
    $ Info3= TRIM ( $ INF02 [0], '% Cpu (S)'); // handled about 3 to remove the string 
    $ Info4 = preg_replace ( '/ \ S {2,} /', $ RS ); // process embodiment 4 Alternatively string 
    
    // the information they need to process a different combination process

Processing the command list records

// Get CPU usage and memory usage 
$ FP = popen ( '-b Top -n 2 | grep -E "(Cpu \ (S \)) | (KiB Mem)"', "R & lt" );
 / * Description: get the information twice, once because only acquire data is not accurate, but slow returns cause, it is recommended to make asynchronous processing 
approach 
     $ the SYS iNFO = the explode ( "\ the n-", $ rs); 
    $ = The cpu_info the explode ( ",", SYS_INFO $ [2]); 
    $ cpu_usage = TRIM (TRIM ($ The cpu_info [0], '% Cpu (S):'), 'US'); // percentage 
    
       $ mem_info = explode ( "," , $ sys_info [ 3]); // amount of memory array 
     $ mem_total = TRIM (TRIM ($ mem_info [0], 'KiB Mem:'), 'Total'); 
    $ mem_used = TRIM (TRIM ($ mem_info [2], 'Used ')); 
    $ mem_usage = round (the intval 100 * ($ mem_used) / the intval ($ mem_total), 2); // percentage 
* /
 
// get disk usage 
$ fp = popen ( 'df -LH | grep -E "^ (/)"', "r" );
     $ rs = fread ( $ fp , 1024 );
     pclose ( $ fp );
     $ RS = preg_replace ( '/ \ S {2,} /', '', $ RS );   // put into a plurality of spaces "_" 
    $ HD = the explode ( "", $ RS ); 

    $ hd_avail = TRIM ( HD $ [. 3], 'G'); // disk free space units G 
    $ hd_usage = TRIM ( $ HD [. 4], '%'); // mount point percentage
//获取MAC地址
 $fp = popen('lshw -c network | grep serial | head -n 1', "r");
    $rs = fread($fp, 1024);
    pclose($fp);
    $mac = explode(':', $rs);
    array_shift($mac);
   $macAddr = implode(':', $mac); //XX:XX:XX:XX:XX
// get the internal IP address 
 $ fp = popen ( 'ip addr | grep inet | grep -v inet6', "r" ); 

/ * * 
    The results show: own processing 
    inet 192.168.1.180/24 brd 192.168.1.255 scope global Dynamic 
    inet 172.20.175.225/28 brd 172.20.175.239 scope, Ltd. Free Join Dynamic 
    inet 127.0.0.1/8 brd 127.255.255.255 scope, Ltd. Free Join Dynamic 
* * /

 

Guess you like

Origin www.cnblogs.com/Gasg/p/10948580.html