SysInfoWINNT.php 3.2 KB

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