SystemInfoLinux.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace addons\RfDevTool\backend\components;
  3. /**
  4. * linux 探针
  5. *
  6. * Class SystemInfoLinux
  7. * @package backend\components
  8. * @author jianyan74 <751393839@qq.com>
  9. */
  10. class SystemInfoLinux
  11. {
  12. /**
  13. * @return array
  14. */
  15. public function getCpu()
  16. {
  17. $res = [];
  18. // 获取CPU信息
  19. if ($cpuinfo = @file("/proc/cpuinfo")) {
  20. $cpuinfo = implode("",$cpuinfo);
  21. @preg_match_all("/model\s+name\s{0,}\:+\s{0,}([\w\s\)\(\@.-]+)([\r\n]+)/s",$cpuinfo, $model);// CPU 名称
  22. @preg_match_all("/cpu\s+MHz\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/",$cpuinfo, $mhz);// CPU频率
  23. @preg_match_all("/cache\s+size\s{0,}\:+\s{0,}([\d\.]+\s{0,}[A-Z]+[\r\n]+)/",$cpuinfo, $cache);// CPU缓存
  24. @preg_match_all("/bogomips\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/",$cpuinfo, $bogomips);
  25. if (is_array($model[1])) {
  26. $cpunum = count($model[1]);
  27. $mhz[1][0] =' | 频率(MHz):' . $mhz[1][0];
  28. $cache[1][0] =' | 二级缓存:' . $cache[1][0];
  29. $bogomips[1][0] =' | Bogomips:' . $bogomips[1][0];
  30. $res['num'] = $cpunum;
  31. $res['model'] = $model[1][0] . $mhz[1][0] . $cache[1][0] . $bogomips[1][0];
  32. // if (is_array($res['model'])) {$res['model'] = implode("<br />", $res['model']);}
  33. // if(is_array($res['mhz']))$res['mhz'] = implode("<br />", $res['mhz']);
  34. // if(is_array($res['cache']))$res['cache'] = implode("<br />", $res['cache']);
  35. // if(is_array($res['bogomips']))$res['bogomips'] = implode("<br />", $res['bogomips']);
  36. }
  37. }
  38. return $res;
  39. }
  40. /**
  41. * 获取cpu使用情况
  42. *
  43. * @return array
  44. */
  45. public function getCpuUse()
  46. {
  47. $cores= [];
  48. $data= @file('/proc/stat');
  49. foreach ($data as $line) {
  50. if (preg_match('/^cpu[0-9]/',$line)) {
  51. $info = explode(' ',$line);
  52. $cores[] = [
  53. 'idle' => $info[4],
  54. 'user' => $info[1],
  55. 'nice' => $info[2],
  56. 'sys' => $info[3],
  57. 'iowait' => $info[5],
  58. 'irq' => $info[6],
  59. 'softirq' => $info[7]
  60. ];
  61. }
  62. }
  63. return $cores;
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function getNetwork()
  69. {
  70. $res = [];
  71. $res['allOutSpeed'] = 0;
  72. $res['allInputSpeed'] = 0;
  73. // 网络速度
  74. $res['currentOutSpeed'] = 0;
  75. $res['currentInputSpeed'] = 0;
  76. $strs = @file("/proc/net/dev");
  77. $lines = count($strs);
  78. for ($i = 2; $i < $lines; $i++) {
  79. preg_match_all("/([^\s]+):[\s]{0,}(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/", $strs[$i], $info);
  80. $name = $info[1][0];
  81. $res[$name]['name'] = $name;
  82. $res[$name]['outSpeed'] = $info[10][0];
  83. $res[$name]['inputSpeed'] = $info[2][0];
  84. $res['allOutSpeed'] += $info[10][0];
  85. $res['allInputSpeed'] += $info[2][0];
  86. }
  87. return $res;
  88. }
  89. /**
  90. * 获取内存信息
  91. *
  92. * @return array
  93. */
  94. public function getMemory()
  95. {
  96. // 内存
  97. if ($meminfo = @file("/proc/meminfo")) {
  98. $meminfo = implode("",$meminfo);
  99. preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s",$meminfo, $buf);
  100. preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $meminfo, $buffers);
  101. // 内存
  102. $memory = [];
  103. $memory['total'] = round($buf[1][0] / 1024, 2);
  104. $memory['free'] = round($buf[2][0] / 1024, 2);
  105. $memory['used'] = round($memory['total'] - $memory['free'], 2);
  106. $memory['buffers'] = round($buffers[1][0] / 1024, 2);
  107. $memory['usage_rate'] = (floatval($memory['total']) != 0) ? round($memory['used'] / $memory['total'] * 100, 2) : 0;
  108. // 缓存
  109. $cache = [];
  110. $cache['total'] = round($buf[3][0] / 1024, 2);
  111. $cache['usage_rate'] = (floatval($cache['total']) != 0) ? round($cache['total'] / $memory['total'] * 100,2) : 0;
  112. $cache['real'] = $cache['total'] * $cache['usage_rate'];
  113. !empty($cache['real']) && $cache['real'] = round($cache['real'] / 100, 2);
  114. // 真实
  115. $real = [];
  116. $real['used'] = round($memory['total'] - $memory['free'] - $cache['total'] - $memory['buffers'], 2);
  117. $real['free'] = $memory['total'] - $real['used'];
  118. $real['usage_rate'] = (floatval($memory['total']) != 0 ) ? round($real['used'] / $memory['total'] * 100,2) : 0 ;
  119. $swap = [];
  120. $swap['total'] = round($buf[4][0] / 1024, 2);
  121. $swap['free'] = round($buf[5][0] / 1024, 2);
  122. $swap['used'] = round($swap['total'] - $swap['free'], 2);
  123. $swap['usage_rate'] = (floatval($swap['total']) != 0) ? round($swap['used'] / $swap['total'] * 100,2) : 0;
  124. }
  125. return [
  126. 'memory' => $memory,
  127. 'cache' => $cache,
  128. 'real' => $real,
  129. 'swap' => $swap,
  130. ];
  131. }
  132. /**
  133. * 系统负载
  134. */
  135. public function getLoadavg()
  136. {
  137. $info = [];
  138. $info['loadavg'] = [];
  139. if ($loadavg = @file("/proc/loadavg")) {
  140. $loadavg = explode(" ", implode("", $loadavg));
  141. $loadavg = array_chunk($loadavg, 4);
  142. $info['loadavg'] = $loadavg;
  143. $info['explain'] = implode(" ", $loadavg[0]);
  144. }
  145. return $info;
  146. }
  147. /**
  148. * 获取系统已在时间
  149. *
  150. * @return string
  151. */
  152. public function getUptime()
  153. {
  154. $info = '获取失败';
  155. if ($uptime = @file("/proc/uptime")) {
  156. $str = explode(" ", implode("", $uptime));
  157. $str = trim($str[0]);
  158. $min = $str / 60;
  159. $hours = $min / 60;
  160. $days = floor($hours / 24);
  161. $hours = floor($hours - ($days * 24));
  162. $min = floor($min-($days * 60 * 24)-($hours * 60));
  163. $info = $days . " 天 " . $hours . " 小时 " . $min . " 分钟 ";
  164. }
  165. return $info;
  166. }
  167. }
粤ICP备19079148号