sysinfo.lib.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Library for extracting information about system memory and cpu.
  5. * Currently supports all Windows and Linux plattforms
  6. *
  7. * This code is based on the OS Classes from the phpsysinfo project
  8. * (http://phpsysinfo.sourceforge.net/)
  9. *
  10. * @package PhpMyAdmin-sysinfo
  11. */
  12. if (! defined('PHPMYADMIN')) {
  13. exit;
  14. }
  15. define(
  16. 'MEMORY_REGEXP',
  17. '/^(MemTotal|MemFree|Cached|Buffers|SwapCached|SwapTotal|SwapFree):'
  18. . '\s+(.*)\s*kB/im'
  19. );
  20. /**
  21. * Returns OS type used for sysinfo class
  22. *
  23. * @param string $php_os PHP_OS constant
  24. *
  25. * @return string
  26. */
  27. function PMA_getSysInfoOs($php_os = PHP_OS)
  28. {
  29. // look for common UNIX-like systems
  30. $unix_like = array('FreeBSD', 'DragonFly');
  31. if (in_array($php_os, $unix_like)) {
  32. $php_os = 'Linux';
  33. }
  34. return ucfirst($php_os);
  35. }
  36. /**
  37. * Gets sysinfo class mathing current OS
  38. *
  39. * @return sysinfo class
  40. */
  41. function PMA_getSysInfo()
  42. {
  43. $php_os = PMA_getSysInfoOs();
  44. $supported = array('Linux', 'WINNT', 'SunOS');
  45. $sysinfo = array();
  46. if (in_array($php_os, $supported)) {
  47. $ret = eval("return new PMA_SysInfo" . $php_os . "();");
  48. if ($ret->supported()) {
  49. return $ret;
  50. }
  51. }
  52. return new PMA_SysInfo;
  53. }
  54. /**
  55. * Basic sysinfo class not providing any real data.
  56. *
  57. * @package PhpMyAdmin-sysinfo
  58. */
  59. class PMA_SysInfo
  60. {
  61. public $os = PHP_OS;
  62. /**
  63. * Gets load information
  64. *
  65. * @return array with load data
  66. */
  67. public function loadavg()
  68. {
  69. return array('loadavg' => 0);
  70. }
  71. /**
  72. * Gets information about memory usage
  73. *
  74. * @return array with memory usage data
  75. */
  76. public function memory()
  77. {
  78. return array();
  79. }
  80. /**
  81. * Checks whether class is supported in this environment
  82. *
  83. * @return true on success
  84. */
  85. public function supported()
  86. {
  87. return true;
  88. }
  89. }
  90. /**
  91. * Windows NT based SysInfo class
  92. *
  93. * @package PhpMyAdmin-sysinfo
  94. */
  95. class PMA_SysInfoWinnt extends PMA_SysInfo
  96. {
  97. private $_wmi;
  98. public $os = 'WINNT';
  99. /**
  100. * Constructor to access to wmi database.
  101. */
  102. public function __construct()
  103. {
  104. if (!class_exists('COM')) {
  105. $this->_wmi = null;
  106. } else {
  107. // initialize the wmi object
  108. $objLocator = new COM('WbemScripting.SWbemLocator');
  109. $this->_wmi = $objLocator->ConnectServer();
  110. }
  111. }
  112. /**
  113. * Gets load information
  114. *
  115. * @return array with load data
  116. */
  117. function loadavg()
  118. {
  119. $loadavg = "";
  120. $sum = 0;
  121. $buffer = $this->_getWMI('Win32_Processor', array('LoadPercentage'));
  122. foreach ($buffer as $load) {
  123. $value = $load['LoadPercentage'];
  124. $loadavg .= $value . ' ';
  125. $sum += $value;
  126. }
  127. return array('loadavg' => $sum / count($buffer));
  128. }
  129. /**
  130. * Checks whether class is supported in this environment
  131. *
  132. * @return true on success
  133. */
  134. public function supported()
  135. {
  136. return !is_null($this->_wmi);
  137. }
  138. /**
  139. * Reads data from WMI
  140. *
  141. * @param string $strClass Class to read
  142. * @param array $strValue Values to read
  143. *
  144. * @return arrray with results
  145. */
  146. private function _getWMI($strClass, $strValue = array())
  147. {
  148. $arrData = array();
  149. $value = "";
  150. $objWEBM = $this->_wmi->Get($strClass);
  151. $arrProp = $objWEBM->Properties_;
  152. $arrWEBMCol = $objWEBM->Instances_();
  153. foreach ($arrWEBMCol as $objItem) {
  154. if (is_array($arrProp)) {
  155. reset($arrProp);
  156. }
  157. $arrInstance = array();
  158. foreach ($arrProp as $propItem) {
  159. if ( empty($strValue)) {
  160. eval("\$value = \$objItem->" . $propItem->Name . ";");
  161. $arrInstance[$propItem->Name] = trim($value);
  162. } else {
  163. if (in_array($propItem->Name, $strValue)) {
  164. eval("\$value = \$objItem->" . $propItem->Name . ";");
  165. $arrInstance[$propItem->Name] = trim($value);
  166. }
  167. }
  168. }
  169. $arrData[] = $arrInstance;
  170. }
  171. return $arrData;
  172. }
  173. /**
  174. * Gets information about memory usage
  175. *
  176. * @return array with memory usage data
  177. */
  178. function memory()
  179. {
  180. $buffer = $this->_getWMI(
  181. "Win32_OperatingSystem",
  182. array('TotalVisibleMemorySize', 'FreePhysicalMemory')
  183. );
  184. $mem = Array();
  185. $mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
  186. $mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
  187. $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
  188. $buffer = $this->_getWMI('Win32_PageFileUsage');
  189. $mem['SwapTotal'] = 0;
  190. $mem['SwapUsed'] = 0;
  191. $mem['SwapPeak'] = 0;
  192. foreach ($buffer as $swapdevice) {
  193. $mem['SwapTotal'] += $swapdevice['AllocatedBaseSize'] * 1024;
  194. $mem['SwapUsed'] += $swapdevice['CurrentUsage'] * 1024;
  195. $mem['SwapPeak'] += $swapdevice['PeakUsage'] * 1024;
  196. }
  197. return $mem;
  198. }
  199. }
  200. /**
  201. * Linux based SysInfo class
  202. *
  203. * @package PhpMyAdmin-sysinfo
  204. */
  205. class PMA_SysInfoLinux extends PMA_SysInfo
  206. {
  207. public $os = 'Linux';
  208. /**
  209. * Gets load information
  210. *
  211. * @return array with load data
  212. */
  213. function loadavg()
  214. {
  215. $buf = file_get_contents('/proc/stat');
  216. $nums = preg_split("/\s+/", substr($buf, 0, strpos($buf, "\n")));
  217. return Array(
  218. 'busy' => $nums[1] + $nums[2] + $nums[3],
  219. 'idle' => intval($nums[4])
  220. );
  221. }
  222. /**
  223. * Checks whether class is supported in this environment
  224. *
  225. * @return true on success
  226. */
  227. public function supported()
  228. {
  229. return is_readable('/proc/meminfo') && is_readable('/proc/stat');
  230. }
  231. /**
  232. * Gets information about memory usage
  233. *
  234. * @return array with memory usage data
  235. */
  236. function memory()
  237. {
  238. preg_match_all(
  239. MEMORY_REGEXP,
  240. file_get_contents('/proc/meminfo'),
  241. $matches
  242. );
  243. $mem = array_combine($matches[1], $matches[2]);
  244. $mem['MemUsed']
  245. = $mem['MemTotal'] - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers'];
  246. $mem['SwapUsed']
  247. = $mem['SwapTotal'] - $mem['SwapFree'] - $mem['SwapCached'];
  248. foreach ($mem as $idx => $value) {
  249. $mem[$idx] = intval($value);
  250. }
  251. return $mem;
  252. }
  253. }
  254. /**
  255. * SunOS based SysInfo class
  256. *
  257. * @package PhpMyAdmin-sysinfo
  258. */
  259. class PMA_SysInfoSunos extends PMA_SysInfo
  260. {
  261. public $os = 'SunOS';
  262. /**
  263. * Read value from kstat
  264. *
  265. * @param string $key Key to read
  266. *
  267. * @return string with value
  268. */
  269. private function _kstat($key)
  270. {
  271. if ($m = shell_exec('kstat -p d '.$key)) {
  272. list($key, $value) = preg_split("/\t/", trim($m), 2);
  273. return $value;
  274. } else {
  275. return '';
  276. }
  277. }
  278. /**
  279. * Gets load information
  280. *
  281. * @return array with load data
  282. */
  283. public function loadavg()
  284. {
  285. $load1 = $this->_kstat('unix:0:system_misc:avenrun_1min');
  286. return array('loadavg' => $load1);
  287. }
  288. /**
  289. * Checks whether class is supported in this environment
  290. *
  291. * @return true on success
  292. */
  293. public function supported()
  294. {
  295. return is_readable('/proc/meminfo');
  296. }
  297. /**
  298. * Gets information about memory usage
  299. *
  300. * @return array with memory usage data
  301. */
  302. public function memory()
  303. {
  304. preg_match_all(
  305. MEMORY_REGEXP,
  306. file_get_contents('/proc/meminfo'),
  307. $matches
  308. );
  309. $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
  310. $mem['MemTotal']
  311. = $this->_kstat('unix:0:system_pages:pagestotal') * $pagesize;
  312. $mem['MemUsed']
  313. = $this->_kstat('unix:0:system_pages:pageslocked') * $pagesize;
  314. $mem['MemFree']
  315. = $this->_kstat('unix:0:system_pages:pagesfree') * $pagesize;
  316. $mem['SwapTotal'] = $this->_kstat('unix:0:vminfo:swap_avail') / 1024;
  317. $mem['SwapUsed'] = $this->_kstat('unix:0:vminfo:swap_alloc') / 1024;
  318. $mem['SwapFree'] = $this->_kstat('unix:0:vminfo:swap_free') / 1024;
  319. return $mem;
  320. }
  321. }