Home > Backend Development > PHP Tutorial > PHP code to get linux server status

PHP code to get linux server status

WBOY
Release: 2016-07-25 09:11:59
Original
1023 people have browsed it

Example, PHP code to get Linux server status.

  1. function get_used_status(){

  2. $fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r"); //Get the system cpu and memory usage at a certain moment
  3. $rs = "";
  4. while(!feof($fp)){
  5. $rs .= fread($fp,1024);
  6. }
  7. pclose($fp );
  8. $sys_info = explode("n",$rs);
  9. $tast_info = explode(",",$sys_info[3]);//Process array
  10. $cpu_info = explode(",",$sys_info[ 4]); //CPU occupancy array
  11. $mem_info = explode(",",$sys_info[5]); //Memory occupancy array
  12. //Number of running processes
  13. $tast_running = trim(trim($ taste_info[1],'running'));
  14. //CPU usage
  15. $cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //Percentage< ;/p>
  16. //Memory occupancy

  17. $mem_total = trim(trim($mem_info[0],'Mem: '),'k total');
  18. $mem_used = trim($mem_info[1 ],'k used');
  19. $mem_usage = round(100*intval($mem_used)/intval($mem_total),2); //Percent
  20. /*hard disk usage begin*/
  21. $fp = popen( 'df -lh | grep -E "^(/)"',"r");
  22. $rs = fread($fp,1024);
  23. pclose($fp);
  24. $rs = preg_replace("/s{ 2,}/",' ',$rs); //Replace multiple spaces with "_"
  25. $hd = explode(" ",$rs);
  26. $hd_avail = trim($hd[3],' G'); //Disk available space size unit G
  27. $hd_usage = trim($hd[4],'%'); //Mount point percentage
  28. //print_r($hd);
  29. /*Hard disk usage end*/

  30. //Detection time

  31. $fp = popen("date +"%Y-%m-%d %H:%M"","r");
  32. $ rs = fread($fp,1024);
  33. pclose($fp);
  34. $detection_time = trim($rs);

  35. /*Get IP address begin*/

  36. /*
  37. $ fp = popen('ifconfig eth0 | grep -E "(inet addr)"','r');
  38. $rs = fread($fp,1024);
  39. pclose($fp);
  40. $rs = preg_replace(" /s{2,}/",' ',trim($rs)); //Replace multiple spaces with "_"
  41. $rs = explode(" ",$rs);
  42. $ip = trim($ rs[1],'addr:');
  43. */
  44. /*Get IP addressend*/
  45. /*
  46. $file_name = "/tmp/data.txt"; // Absolute path: homedata.dat
  47. $file_pointer = fopen($file_name, "a+"); // "w" is a mode, see below for details
  48. fwrite($file_pointer,$ip); // First cut the file to 0 bytes, then write
  49. fclose($file_pointer); // End
  50. */

  51. return array('cpu_usage'=>$cpu_usage,'mem_usage'=>$mem_usage,'hd_avail'=> $hd_avail,'hd_usage'=>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template