首頁  >  文章  >  後端開發  >  php取得cpu與記憶體使用量的程式碼一例

php取得cpu與記憶體使用量的程式碼一例

WBOY
WBOY原創
2016-07-25 08:59:321065瀏覽
  1. /*

  2. @系統參數偵測 LINUX
  3. @編輯 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(“/models+names{0,}:+s{0,}([ ws)(.]+)[rn]+/”, $str, $model);
  12. //@preg_match_all(“/cpus+MHzs{0,}:+s{0,}([d.] +)[rn]+/”, $str, $mhz);
  13. @preg_match_all(“/caches+sizes{0,}:+s{0,}([d.]+s{0,}[ A-Z]+[rn]+)/”, $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. // 正常運作時間

  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 = 樓層($hours / 24);
  30. $小時= 下限($小時– ($天數* 24));
  31. $小時= 下限($小時– ($天數* 24));
  32. $min = 下限($min – ($天數* 60 * 24) – ($小時* 60));
  33. if ($天!== 0) $res['uptime'] = $days.“天”;
  34. if ($hours !== 0) $res['uptime'] .= $hours.“小時”;
  35. $res['uptime'] .= $min.”分鐘”;
  36. // MEMORY

  37. if (false === ($str = @file(“ /proc/meminfo”))) return false;
  38. $str = implode(“ ”, $str);
  39. preg_match_all(“/MemTotals{0,}:+s{0,}([d.] +).+?MemFrees{0,}:+s{0,}([d .]+).+?SwapTotals{0,}:+s{0,}([d.]+).+?SwapFrees {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'])*90,2) :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'])*90,2):0;
  48. // 載入平均值

  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]); p>
  53. 回傳$res;

  54. }
  55. /*
  56. 系統參數偵測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. //載入平均值

  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 = 下限($hours – ($days * 24));
  75. $min = 下限($min – ($days * 60 * 24) – ($hours * 60)) ;
  76. if ($days !== 0) $res['uptime'] = $days.」天」;
  77. if ($hours !== 0) $res['uptime'] .= $小時。 ”小時”;
  78. $res['uptime'] .= $min.“分鐘”;
  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'])*90,2):0;< ;/p>
  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'])*90,2):0;
  94. 回傳$res;}

  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. 返回修剪($buffer);
  128. }
  129. return false;
  130. }
  131. ?>
複製程式碼

說明: 記憶體用此函數可搞定:memory_get_usage() 在Windows下面只能讀取cmd命令運行的結果。 例如:

  1. $a = system('mem');
  2. echo $a.」
  3. 「;
  4. //或
  5. $a = system('mem');
  6. echo $a.」
  7. 「;
  8. ?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn