PHP get CPU and Memory usage code example

Source: Internet
Author: User
  1. /*

  2. @ system parameter probing LINUX
  3. @Edit bbs.it-home.org
  4. @date 2013/5/8
  5. */
  6. function Sys_linux ()
  7. {
  8. Cpu
  9. if (false = = = ($str = @file ("/proc/cpuinfo"))) return false;
  10. $str = Implode ("", $str);
  11. @preg_match_all ("/model\s+name\s{0,}\:+\s{0,} ([\w\s\) \ (.] +) [\r\n]+/, $str, $model);
  12. @preg_match_all ("/cpu\s+mhz\s{0,}\:+\s{0,} ([\d\.] +) [\r\n]+/, $str, $mhz);
  13. @preg_match_all ("/cache\s+size\s{0,}\:+\s{0,} ([\d\.] +\s{0,}[a-z]+[\r\n]+)/", $str, $cache);
  14. if (False!== Is_array ($model [1]))
  15. {
  16. $res [' CPU '] [' num '] = sizeof ($model [1]);
  17. for ($i = 0; $i < $res [' CPU '] [' num ']; $i + +)
  18. {
  19. $res [' CPU '] [' detail '] [] = ' type: '. $model [1][$i]. "Cache:". $cache [1][$i];
  20. }
  21. if (False!== Is_array ($res [' CPU '] [' detail ']) $res [' CPU '] [' detail '] = implode ("
  22. ", $res [' CPU '] [' detail ']);
  23. }

  24. UPTIME

  25. if (false = = = ($str = @file ("/proc/uptime"))) return false;
  26. $str = Explode ("", Implode ("", $str));
  27. $str = Trim ($str [0]);
  28. $min = $STR/60;
  29. $hours = $min/60;
  30. $days = Floor ($hours/24);
  31. $hours = Floor ($hours – ($days * 24));
  32. $min = Floor ($min – ($days * 60 * 24) – ($hours * 60));
  33. if ($days!== 0) $res [' uptime '] = $days. " Days ";
  34. if ($hours!== 0) $res [' uptime ']. = $hours. " Hours ";
  35. $res [' uptime ']. = $min. " Minutes ";

  36. MEMORY

  37. if (false = = = ($str = @file ("/proc/meminfo"))) return false;
  38. $str = Implode ("", $str);
  39. Preg_match_all ("/memtotal\s{0,}\:+\s{0,} ([\d\.] +).+? Memfree\s{0,}\:+\s{0,} ([\d\.] +).+? Swaptotal\s{0,}\:+\s{0,} ([\d\.] +).+? Swapfree\s{0,}\:+\s{0,} ([\d\.] +)/S ", $str, $buf);

  40. $res [' memtotal '] = round ($buf [1][0]/1024, 2);

  41. $res [' memfree '] = round ($buf [2][0]/1024, 2);
  42. $res [' memused '] = ($res [' memtotal ']-$res [' memfree ']);
  43. $res [' mempercent '] = (floatval ($res [' memtotal '])!=0)? Round (($res [' memused ']/$res [' Memtotal ']): 0;

  44. $res [' swaptotal '] = round ($buf [3][0]/1024, 2);

  45. $res [' swapfree '] = round ($buf [4][0]/1024, 2);
  46. $res [' swapused '] = ($res [' swaptotal ']-$res [' swapfree ']);
  47. $res [' swappercent '] = (floatval ($res [' swaptotal '])!=0)? Round (($res [' swapused ']/$res [' Swaptotal ']): 0;

  48. LOAD AVG

  49. if (false = = = ($str = @file ("/proc/loadavg"))) return false;
  50. $str = Explode ("", Implode ("", $str));
  51. $str = Array_chunk ($str, 3);
  52. $res [' loadavg '] = Implode ("", $str [0]);

  53. return $res;

  54. }
  55. /*
  56. System parameter Detection FreeBSD
  57. */
  58. function Sys_freebsd ()
  59. {
  60. Cpu
  61. if (false = = = ($res [' CPU '] [' num '] = Get_key ("Hw.ncpu")) return false;
  62. $res [' CPU '] [' detail '] = Get_key ("Hw.model");

  63. LOAD AVG

  64. if (false = = = ($res [' loadavg '] = Get_key ("Vm.loadavg")) return false;
  65. $res [' loadavg '] = Str_replace ("{", "", $res [' loadavg ']);
  66. $res [' loadavg '] = Str_replace ("}", "", $res [' loadavg ']);

  67. UPTIME

  68. if (false = = = ($buf = Get_key ("Kern.boottime"))) return false;
  69. $buf = Explode (' ', $buf);
  70. $sys _ticks = time () –intval ($buf [3]);
  71. $min = $sys _ticks/60;
  72. $hours = $min/60;
  73. $days = Floor ($hours/24);
  74. $hours = Floor ($hours – ($days * 24));
  75. $min = Floor ($min – ($days * 60 * 24) – ($hours * 60));
  76. if ($days!== 0) $res [' uptime '] = $days. " Days ";
  77. if ($hours!== 0) $res [' uptime ']. = $hours. " Hours ";
  78. $res [' uptime ']. = $min. " Minutes ";

  79. MEMORY

  80. if (false = = = ($buf = Get_key ("Hw.physmem"))) return false;
  81. $res [' memtotal '] = round ($buf/1024/1024, 2);
  82. $buf = explode ("\ n", Do_command ("Vmstat", ""));
  83. $buf = Explode ("", Trim ($buf [2]));

  84. $res [' memfree '] = round ($buf [5]/1024, 2);

  85. $res [' memused '] = ($res [' memtotal ']-$res [' memfree ']);
  86. $res [' mempercent '] = (floatval ($res [' memtotal '])!=0)? Round (($res [' memused ']/$res [' Memtotal ']): 0;

  87. $buf = explode ("\ n", Do_command ("Swapinfo", "K"));

  88. $buf = $buf [1];
  89. Preg_match_all ("/([0-9]+) \s+ ([0-9]+] \s+ ([0-9]+)/", $buf, $BUFARR);
  90. $res [' swaptotal '] = round ($bufArr [1][0]/1024, 2);
  91. $res [' swapused '] = round ($bufArr [2][0]/1024, 2);
  92. $res [' swapfree '] = round ($bufArr [3][0]/1024, 2);
  93. $res [' swappercent '] = (floatval ($res [' swaptotal '])!=0)? Round (($res [' swapused ']/$res [' Swaptotal ']): 0;

  94. return $res;

  95. }

  96. /*

  97. Get parameter Value FreeBSD
  98. */
  99. function Get_key ($keyName)
  100. {
  101. Return Do_command (' Sysctl ', "-N $keyName");
  102. }

  103. /*

  104. Determining the execution file location FreeBSD
  105. */
  106. function Find_command ($commandName)
  107. {
  108. $path = Array ('/bin ', '/sbin ', '/usr/bin ', '/usr/sbin ', '/usr/local/bin ', '/usr/local/sbin ');
  109. foreach ($path as $p)
  110. {
  111. if (@is_executable ("$p/$commandName") return "$p/$commandName";
  112. }
  113. return false;
  114. }

  115. /*

  116. Execute system Command FreeBSD
  117. */
  118. function Do_command ($commandName, $args)
  119. {
  120. $buffer = "";
  121. if (false = = = ($command = Find_command ($commandName))) return false;
  122. if ($fp = @popen ("$command $args", ' R '))
  123. {
  124. while (! @feof ($FP))
  125. {
  126. $buffer. = @fgets ($fp, 4096);
  127. }
  128. Return trim ($buffer);
  129. }
  130. return false;
  131. }
  132. ?>

Copy Code

Description: Memory with this function can be done: Memory_get_usage () under Windows can only read the results of the cmd command run. For example:

    1. $a = System (' mem ');
    2. echo $a. "
    3. “;
    4. Or
    5. $a = System (' mem ');
    6. echo $a. "
    7. “;
    8. ?>
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.