SystemInfoWindows.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace addons\RfDevTool\backend\components;
  3. /**
  4. * windows 探针
  5. *
  6. * Class SystemInfoWindows
  7. * @package backend\components
  8. * @author jianyan74 <751393839@qq.com>
  9. */
  10. class SystemInfoWindows
  11. {
  12. /**
  13. * @return array
  14. */
  15. public function getCpu()
  16. {
  17. return [
  18. 'num' => '当前系统不支持',
  19. 'model' => '当前系统不支持',
  20. ];
  21. }
  22. /**
  23. * @return array
  24. */
  25. public function getCpuUse()
  26. {
  27. $path = $this->getCupUsageVbsPath();
  28. exec("cscript -nologo $path", $usage);
  29. return [
  30. 'usage_rate' => $usage[0],
  31. 'explain' => $usage[0] . '%'
  32. ];
  33. }
  34. /**
  35. * 网络
  36. *
  37. * @return array
  38. */
  39. public function getNetwork()
  40. {
  41. return [
  42. 'allOutSpeed' => '0',
  43. 'allInputSpeed' => '0',
  44. 'currentOutSpeed' => '0',
  45. 'currentInputSpeed' => '0',
  46. ];
  47. }
  48. /**
  49. * 内存
  50. *
  51. * @return array
  52. */
  53. public function getMemory()
  54. {
  55. $path = $this->getMemoryUsageVbsPath();
  56. exec("cscript -nologo $path", $usage);
  57. $memory = json_decode($usage[0], true);
  58. $usage = round((($memory['TotalVisibleMemorySize'] - $memory['FreePhysicalMemory']) / $memory['TotalVisibleMemorySize']) * 100, 2);
  59. return [
  60. 'memory' => [
  61. 'total' => round($memory['TotalVisibleMemorySize'] / 1024, 2),
  62. 'free' => round($memory['TotalVisibleMemorySize'] / 1024, 2),
  63. 'usage_rate' => $usage,
  64. 'buffers' => 0,
  65. 'used' => round(($memory['TotalVisibleMemorySize'] - $memory['FreePhysicalMemory']) / 1024, 2),
  66. ],
  67. 'cache' => [
  68. 'total' => 0,
  69. 'usage_rate' => 0,
  70. 'real' => 0,
  71. ],
  72. 'real' => [
  73. 'used' => round(($memory['TotalVisibleMemorySize'] - $memory['FreePhysicalMemory']) / 1024, 2),
  74. 'usage_rate' => $usage,
  75. 'free' => round($memory['TotalVisibleMemorySize'] / 1024, 2),
  76. ],
  77. 'swap' => [
  78. 'total' => 0,
  79. 'usage_rate' => 0,
  80. 'used' => 0,
  81. 'free' => 0,
  82. ],
  83. ];
  84. }
  85. public function getLoadavg()
  86. {
  87. $info = [];
  88. $info['loadavg'] = [];
  89. $info['explain'] = '当前系统不支持';
  90. return $info;
  91. }
  92. /**
  93. * @return string
  94. */
  95. public function getUptime()
  96. {
  97. return '当前系统不支持';
  98. }
  99. /***************************************************************/
  100. /**
  101. * 判断指定路径下指定文件是否存在,如不存在则创建
  102. *
  103. * @param string $fileName 文件名
  104. * @param string $content 文件内容
  105. * @return string 返回文件路径
  106. */
  107. private function getFilePath($fileName, $content)
  108. {
  109. $path = dirname(__FILE__) . "\\$fileName";
  110. if (!file_exists($path)) {
  111. file_put_contents($path, $content);
  112. }
  113. return $path;
  114. }
  115. /**
  116. * 获得cpu使用率vbs文件生成函数
  117. * @return string 返回vbs文件路径
  118. */
  119. private function getCupUsageVbsPath()
  120. {
  121. return $this->getFilePath(
  122. 'cpu_usage.vbs',
  123. "On Error Resume Next
  124. Set objProc = GetObject(\"winmgmts:\\\\.\\root\cimv2:win32_processor='cpu0'\")
  125. WScript.Echo(objProc.LoadPercentage)"
  126. );
  127. }
  128. /**
  129. * 获得总内存及可用物理内存JSON vbs文件生成函数
  130. * @return string 返回vbs文件路径
  131. */
  132. private function getMemoryUsageVbsPath()
  133. {
  134. return $this->getFilePath(
  135. 'memory_usage.vbs',
  136. "On Error Resume Next
  137. Set objWMI = GetObject(\"winmgmts:\\\\.\\root\cimv2\")
  138. Set colOS = objWMI.InstancesOf(\"Win32_OperatingSystem\")
  139. For Each objOS in colOS
  140. Wscript.Echo(\"{\"\"TotalVisibleMemorySize\"\":\" & objOS.TotalVisibleMemorySize & \",\"\"FreePhysicalMemory\"\":\" & objOS.FreePhysicalMemory & \"}\")
  141. Next"
  142. );
  143. }
  144. }
粤ICP备19079148号