SystemController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace addons\RfDevTool\backend\controllers;
  3. use Yii;
  4. use addons\RfDevTool\backend\components\SystemInfo;
  5. use common\helpers\ResultHelper;
  6. /**
  7. * Class SystemController
  8. * @package backend\modules\sys\controllers
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class SystemController extends BaseController
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $key = 'sys:probe';
  17. /**
  18. * 服务器探针
  19. *
  20. * @return array|string
  21. * @throws \yii\web\NotFoundHttpException
  22. */
  23. public function actionProbe()
  24. {
  25. $info = $this->getProbeInfo();
  26. if (Yii::$app->request->isAjax) {
  27. return ResultHelper::json(200, '获取成功', $info);
  28. }
  29. return $this->render('probe', [
  30. 'info' => $info,
  31. ]);
  32. }
  33. /**
  34. * 探针
  35. *
  36. * @return mixed
  37. * @throws \yii\web\NotFoundHttpException
  38. */
  39. protected function getProbeInfo()
  40. {
  41. $info = new SystemInfo();
  42. $systemInfo = [
  43. 'environment' => $info->getEnvironment(),
  44. 'hardDisk' => $info->getHardDisk(),
  45. 'cpu' => $info->getCpu(),
  46. 'cpuUse' => $info->getCpuUse(),
  47. 'netWork' => $info->getNetwork(),
  48. 'memory' => $info->getMemory(),
  49. 'loadavg' => $info->getLoadavg(),
  50. 'uptime' => $info->getUptime(),
  51. 'time' => time()
  52. ];
  53. // 计算网络速度和CPU使用情况
  54. $oldSystemInfo = Yii::$app->cache->get($this->key);
  55. if (!empty($oldSystemInfo) && PHP_OS == 'Linux') {
  56. $oldNewwork = $oldSystemInfo['netWork'];
  57. $newNewwork = $systemInfo['netWork'];
  58. $networkFormatting = (time() - $oldSystemInfo['time']) * 1024;
  59. empty($networkFormatting) && $networkFormatting = 1;
  60. $systemInfo['netWork']['currentOutSpeed'] = round(($newNewwork['allOutSpeed'] - $oldNewwork['allOutSpeed']) / $networkFormatting, 2);
  61. $systemInfo['netWork']['currentInputSpeed'] = round(($newNewwork['allInputSpeed'] - $oldNewwork['allInputSpeed']) / $networkFormatting, 2);
  62. // 计算CPU情况
  63. $oldCpuUse = $oldSystemInfo['cpuUse'];
  64. unset($oldCpuUse['explain']);
  65. $newCpuUse = $systemInfo['cpuUse'];
  66. $cpus = [];
  67. $systemInfo['cpuUse']['explain'] = '';
  68. foreach ($oldCpuUse as $key => $item) {
  69. $cpuUse = [];
  70. foreach ($item as $k => $v) {
  71. $cpuUse[$k] = $newCpuUse[$key][$k] - $oldCpuUse[$key][$k];
  72. }
  73. unset($v);
  74. $total = array_sum($cpuUse);
  75. $cpu = [];
  76. $prefix = '';
  77. foreach($cpuUse as $k => $v) {
  78. $cpu['cpu'][$k] = round($v / $total * 100, 2);
  79. $systemInfo['cpuUse']['explain'] .= $prefix . SystemInfo::$cpuExplain[$k] .' : ' . $cpu['cpu'][$k] . '% ';
  80. $prefix = ' | ';
  81. }
  82. $systemInfo['cpuUse']['explain'] .= "<br>";
  83. $cpus[] = $cpu;
  84. }
  85. }
  86. empty($systemInfo['cpuUse']['explain']) && $systemInfo['cpuUse']['explain'] = '正在计算...';
  87. Yii::$app->cache->set($this->key, $systemInfo);
  88. // 网络数字转格式
  89. $systemInfo['netWork']['allOutSpeed'] = round($systemInfo['netWork']['allOutSpeed'] / (1024 * 1024 * 1024), 2);
  90. $systemInfo['netWork']['allInputSpeed'] = round($systemInfo['netWork']['allInputSpeed'] / (1024 * 1024 * 1024), 2);
  91. $num = 3;
  92. $num_arr = [];
  93. for ($i = 20; $i >= 1; $i--) {
  94. $num_arr[] = date('H:i:s', time() - $i * $num);
  95. }
  96. $systemInfo['chartTime'] = $num_arr;
  97. return $systemInfo;
  98. }
  99. }
粤ICP备19079148号