Example, PHP gets the Linux server status code.
function Get_used_status () {
- $fp = Popen (' top-b-N 2 | grep-e "^ (cpu| mem| Tasks) "'," "R");//Get a moment of system CPU and memory usage
- $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 occupancy
- $mem _info = Explode (",", $sys _info[5]); Memory Occupancy Array
- Number of processes that are running
- $tast _running = Trim (Trim ($tast _info[1], ' running '));
- CPU occupancy
- $cpu _usage = Trim (Trim ($cpu _info[0], ' CPU (s): '), '%us '); Percentage
Memory occupancy
- $mem _total = Trim (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
- /* Hard Drive utilization 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 free space Size Unit g
- $hd _usage = Trim ($HD [4], '% '); mount point Percentage
- Print_r ($HD);
- /* Hard Drive utilization end*/
Detection time
- $fp = Popen ("date +\"%y-%m-%d%h:%m\ "", "R");
- $rs = Fread ($fp, 1024);
- Pclose ($FP);
- $detection _time = Trim ($RS);
/* Get 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 IP address end*/
- /*
- $file _name = "/tmp/data.txt"; Absolute path: Homedata.dat
- $file _pointer = fopen ($file _name, "A +"); "W" is a pattern, see later
- Fwrite ($file _pointer, $IP); First cut the file to a size of 0 bytes, then write
- Fclose ($file _pointer); End
- */
The return array (' cpu_usage ' = = $cpu _usage, ' mem_usage ' and ' _usage ', ' hd_avail ' = $hd _avail, ' hd_usage ' = $hd _ Usage, ' tast_running ' = $tast _running, ' detection_time ' = $detection _time);
- }
Copy Code |