WindowsNt.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Server\SysInfo;
  4. use COM;
  5. use function class_exists;
  6. use function count;
  7. use function in_array;
  8. use function is_string;
  9. use function trim;
  10. /**
  11. * Windows NT based SysInfo class
  12. */
  13. class WindowsNt extends Base
  14. {
  15. /** @var COM|null */
  16. private $wmi;
  17. /**
  18. * The OS name
  19. *
  20. * @var string
  21. */
  22. public $os = 'WINNT';
  23. /**
  24. * Constructor to access to wmi database.
  25. */
  26. public function __construct()
  27. {
  28. if (! class_exists('COM')) {
  29. $this->wmi = null;
  30. } else {
  31. // initialize the wmi object
  32. $objLocator = new COM('WbemScripting.SWbemLocator');
  33. $this->wmi = $objLocator->ConnectServer();
  34. }
  35. }
  36. /**
  37. * Gets load information
  38. *
  39. * @return array with load data
  40. */
  41. public function loadavg()
  42. {
  43. $sum = 0;
  44. $buffer = $this->getWMI('Win32_Processor', ['LoadPercentage']);
  45. foreach ($buffer as $load) {
  46. $value = $load['LoadPercentage'];
  47. $sum += $value;
  48. }
  49. return ['loadavg' => $sum / count($buffer)];
  50. }
  51. /**
  52. * Checks whether class is supported in this environment
  53. *
  54. * @return bool true on success
  55. */
  56. public function supported()
  57. {
  58. return $this->wmi !== null;
  59. }
  60. /**
  61. * Reads data from WMI
  62. *
  63. * @param string $strClass Class to read
  64. * @param array $strValue Values to read
  65. *
  66. * @return array with results
  67. */
  68. private function getWMI($strClass, array $strValue = [])
  69. {
  70. $arrData = [];
  71. $objWEBM = $this->wmi->Get($strClass);
  72. $arrProp = $objWEBM->Properties_;
  73. $arrWEBMCol = $objWEBM->Instances_();
  74. foreach ($arrWEBMCol as $objItem) {
  75. $arrInstance = [];
  76. foreach ($arrProp as $propItem) {
  77. $name = $propItem->Name;
  78. if (! empty($strValue) && ! in_array($name, $strValue)) {
  79. continue;
  80. }
  81. $value = $objItem->$name;
  82. if (is_string($value)) {
  83. $arrInstance[$name] = trim($value);
  84. } else {
  85. $arrInstance[$name] = $value;
  86. }
  87. }
  88. $arrData[] = $arrInstance;
  89. }
  90. return $arrData;
  91. }
  92. /**
  93. * Gets information about memory usage
  94. *
  95. * @return array with memory usage data
  96. */
  97. public function memory()
  98. {
  99. $buffer = $this->getWMI(
  100. 'Win32_OperatingSystem',
  101. [
  102. 'TotalVisibleMemorySize',
  103. 'FreePhysicalMemory',
  104. ]
  105. );
  106. $mem = [];
  107. $mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
  108. $mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
  109. $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
  110. $buffer = $this->getWMI('Win32_PageFileUsage');
  111. $mem['SwapTotal'] = 0;
  112. $mem['SwapUsed'] = 0;
  113. $mem['SwapPeak'] = 0;
  114. foreach ($buffer as $swapdevice) {
  115. $mem['SwapTotal'] += $swapdevice['AllocatedBaseSize'] * 1024;
  116. $mem['SwapUsed'] += $swapdevice['CurrentUsage'] * 1024;
  117. $mem['SwapPeak'] += $swapdevice['PeakUsage'] * 1024;
  118. }
  119. return $mem;
  120. }
  121. }