sysinfo.lib.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 PMA_SysInfo|mixed sysinfo class
  40. */
  41. function PMA_getSysInfo()
  42. {
  43. $php_os = PMA_getSysInfoOs();
  44. $supported = array('Linux', 'WINNT', 'SunOS');
  45. if (in_array($php_os, $supported)) {
  46. $class_name = 'PMA_SysInfo' . $php_os;
  47. $ret = new $class_name();
  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 array 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. $name = $propItem->Name;
  160. if ( empty($strValue) || in_array($name, $strValue)) {
  161. $value = $objItem->$name;
  162. $arrInstance[$name] = trim($value);
  163. }
  164. }
  165. $arrData[] = $arrInstance;
  166. }
  167. return $arrData;
  168. }
  169. /**
  170. * Gets information about memory usage
  171. *
  172. * @return array with memory usage data
  173. */
  174. function memory()
  175. {
  176. $buffer = $this->_getWMI(
  177. "Win32_OperatingSystem",
  178. array('TotalVisibleMemorySize', 'FreePhysicalMemory')
  179. );
  180. $mem = Array();
  181. $mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
  182. $mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
  183. $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
  184. $buffer = $this->_getWMI('Win32_PageFileUsage');
  185. $mem['SwapTotal'] = 0;
  186. $mem['SwapUsed'] = 0;
  187. $mem['SwapPeak'] = 0;
  188. foreach ($buffer as $swapdevice) {
  189. $mem['SwapTotal'] += $swapdevice['AllocatedBaseSize'] * 1024;
  190. $mem['SwapUsed'] += $swapdevice['CurrentUsage'] * 1024;
  191. $mem['SwapPeak'] += $swapdevice['PeakUsage'] * 1024;
  192. }
  193. return $mem;
  194. }
  195. }
  196. /**
  197. * Linux based SysInfo class
  198. *
  199. * @package PhpMyAdmin-sysinfo
  200. */
  201. class PMA_SysInfoLinux extends PMA_SysInfo
  202. {
  203. public $os = 'Linux';
  204. /**
  205. * Gets load information
  206. *
  207. * @return array with load data
  208. */
  209. function loadavg()
  210. {
  211. $buf = file_get_contents('/proc/stat');
  212. $nums = preg_split("/\s+/", substr($buf, 0, strpos($buf, "\n")));
  213. return Array(
  214. 'busy' => $nums[1] + $nums[2] + $nums[3],
  215. 'idle' => intval($nums[4])
  216. );
  217. }
  218. /**
  219. * Checks whether class is supported in this environment
  220. *
  221. * @return true on success
  222. */
  223. public function supported()
  224. {
  225. return is_readable('/proc/meminfo') && is_readable('/proc/stat');
  226. }
  227. /**
  228. * Gets information about memory usage
  229. *
  230. * @return array with memory usage data
  231. */
  232. function memory()
  233. {
  234. preg_match_all(
  235. MEMORY_REGEXP,
  236. file_get_contents('/proc/meminfo'),
  237. $matches
  238. );
  239. $mem = array_combine($matches[1], $matches[2]);
  240. $memTotal = isset($mem['MemTotal']) ? $mem['MemTotal'] : 0;
  241. $memFree = isset($mem['MemFree']) ? $mem['MemFree'] : 0;
  242. $cached = isset($mem['Cached']) ? $mem['Cached'] : 0;
  243. $buffers = isset($mem['Buffers']) ? $mem['Buffers'] : 0;
  244. $swapTotal = isset($mem['SwapTotal']) ? $mem['SwapTotal'] : 0;
  245. $swapFree = isset($mem['SwapFree']) ? $mem['SwapFree'] : 0;
  246. $swapCached = isset($mem['SwapCached']) ? $mem['SwapCached'] : 0;
  247. $mem['MemUsed']
  248. = $memTotal - $memFree - $cached - $buffers;
  249. $mem['SwapUsed']
  250. = $swapTotal - $swapFree - $swapCached;
  251. foreach ($mem as $idx => $value) {
  252. $mem[$idx] = intval($value);
  253. }
  254. return $mem;
  255. }
  256. }
  257. /**
  258. * SunOS based SysInfo class
  259. *
  260. * @package PhpMyAdmin-sysinfo
  261. */
  262. class PMA_SysInfoSunos extends PMA_SysInfo
  263. {
  264. public $os = 'SunOS';
  265. /**
  266. * Read value from kstat
  267. *
  268. * @param string $key Key to read
  269. *
  270. * @return string with value
  271. */
  272. private function _kstat($key)
  273. {
  274. if ($m = shell_exec('kstat -p d ' . $key)) {
  275. list($key, $value) = preg_split("/\t/", trim($m), 2);
  276. return $value;
  277. } else {
  278. return '';
  279. }
  280. }
  281. /**
  282. * Gets load information
  283. *
  284. * @return array with load data
  285. */
  286. public function loadavg()
  287. {
  288. $load1 = $this->_kstat('unix:0:system_misc:avenrun_1min');
  289. return array('loadavg' => $load1);
  290. }
  291. /**
  292. * Checks whether class is supported in this environment
  293. *
  294. * @return true on success
  295. */
  296. public function supported()
  297. {
  298. return is_readable('/proc/meminfo');
  299. }
  300. /**
  301. * Gets information about memory usage
  302. *
  303. * @return array with memory usage data
  304. */
  305. public function memory()
  306. {
  307. $mem = array();
  308. $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
  309. $mem['MemTotal']
  310. = $this->_kstat('unix:0:system_pages:pagestotal') * $pagesize;
  311. $mem['MemUsed']
  312. = $this->_kstat('unix:0:system_pages:pageslocked') * $pagesize;
  313. $mem['MemFree']
  314. = $this->_kstat('unix:0:system_pages:pagesfree') * $pagesize;
  315. $mem['SwapTotal'] = $this->_kstat('unix:0:vminfo:swap_avail') / 1024;
  316. $mem['SwapUsed'] = $this->_kstat('unix:0:vminfo:swap_alloc') / 1024;
  317. $mem['SwapFree'] = $this->_kstat('unix:0:vminfo:swap_free') / 1024;
  318. return $mem;
  319. }
  320. }