SysInfoWINNT.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. $arrInstance[$name] = trim($value);
  80. }
  81. }
  82. $arrData[] = $arrInstance;
  83. }
  84. return $arrData;
  85. }
  86. /**
  87. * Gets information about memory usage
  88. *
  89. * @return array with memory usage data
  90. */
  91. function memory()
  92. {
  93. $buffer = $this->_getWMI(
  94. "Win32_OperatingSystem",
  95. array('TotalVisibleMemorySize', 'FreePhysicalMemory')
  96. );
  97. $mem = Array();
  98. $mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
  99. $mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
  100. $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
  101. $buffer = $this->_getWMI('Win32_PageFileUsage');
  102. $mem['SwapTotal'] = 0;
  103. $mem['SwapUsed'] = 0;
  104. $mem['SwapPeak'] = 0;
  105. foreach ($buffer as $swapdevice) {
  106. $mem['SwapTotal'] += $swapdevice['AllocatedBaseSize'] * 1024;
  107. $mem['SwapUsed'] += $swapdevice['CurrentUsage'] * 1024;
  108. $mem['SwapPeak'] += $swapdevice['PeakUsage'] * 1024;
  109. }
  110. return $mem;
  111. }
  112. }