UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / sysinfo.lib.php
blob602485172b34c22e8aab6c44cbc8695118a3c203
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
7 * This code is based on the OS Classes from the phpsysinfo project
8 * (http://phpsysinfo.sourceforge.net/)
10 * @package PhpMyAdmin-sysinfo
12 if (! defined('PHPMYADMIN')) {
13 exit;
16 define(
17 'MEMORY_REGEXP',
18 '/^(MemTotal|MemFree|Cached|Buffers|SwapCached|SwapTotal|SwapFree):'
19 . '\s+(.*)\s*kB/im'
22 /**
23 * Returns OS type used for sysinfo class
25 * @param string $php_os PHP_OS constant
27 * @return string
29 function PMA_getSysInfoOs($php_os = PHP_OS)
32 // look for common UNIX-like systems
33 $unix_like = array('FreeBSD', 'DragonFly');
34 if (in_array($php_os, $unix_like)) {
35 $php_os = 'Linux';
38 return ucfirst($php_os);
41 /**
42 * Gets sysinfo class mathing current OS
44 * @return PMA_SysInfo|mixed sysinfo class
46 function PMA_getSysInfo()
48 $php_os = PMA_getSysInfoOs();
49 $supported = array('Linux', 'WINNT', 'SunOS');
51 if (in_array($php_os, $supported)) {
52 $class_name = 'PMA_SysInfo' . $php_os;
53 $ret = new $class_name();
54 if ($ret->supported()) {
55 return $ret;
59 return new PMA_SysInfo();
62 /**
63 * Basic sysinfo class not providing any real data.
65 * @package PhpMyAdmin-sysinfo
67 class PMA_SysInfo
69 public $os = PHP_OS;
71 /**
72 * Gets load information
74 * @return array with load data
76 public function loadavg()
78 return array('loadavg' => 0);
81 /**
82 * Gets information about memory usage
84 * @return array with memory usage data
86 public function memory()
88 return array();
91 /**
92 * Checks whether class is supported in this environment
94 * @return true on success
96 public function supported()
98 return true;
103 * Windows NT based SysInfo class
105 * @package PhpMyAdmin-sysinfo
107 class PMA_SysInfoWinnt extends PMA_SysInfo
109 private $_wmi;
111 public $os = 'WINNT';
114 * Constructor to access to wmi database.
116 public function __construct()
118 if (!class_exists('COM')) {
119 $this->_wmi = null;
120 } else {
121 // initialize the wmi object
122 $objLocator = new COM('WbemScripting.SWbemLocator');
123 $this->_wmi = $objLocator->ConnectServer();
128 * Gets load information
130 * @return array with load data
132 function loadavg()
134 $loadavg = "";
135 $sum = 0;
136 $buffer = $this->_getWMI('Win32_Processor', array('LoadPercentage'));
138 foreach ($buffer as $load) {
139 $value = $load['LoadPercentage'];
140 $loadavg .= $value . ' ';
141 $sum += $value;
144 return array('loadavg' => $sum / count($buffer));
148 * Checks whether class is supported in this environment
150 * @return true on success
152 public function supported()
154 return !is_null($this->_wmi);
158 * Reads data from WMI
160 * @param string $strClass Class to read
161 * @param array $strValue Values to read
163 * @return array with results
165 private function _getWMI($strClass, $strValue = array())
167 $arrData = array();
169 $objWEBM = $this->_wmi->Get($strClass);
170 $arrProp = $objWEBM->Properties_;
171 $arrWEBMCol = $objWEBM->Instances_();
172 foreach ($arrWEBMCol as $objItem) {
173 if (is_array($arrProp)) {
174 reset($arrProp);
176 $arrInstance = array();
177 foreach ($arrProp as $propItem) {
178 $name = $propItem->Name;
179 if ( empty($strValue) || in_array($name, $strValue)) {
180 $value = $objItem->$name;
181 $arrInstance[$name] = trim($value);
184 $arrData[] = $arrInstance;
186 return $arrData;
190 * Gets information about memory usage
192 * @return array with memory usage data
194 function memory()
196 $buffer = $this->_getWMI(
197 "Win32_OperatingSystem",
198 array('TotalVisibleMemorySize', 'FreePhysicalMemory')
200 $mem = Array();
201 $mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
202 $mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
203 $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
205 $buffer = $this->_getWMI('Win32_PageFileUsage');
207 $mem['SwapTotal'] = 0;
208 $mem['SwapUsed'] = 0;
209 $mem['SwapPeak'] = 0;
211 foreach ($buffer as $swapdevice) {
212 $mem['SwapTotal'] += $swapdevice['AllocatedBaseSize'] * 1024;
213 $mem['SwapUsed'] += $swapdevice['CurrentUsage'] * 1024;
214 $mem['SwapPeak'] += $swapdevice['PeakUsage'] * 1024;
217 return $mem;
222 * Linux based SysInfo class
224 * @package PhpMyAdmin-sysinfo
226 class PMA_SysInfoLinux extends PMA_SysInfo
228 public $os = 'Linux';
231 * Gets load information
233 * @return array with load data
235 function loadavg()
237 $buf = file_get_contents('/proc/stat');
238 $nums = preg_split(
239 "/\s+/",
240 /*overload*/mb_substr($buf, 0, /*overload*/mb_strpos($buf, "\n"))
242 return Array(
243 'busy' => $nums[1] + $nums[2] + $nums[3],
244 'idle' => intval($nums[4])
249 * Checks whether class is supported in this environment
251 * @return true on success
253 public function supported()
255 return @is_readable('/proc/meminfo') && @is_readable('/proc/stat');
260 * Gets information about memory usage
262 * @return array with memory usage data
264 function memory()
266 preg_match_all(
267 MEMORY_REGEXP,
268 file_get_contents('/proc/meminfo'),
269 $matches
272 $mem = array_combine($matches[1], $matches[2]);
274 $defaults = array(
275 'MemTotal' => 0,
276 'MemFree' => 0,
277 'Cached' => 0,
278 'Buffers' => 0,
279 'SwapTotal' => 0,
280 'SwapFree' => 0,
281 'SwapCached' => 0,
284 $mem = array_merge($defaults, $mem);
286 $mem['MemUsed'] = $mem['MemTotal']
287 - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers'];
289 $mem['SwapUsed'] = $mem['SwapTotal']
290 - $mem['SwapFree'] - $mem['SwapCached'];
292 foreach ($mem as $idx => $value) {
293 $mem[$idx] = intval($value);
295 return $mem;
300 * SunOS based SysInfo class
302 * @package PhpMyAdmin-sysinfo
304 class PMA_SysInfoSunos extends PMA_SysInfo
306 public $os = 'SunOS';
309 * Read value from kstat
311 * @param string $key Key to read
313 * @return string with value
315 private function _kstat($key)
317 if ($m = shell_exec('kstat -p d ' . $key)) {
318 list(, $value) = preg_split("/\t/", trim($m), 2);
319 return $value;
320 } else {
321 return '';
326 * Gets load information
328 * @return array with load data
330 public function loadavg()
332 $load1 = $this->_kstat('unix:0:system_misc:avenrun_1min');
334 return array('loadavg' => $load1);
338 * Checks whether class is supported in this environment
340 * @return true on success
342 public function supported()
344 return @is_readable('/proc/meminfo');
349 * Gets information about memory usage
351 * @return array with memory usage data
353 public function memory()
355 $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
356 $mem = array();
357 $mem['MemTotal']
358 = $this->_kstat('unix:0:system_pages:pagestotal') * $pagesize;
359 $mem['MemUsed']
360 = $this->_kstat('unix:0:system_pages:pageslocked') * $pagesize;
361 $mem['MemFree']
362 = $this->_kstat('unix:0:system_pages:pagesfree') * $pagesize;
363 $mem['SwapTotal'] = $this->_kstat('unix:0:vminfo:swap_avail') / 1024;
364 $mem['SwapUsed'] = $this->_kstat('unix:0:vminfo:swap_alloc') / 1024;
365 $mem['SwapFree'] = $this->_kstat('unix:0:vminfo:swap_free') / 1024;
367 return $mem;