In the ongoing project, you need to use php to obtain the Linux Server Status. du Niang finds that most of them are based on the WIN System. well, we are self-reliant, I would like to share with you a piece of simple Linux code: php code for getting the linux server status, not to mention-go directly to the function:
The code is as follows:
Function get_used_status (){
$ Fp = popen ('top-B-n 2 | grep-E "^ (Cpu | Mem | Tasks)" ', "r "); // Obtain the cpu and memory usage of the system at a specific time point
$ Rs = "";
While (! Feof ($ fp )){
$ Rs. = fread ($ fp, 1024 );
}
Pclose ($ fp );
$ Sys_info = explode ("\ n", $ rs );
$ Tast_info = explode (",", $ sys_info [3]); // process array
$ Cpu_info = explode (",", $ sys_info [4]); // array of CPU usage
$ Mem_info = explode (",", $ sys_info [5]); // array of memory shares
// Number of running processes
$ Tast_running = trim ($ tast_info [1], 'running '));
// CPU usage
$ Cpu_usage = trim ($ cpu_info [0], 'CPU (s): '),' % us'); // percentage
// Memory usage
$ Mem_total = trim ($ mem_info [0], 'mem: '), 'K total ');
$ Mem_used = trim ($ mem_info [1], 'K used ');
$ Mem_usage = round (100 * intval ($ mem_used)/intval ($ mem_total), 2); // percentage
/* Disk usage begin */
$ Fp = popen ('df-lh | grep-E "^ (/)" ', "r ");
$ Rs = fread ($ fp, 1024 );
Pclose ($ fp );
$ Rs = preg_replace ("/\ s {2,}/", '', $ rs); // replace multiple spaces with" _"
$ Hd = explode ("", $ rs );
$ Hd_avail = trim ($ hd [3], 'g'); // disk available space size unit: G
$ Hd_usage = trim ($ hd [4], '%'); // percentage of Mount points
// Print_r ($ hd );
/* Hard disk usage end */
// Detection time
$ Fp = popen ("date + \" % Y-% m-% d % H: % M \ "", "r ");
$ Rs = fread ($ fp, 1024 );
Pclose ($ fp );
$ Detection_time = trim ($ rs );
/* Obtain the IP address begin */
/*
$ Fp = popen ('ifconfig eth0 | grep-E "(inet addr)" ', 'r ');
$ Rs = fread ($ fp, 1024 );
Pclose ($ fp );
$ Rs = preg_replace ("/\ s {2,}/", '', trim ($ rs); // replace multiple spaces with" _"
$ Rs = explode ("", $ rs );
$ Ip = trim ($ rs [1], 'addr :');
*/
/* Get the IP address end */
/*
$ File_name = "/tmp/data.txt"; // absolute path: homedata. dat
$ File_pointer = fopen ($ file_name, "a +"); // "w" is a mode. For more information, see
Fwrite ($ file_pointer, $ ip); // first cut the file to 0 bytes, and then write
Fclose ($ file_pointer); // end
*/
Return array ('CPU _ usage' => $ cpu_usage, 'mem _ usage' => $ mem_usage, 'HD _ avail '=> $ hd_avail, 'HD _ usage' => $ hd_usage, 'tast _ running '=> $ tast_running, 'Detection _ time' => $ detection_time );
}