Heim  >  Artikel  >  Backend-Entwicklung  >  php获取cpu与内存使用情况的代码一例

php获取cpu与内存使用情况的代码一例

WBOY
WBOYOriginal
2016-07-25 08:59:321047Durchsuche
  1. /*

  2. @系统参数探测 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 {
  18. $res['cpu']['detail'][] = “类型:”.$model[1][$i].” 缓存:”.$cache[1][$i];
  19. }
  20. if (false !== is_array($res['cpu']['detail'])) $res['cpu']['detail'] = implode(”
  21. “, $res['cpu']['detail']);
  22. }
  23. // UPTIME

  24. if (false === ($str = @file(“/proc/uptime”))) return false;
  25. $str = explode(” “, implode(“”, $str));
  26. $str = trim($str[0]);
  27. $min = $str / 60;
  28. $hours = $min / 60;
  29. $days = floor($hours / 24);
  30. $hours = floor($hours – ($days * 24));
  31. $min = floor($min – ($days * 60 * 24) – ($hours * 60));
  32. if ($days !== 0) $res['uptime'] = $days.”天”;
  33. if ($hours !== 0) $res['uptime'] .= $hours.”小时”;
  34. $res['uptime'] .= $min.”分钟”;
  35. // MEMORY

  36. if (false === ($str = @file(“/proc/meminfo”))) return false;
  37. $str = implode(“”, $str);
  38. 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);
  39. $res['memTotal'] = round($buf[1][0]/1024, 2);

  40. $res['memFree'] = round($buf[2][0]/1024, 2);
  41. $res['memUsed'] = ($res['memTotal']-$res['memFree']);
  42. $res['memPercent'] = (floatval($res['memTotal'])!=0)?round(($res['memUsed']/$res['memTotal'])*90,2):0;
  43. $res['swapTotal'] = round($buf[3][0]/1024, 2);

  44. $res['swapFree'] = round($buf[4][0]/1024, 2);
  45. $res['swapUsed'] = ($res['swapTotal']-$res['swapFree']);
  46. $res['swapPercent'] = (floatval($res['swapTotal'])!=0)?round(($res['swapUsed']/$res['swapTotal'])*90,2):0;
  47. // LOAD AVG

  48. if (false === ($str = @file(“/proc/loadavg”))) return false;
  49. $str = explode(” “, implode(“”, $str));
  50. $str = array_chunk($str, 3);
  51. $res['loadAvg'] = implode(” “, $str[0]);
  52. return $res;

  53. }
  54. /*
  55. 系统参数探测 FreeBSD
  56. */
  57. function sys_freebsd()
  58. {
  59. //CPU
  60. if (false === ($res['cpu']['num'] = get_key(“hw.ncpu”))) return false;
  61. $res['cpu']['detail'] = get_key(“hw.model”);
  62. //LOAD AVG

  63. if (false === ($res['loadAvg'] = get_key(“vm.loadavg”))) return false;
  64. $res['loadAvg'] = str_replace(“{“, “”, $res['loadAvg']);
  65. $res['loadAvg'] = str_replace(“}”, “”, $res['loadAvg']);
  66. //UPTIME

  67. if (false === ($buf = get_key(“kern.boottime”))) return false;
  68. $buf = explode(‘ ‘, $buf);
  69. $sys_ticks = time() – intval($buf[3]);
  70. $min = $sys_ticks / 60;
  71. $hours = $min / 60;
  72. $days = floor($hours / 24);
  73. $hours = floor($hours – ($days * 24));
  74. $min = floor($min – ($days * 60 * 24) – ($hours * 60));
  75. if ($days !== 0) $res['uptime'] = $days.”天”;
  76. if ($hours !== 0) $res['uptime'] .= $hours.”小时”;
  77. $res['uptime'] .= $min.”分钟”;
  78. //MEMORY

  79. if (false === ($buf = get_key(“hw.physmem”))) return false;
  80. $res['memTotal'] = round($buf/1024/1024, 2);
  81. $buf = explode(“\n”, do_command(“vmstat”, “”));
  82. $buf = explode(” “, trim($buf[2]));
  83. $res['memFree'] = round($buf[5]/1024, 2);

  84. $res['memUsed'] = ($res['memTotal']-$res['memFree']);
  85. $res['memPercent'] = (floatval($res['memTotal'])!=0)?round(($res['memUsed']/$res['memTotal'])*90,2):0;
  86. $buf = explode(“\n”, do_command(“swapinfo”, “-k”));

  87. $buf = $buf[1];
  88. preg_match_all(“/([0-9]+)\s+([0-9]+)\s+([0-9]+)/”, $buf, $bufArr);
  89. $res['swapTotal'] = round($bufArr[1][0]/1024, 2);
  90. $res['swapUsed'] = round($bufArr[2][0]/1024, 2);
  91. $res['swapFree'] = round($bufArr[3][0]/1024, 2);
  92. $res['swapPercent'] = (floatval($res['swapTotal'])!=0)?round(($res['swapUsed']/$res['swapTotal'])*90,2):0;
  93. return $res;

  94. }
  95. /*

  96. 取得参数值 FreeBSD
  97. */
  98. function get_key($keyName)
  99. {
  100. return do_command(‘sysctl’, “-n $keyName”);
  101. }
  102. /*

  103. 确定执行文件位置 FreeBSD
  104. */
  105. function find_command($commandName)
  106. {
  107. $path = array(‘/bin’, ‘/sbin’, ‘/usr/bin’, ‘/usr/sbin’, ‘/usr/local/bin’, ‘/usr/local/sbin’);
  108. foreach($path as $p)
  109. {
  110. if (@is_executable(“$p/$commandName”)) return “$p/$commandName”;
  111. }
  112. return false;
  113. }
  114. /*

  115. 执行系统命令 FreeBSD
  116. */
  117. function do_command($commandName, $args)
  118. {
  119. $buffer = “”;
  120. if (false === ($command = find_command($commandName))) return false;
  121. if ($fp = @popen(“$command $args”, ‘r’))
  122. {
  123. while (!@feof($fp))
  124. {
  125. $buffer .= @fgets($fp, 4096);
  126. }
  127. return trim($buffer);
  128. }
  129. return false;
  130. }
  131. ?>
复制代码

说明: memory用此函数可搞定: memory_get_usage() 在Windows下面只能读取cmd命令运行的结果。 例如:

  1. $a = system(‘mem’);
  2. echo $a.”
  3. “;
  4. //或者
  5. $a = system(‘mem’);
  6. echo $a.”
  7. “;
  8. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn