PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/sysinfo.lib.php

https://bitbucket.org/openemr/openemr
PHP | 370 lines | 203 code | 44 blank | 123 comment | 12 complexity | bdcb7fa0c95daef792603e0facb804cc MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, LGPL-3.0, BSD-3-Clause, Unlicense, MPL-2.0, GPL-3.0, LGPL-2.1
  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 platforms
  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. /** @var PMA_SysInfo $ret */
  48. $ret = new $class_name();
  49. if ($ret->supported()) {
  50. return $ret;
  51. }
  52. }
  53. return new PMA_SysInfo();
  54. }
  55. /**
  56. * Basic sysinfo class not providing any real data.
  57. *
  58. * @package PhpMyAdmin-sysinfo
  59. */
  60. class PMA_SysInfo
  61. {
  62. public $os = PHP_OS;
  63. /**
  64. * Gets load information
  65. *
  66. * @return array with load data
  67. */
  68. public function loadavg()
  69. {
  70. return array('loadavg' => 0);
  71. }
  72. /**
  73. * Gets information about memory usage
  74. *
  75. * @return array with memory usage data
  76. */
  77. public function memory()
  78. {
  79. return array();
  80. }
  81. /**
  82. * Checks whether class is supported in this environment
  83. *
  84. * @return true on success
  85. */
  86. public function supported()
  87. {
  88. return true;
  89. }
  90. }
  91. /**
  92. * Windows NT based SysInfo class
  93. *
  94. * @package PhpMyAdmin-sysinfo
  95. */
  96. class PMA_SysInfoWinnt extends PMA_SysInfo
  97. {
  98. private $_wmi;
  99. public $os = 'WINNT';
  100. /**
  101. * Constructor to access to wmi database.
  102. */
  103. public function __construct()
  104. {
  105. if (!class_exists('COM')) {
  106. $this->_wmi = null;
  107. } else {
  108. // initialize the wmi object
  109. $objLocator = new COM('WbemScripting.SWbemLocator');
  110. $this->_wmi = $objLocator->ConnectServer();
  111. }
  112. }
  113. /**
  114. * Gets load information
  115. *
  116. * @return array with load data
  117. */
  118. function loadavg()
  119. {
  120. $loadavg = "";
  121. $sum = 0;
  122. $buffer = $this->_getWMI('Win32_Processor', array('LoadPercentage'));
  123. foreach ($buffer as $load) {
  124. $value = $load['LoadPercentage'];
  125. $loadavg .= $value . ' ';
  126. $sum += $value;
  127. }
  128. return array('loadavg' => $sum / count($buffer));
  129. }
  130. /**
  131. * Checks whether class is supported in this environment
  132. *
  133. * @return true on success
  134. */
  135. public function supported()
  136. {
  137. return !is_null($this->_wmi);
  138. }
  139. /**
  140. * Reads data from WMI
  141. *
  142. * @param string $strClass Class to read
  143. * @param array $strValue Values to read
  144. *
  145. * @return array with results
  146. */
  147. private function _getWMI($strClass, $strValue = array())
  148. {
  149. $arrData = array();
  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(
  213. "/\s+/",
  214. /*overload*/mb_substr($buf, 0, /*overload*/mb_strpos($buf, "\n"))
  215. );
  216. return Array(
  217. 'busy' => $nums[1] + $nums[2] + $nums[3],
  218. 'idle' => intval($nums[4])
  219. );
  220. }
  221. /**
  222. * Checks whether class is supported in this environment
  223. *
  224. * @return true on success
  225. */
  226. public function supported()
  227. {
  228. return @is_readable('/proc/meminfo') && @is_readable('/proc/stat');
  229. }
  230. /**
  231. * Gets information about memory usage
  232. *
  233. * @return array with memory usage data
  234. */
  235. function memory()
  236. {
  237. preg_match_all(
  238. MEMORY_REGEXP,
  239. file_get_contents('/proc/meminfo'),
  240. $matches
  241. );
  242. $mem = array_combine($matches[1], $matches[2]);
  243. $defaults = array(
  244. 'MemTotal' => 0,
  245. 'MemFree' => 0,
  246. 'Cached' => 0,
  247. 'Buffers' => 0,
  248. 'SwapTotal' => 0,
  249. 'SwapFree' => 0,
  250. 'SwapCached' => 0,
  251. );
  252. $mem = array_merge($defaults, $mem);
  253. $mem['MemUsed'] = $mem['MemTotal']
  254. - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers'];
  255. $mem['SwapUsed'] = $mem['SwapTotal']
  256. - $mem['SwapFree'] - $mem['SwapCached'];
  257. foreach ($mem as $idx => $value) {
  258. $mem[$idx] = intval($value);
  259. }
  260. return $mem;
  261. }
  262. }
  263. /**
  264. * SunOS based SysInfo class
  265. *
  266. * @package PhpMyAdmin-sysinfo
  267. */
  268. class PMA_SysInfoSunos extends PMA_SysInfo
  269. {
  270. public $os = 'SunOS';
  271. /**
  272. * Read value from kstat
  273. *
  274. * @param string $key Key to read
  275. *
  276. * @return string with value
  277. */
  278. private function _kstat($key)
  279. {
  280. if ($m = shell_exec('kstat -p d ' . $key)) {
  281. list(, $value) = preg_split("/\t/", trim($m), 2);
  282. return $value;
  283. } else {
  284. return '';
  285. }
  286. }
  287. /**
  288. * Gets load information
  289. *
  290. * @return array with load data
  291. */
  292. public function loadavg()
  293. {
  294. $load1 = $this->_kstat('unix:0:system_misc:avenrun_1min');
  295. return array('loadavg' => $load1);
  296. }
  297. /**
  298. * Checks whether class is supported in this environment
  299. *
  300. * @return true on success
  301. */
  302. public function supported()
  303. {
  304. return @is_readable('/proc/meminfo');
  305. }
  306. /**
  307. * Gets information about memory usage
  308. *
  309. * @return array with memory usage data
  310. */
  311. public function memory()
  312. {
  313. $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
  314. $mem = array();
  315. $mem['MemTotal']
  316. = $this->_kstat('unix:0:system_pages:pagestotal') * $pagesize;
  317. $mem['MemUsed']
  318. = $this->_kstat('unix:0:system_pages:pageslocked') * $pagesize;
  319. $mem['MemFree']
  320. = $this->_kstat('unix:0:system_pages:pagesfree') * $pagesize;
  321. $mem['SwapTotal'] = $this->_kstat('unix:0:vminfo:swap_avail') / 1024;
  322. $mem['SwapUsed'] = $this->_kstat('unix:0:vminfo:swap_alloc') / 1024;
  323. $mem['SwapFree'] = $this->_kstat('unix:0:vminfo:swap_free') / 1024;
  324. return $mem;
  325. }
  326. }