PHP gets the Linux server status code

Source: Internet
Author: User

Example, PHP gets the Linux server status code.

  1. function Get_used_status () {

  2. $fp = Popen (' top-b-N 2 | grep-e "^ (cpu| mem| Tasks) "'," "R");//Get a moment of system CPU and memory usage
  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 occupancy
  11. $mem _info = Explode (",", $sys _info[5]); Memory Occupancy Array
  12. Number of processes that are running
  13. $tast _running = Trim (Trim ($tast _info[1], ' running '));
  14. CPU occupancy
  15. $cpu _usage = Trim (Trim ($cpu _info[0], ' CPU (s): '), '%us '); Percentage

  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); Percentage
  20. /* Hard Drive utilization 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 free space Size Unit g
  27. $hd _usage = Trim ($HD [4], '% '); mount point Percentage
  28. Print_r ($HD);
  29. /* Hard Drive utilization 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 address end*/
  45. /*
  46. $file _name = "/tmp/data.txt"; Absolute path: Homedata.dat
  47. $file _pointer = fopen ($file _name, "A +"); "W" is a pattern, see later
  48. Fwrite ($file _pointer, $IP); First cut the file to a size of 0 bytes, then write
  49. Fclose ($file _pointer); End
  50. */

  51. 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);

  52. }

Copy Code
  • 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.