SystemInfo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\RfDevTool\backend\components;
  3. use yii\web\NotFoundHttpException;
  4. /**
  5. * Class SystemInfo
  6. * @package backend\components
  7. * @author jianyan74 <751393839@qq.com>
  8. */
  9. class SystemInfo
  10. {
  11. /**
  12. * 系统类
  13. *
  14. * @var SystemInfoWindows
  15. */
  16. protected $system;
  17. /**
  18. * cpu状态说明
  19. *
  20. * @var array
  21. */
  22. public static $cpuExplain = [
  23. 'idle' => 'CPU空闲',
  24. 'user' => '用户进程',
  25. 'sys' => '内核进程',
  26. 'iowait' => '等待进行I/O',
  27. 'nice' => '更改优先级',
  28. 'irq' => '系统中断',
  29. 'softirq' => '软件中断',
  30. ];
  31. /**
  32. * SystemInfo constructor.
  33. * @throws NotFoundHttpException
  34. */
  35. public function __construct()
  36. {
  37. // Darwin
  38. switch (PHP_OS) {
  39. case'Linux':
  40. $this->system = new SystemInfoLinux();
  41. break;
  42. case'WINNT':
  43. $this->system = new SystemInfoWindows();
  44. break;
  45. default :
  46. throw new NotFoundHttpException('当前系统为' . PHP_OS . ',不在支持范围,仅支持Linux和Windows');
  47. break;
  48. }
  49. }
  50. /**
  51. * 获取cpu信息
  52. *
  53. * @return array
  54. */
  55. public function getCpu()
  56. {
  57. return $this->system->getCpu();
  58. }
  59. /**
  60. * 获取cpu使用率
  61. *
  62. * @return array
  63. */
  64. public function getCpuUse()
  65. {
  66. return $this->system->getCpuUse();
  67. }
  68. /**
  69. * 获取网络情况
  70. *
  71. * @return array
  72. */
  73. public function getNetwork()
  74. {
  75. return $this->system->getNetwork();
  76. }
  77. /**
  78. * 获取运行时间
  79. *
  80. * @return string
  81. */
  82. public function getUptime()
  83. {
  84. return $this->system->getUptime();
  85. }
  86. /**
  87. * 获取内存情况
  88. *
  89. * @return array
  90. */
  91. public function getMemory()
  92. {
  93. return $this->system->getMemory();
  94. }
  95. /**
  96. * 获取负载情况
  97. *
  98. * @return array
  99. */
  100. public function getLoadavg()
  101. {
  102. return $this->system->getLoadavg();
  103. }
  104. /**
  105. * 获取环境信息
  106. *
  107. */
  108. public function getEnvironment()
  109. {
  110. $environment = [];
  111. $environment['ip'] = @$_SERVER['REMOTE_ADDR'];
  112. $domain = $this->os() ? $_SERVER['SERVER_ADDR'] : @gethostbyname($_SERVER['SERVER_NAME']);
  113. $os = explode(" ", php_uname());
  114. $environment['domainIP'] = @get_current_user() . ' - ' . $_SERVER['SERVER_NAME'] . '(' . $domain . ')';
  115. $environment['flag'] = php_uname();
  116. $environment['phpOs'] = PHP_OS;
  117. $environment['os'] = $os[0] . '内核版本:' . $this->os() ? $os[2] : $os[1];
  118. $environment['language'] = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];;
  119. $environment['name'] = $this->os() ? $os[1] : $os[2];
  120. $environment['email'] = @$_SERVER['SERVER_ADMIN'];
  121. $environment['webEngine'] = $_SERVER['SERVER_SOFTWARE'];
  122. $environment['webPort'] = @$_SERVER['SERVER_PORT'];
  123. $environment['webPath'] = $_SERVER['DOCUMENT_ROOT'] ? str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']) : str_replace('\\', '/', dirname(__FILE__));
  124. $environment['probePath'] = str_replace('\\', '/', __FILE__) ? str_replace('\\', '/', __FILE__) : $_SERVER['SCRIPT_FILENAME'];
  125. $environment['newTime'] = date('Y-m-d H:i:s');
  126. return $environment;
  127. }
  128. /**
  129. * 获取硬盘情况
  130. *
  131. * total 总量
  132. * free 空闲
  133. * used 已用
  134. * usage_rate 使用率
  135. * @return mixed
  136. */
  137. public function getHardDisk()
  138. {
  139. $hardDisk['total'] = round(@disk_total_space(".") / (1024 * 1024 * 1024), 2);
  140. $hardDisk['free'] = round(@disk_free_space(".") / (1024 * 1024 * 1024), 2);
  141. $hardDisk['used'] = round($hardDisk['total'] - $hardDisk['free'], 2);
  142. $usage_rate = (floatval($hardDisk['total']) != 0) ? round($hardDisk['used'] / $hardDisk['total'] * 100, 2) : 0;
  143. $hardDisk['usage_rate'] = round($usage_rate, 2);
  144. return $hardDisk;
  145. }
  146. /**
  147. * 判断linux系统还是windows系统
  148. *
  149. * @return bool
  150. */
  151. private function os()
  152. {
  153. return DIRECTORY_SEPARATOR == '/' ? true : false;
  154. }
  155. }
粤ICP备19079148号