Php code for obtaining the linux server status

Source: Internet
Author: User
Php code for obtaining the linux server status

Example: php code used to obtain the status of a linux server.

  1. Function get_used_status (){

  2. $ 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
  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]); // array of CPU usage
  11. $ Mem_info = explode (",", $ sys_info [5]); // array of memory shares
  12. // Number of running processes
  13. $ Tast_running = trim ($ tast_info [1], 'running '));
  14. // CPU usage
  15. $ Cpu_usage = trim ($ cpu_info [0], 'CPU (s): '),' % us'); // percentage

  16. // Memory usage

  17. $ Mem_total = 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); // percentage
  20. /* 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], '%'); // percentage of Mount points
  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. /* Obtain the 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 the IP address end */
  45. /*
  46. $ File_name = "/tmp/data.txt"; // absolute path: homedata. dat
  47. $ File_pointer = fopen ($ file_name, "a +"); // "w" is a mode. For more information, see
  48. Fwrite ($ file_pointer, $ ip); // first cut the file to 0 bytes, and 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' => $ hd_usage, 'tast _ running '=> $ tast_running, 'Detection _ time' => $ detection_time );

  52. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.