System.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/24
  6. * Time: 下午2:29
  7. */
  8. namespace app\common\services;
  9. use app\common\helpers\Cache;
  10. use app\common\helpers\YunSession;
  11. use Illuminate\Support\Facades\Redis;
  12. class System
  13. {
  14. private $is_show; //只支持linux
  15. private $loadAvg; //系统负载
  16. private $cpu; //CPU咯
  17. private $RAM; //内存
  18. private $disk; //磁盘
  19. public function __construct()
  20. {
  21. switch (PHP_OS) {
  22. case 'Linux' :
  23. $this->is_show = 1;
  24. break;
  25. default :
  26. $this->is_show = 0;
  27. break;
  28. }
  29. }
  30. public function index()
  31. {
  32. $system_info = Redis::get('system_info');
  33. if (is_null($system_info)) {
  34. $this->loadAvg = $this->getLoadAvg();
  35. $this->cpu = $this->getCpu();
  36. $this->RAM = $this->getRAM();
  37. $this->disk = $this->getDisk();
  38. $data = [
  39. $this->loadAvg,
  40. $this->cpu,
  41. $this->RAM,
  42. $this->disk
  43. ];
  44. Redis::setnx('system_info', json_encode($data));
  45. Redis::expire('system_info', 120);
  46. } else {
  47. $system_info = json_decode($system_info);
  48. $this->loadAvg = $system_info[0];
  49. $this->cpu = $system_info[1];
  50. $this->RAM = $system_info[2];
  51. $this->disk = $system_info[3];
  52. }
  53. return [
  54. 'loadAvg' => $this->loadAvg,
  55. 'cpu' => $this->cpu,
  56. 'RAM' => $this->RAM,
  57. 'disk' => $this->disk,
  58. 'is_show' => $this->is_show,
  59. ];
  60. }
  61. /**
  62. * @return bool
  63. * @return 实例 : 1.63 0.61 0.22
  64. * 1.63(1分钟平均负载) 0.61(5分钟平均负载) 0.22(15分钟平均负载) 1/228(分子是当前正在运行的进程数,分母是总的进程数)
  65. */
  66. private function getLoadAvg()
  67. {
  68. if (false === ($str = @file("/proc/loadavg"))) return false;
  69. $str = explode(" ", implode("", $str));
  70. $str = array_chunk($str, 4);
  71. $percent = explode('/' ,$str[0][3]);
  72. $str[0][3] = round($percent[0]/$percent[1]*100, 2);
  73. return $str[0];
  74. }
  75. private function getCpu()
  76. {
  77. if (false === ($str = @file("/proc/cpuinfo"))) return false;
  78. $str = implode("", $str);
  79. @preg_match_all("/model\s+name\s{0,}\:+\s{0,}([\w\s\)\(\@.-]+)([\r\n]+)/s", $str, $model);
  80. if (false !== is_array($model[1])) {
  81. $res['cpu']['num'] = sizeof($model[1]);
  82. if ($res['cpu']['num'] == 1)
  83. $x1 = '';
  84. else
  85. $x1 = ' ×' . $res['cpu']['num'];
  86. $res['cpu']['model'][] = $model[1][0];
  87. if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("<br />", $res['cpu']['model']);
  88. $stat1 = self::GetCoreInformation();
  89. sleep(1);
  90. $stat2 = self::GetCoreInformation();
  91. $data = self::GetCpuPercentages($stat1, $stat2);
  92. $res['cpu']['using'] = $data['cpu0']['user']; //cpu使用率
  93. return $res['cpu'];
  94. }
  95. }
  96. private function getRAM()
  97. {
  98. if (false === ($str = @file("/proc/meminfo"))) return false;
  99. $str = implode("", $str);
  100. 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", $str, $buf);
  101. preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
  102. $res['memTotal'] = round($buf[1][0]/1024, 2);
  103. $res['memBuffers'] = round($buffers[1][0]/1024, 2);
  104. $res['memFree'] = round($buf[2][0]/1024, 2);
  105. $res['memCached'] = round($buf[3][0]/1024, 2);
  106. $res['memUsed'] = round($res['memTotal']-$res['memFree'] ,3);
  107. $res['memRealUsed'] = $res['memTotal'] - $res['memFree'] - $res['memCached'] - $res['memBuffers']; //真实内存使用
  108. $res['memRealFree'] = $res['memTotal'] - $res['memRealUsed']; //真实空闲
  109. $res['memPercent'] = (floatval($res['memTotal'])!=0)?round($res['memRealUsed']/$res['memTotal']*100,2):0;
  110. if($res['memTotal']<1024)
  111. {
  112. $res['memTotal'] = $res['memTotal']." M";
  113. $res['memUsed'] = $res['memRealUsed']." M";
  114. $res['memFree'] = $res['memRealFree']." M";
  115. }
  116. else
  117. {
  118. $res['memTotal'] = round($res['memTotal']/1024,3)." G";
  119. $res['memUsed'] = round($res['memRealUsed']/1024,3)." G";
  120. $res['memFree'] = round($res['memRealFree']/1024,3)." G";
  121. }
  122. return $res;
  123. }
  124. private function getDisk()
  125. {
  126. //硬盘
  127. $re['total'] = round(@disk_total_space(".")/(1024*1024*1024),3); //总
  128. $re['free'] = round(@disk_free_space(".")/(1024*1024*1024),3); //可用
  129. $re['used'] = round($re['total']-$re['free'], 3); //已用
  130. $re['percent'] = (floatval($re['total'])!=0)?round($re['used']/$re['total']*100,2):0;
  131. return $re;
  132. }
  133. private function GetCoreInformation() {$data = file('/proc/stat');$cores = array();foreach( $data as $line ) {if( preg_match('/^cpu[0-9]/', $line) ){$info = explode(' ', $line);$cores[]=array('user'=>$info[1],'nice'=>$info[2],'sys' => $info[3],'idle'=>$info[4],'iowait'=>$info[5],'irq' => $info[6],'softirq' => $info[7]);}}return $cores;}
  134. private function GetCpuPercentages($stat1, $stat2) {if(count($stat1)!==count($stat2)){return;}$cpus=array();for( $i = 0, $l = count($stat1); $i < $l; $i++) { $dif = array(); $dif['user'] = $stat2[$i]['user'] - $stat1[$i]['user'];$dif['nice'] = $stat2[$i]['nice'] - $stat1[$i]['nice']; $dif['sys'] = $stat2[$i]['sys'] - $stat1[$i]['sys'];$dif['idle'] = $stat2[$i]['idle'] - $stat1[$i]['idle'];$dif['iowait'] = $stat2[$i]['iowait'] - $stat1[$i]['iowait'];$dif['irq'] = $stat2[$i]['irq'] - $stat1[$i]['irq'];$dif['softirq'] = $stat2[$i]['softirq'] - $stat1[$i]['softirq'];$total = array_sum($dif);$cpu = array();foreach($dif as $x=>$y) $cpu[$x] = round($y / $total * 100, 2);$cpus['cpu' . $i] = $cpu;}return $cpus;}
  135. }