Guest User

Untitled

a guest
Feb 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. <?php
  2. class CPUInfo {
  3. public static function getCpuUsage($samples = 5) {
  4. $cmd = 'grep cpu /proc/stat';
  5.  
  6. exec($cmd, $output1);
  7. $cpus1 = self::parseCpuStats($output1);
  8.  
  9. $data = [];
  10. for ($i = 0; $i < $samples; $i++) {
  11. usleep(50000);
  12. $output2 = [];
  13. exec($cmd, $output2);
  14. $cpus2 = self::parseCpuStats($output2);
  15.  
  16. foreach ($cpus1 as $cpu => $value) {
  17. if (!isset($data[$cpu])) $data[$cpu] = 0;
  18.  
  19. $total = $cpus2[$cpu]['idle'] - $value['idle'] + $cpus2[$cpu]['work'] - $value['work'];
  20. $data[$cpu] += ($cpus2[$cpu]['work'] - $value['work']) / $total;
  21. }
  22. $cpus1 = $cpus2;
  23. }
  24.  
  25. foreach ($data as $cpu => $value) {
  26. $data[$cpu] = $data[$cpu] / $samples * 100;
  27. }
  28.  
  29.  
  30. var_dump($data);
  31.  
  32. }
  33.  
  34. public static function parseCpuStats($stat_cpuinfo) {
  35. $cpus = [];
  36. $first = true;
  37. foreach ($stat_cpuinfo as $line) {
  38. $line = preg_replace('!\s+!', ' ', $line);
  39. $parts = explode(' ', $line);
  40. $cpu = $parts[0];
  41.  
  42. unset($parts[0]);
  43.  
  44. $line = implode(' ', $parts);
  45.  
  46. sscanf($line,
  47. "%D %D %D %D %D %D %D %D %D %D",
  48. $user, $nice, $system, $idle, $iowait, $irq, $softirq, $steal, $guest, $guestnice
  49. );
  50.  
  51. if ($first) {
  52. $cpus['ALL'] = [
  53. 'work' => $user + $nice + $system + $irq + $softirq + $steal,
  54. 'idle' => $idle + $iowait
  55. ];
  56. }
  57. else {
  58. $cpus[$cpu] = [
  59. 'work' => $user + $nice + $system + $irq + $softirq + $steal,
  60. 'idle' => $idle + $iowait
  61. ];
  62. }
  63. $first = false;
  64. }
  65.  
  66. return $cpus;
  67. }
  68.  
  69. public static function getRamUsage() {
  70. $free = trim(shell_exec('free'));
  71. // $free = (string)trim($free);
  72. $free_arr = explode("\n", $free);
  73.  
  74. $ret = [
  75. 'hard' => [
  76. 'total' => 0,
  77. 'used' => 0,
  78. 'percent' => 0,
  79. ],
  80. 'swap' => [
  81. 'total' => 0,
  82. 'used' => 0,
  83. 'percent' => 0,
  84. ]
  85. ];
  86. foreach ($free_arr as $line) {
  87. if (!preg_match('/(Mem|Swap).*/', $line)) continue;
  88. $mem_line = explode(" ", $line);
  89. $mem_clean = array_merge(array_filter($mem_line));
  90.  
  91. $memory_usage = $mem_clean[2]/$mem_clean[1]*100;
  92.  
  93. if (preg_match('/Mem/', $line)) {
  94. $ret['hard']['percent'] = $memory_usage;
  95. $ret['hard']['total'] = (int)$mem_clean[2];
  96. $ret['hard']['used'] = (int)$mem_clean[1];
  97. }
  98. if (preg_match('/Swap/', $line)) {
  99. $ret['swap']['percent'] = $memory_usage;
  100. $ret['swap']['total'] = (int)$mem_clean[2];
  101. $ret['swap']['used'] = (int)$mem_clean[1];
  102. }
  103. }
  104.  
  105. return $ret;
  106. }
  107. }
Add Comment
Please, Sign In to add comment