2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Library for extracting information about system memory and cpu.
5 * Currently supports all Windows and Linux plattforms
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')) {
18 '/^(MemTotal|MemFree|Cached|Buffers|SwapCached|SwapTotal|SwapFree):'
23 * Returns OS type used for sysinfo class
25 * @param string $php_os PHP_OS constant
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)) {
38 return ucfirst($php_os);
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()) {
59 return new PMA_SysInfo();
63 * Basic sysinfo class not providing any real data.
65 * @package PhpMyAdmin-sysinfo
72 * Gets load information
74 * @return array with load data
76 public function loadavg()
78 return array('loadavg' => 0);
82 * Gets information about memory usage
84 * @return array with memory usage data
86 public function memory()
92 * Checks whether class is supported in this environment
94 * @return true on success
96 public function supported()
103 * Windows NT based SysInfo class
105 * @package PhpMyAdmin-sysinfo
107 class PMA_SysInfoWinnt
extends PMA_SysInfo
111 public $os = 'WINNT';
114 * Constructor to access to wmi database.
116 public function __construct()
118 if (!class_exists('COM')) {
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
136 $buffer = $this->_getWMI('Win32_Processor', array('LoadPercentage'));
138 foreach ($buffer as $load) {
139 $value = $load['LoadPercentage'];
140 $loadavg .= $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())
170 $objWEBM = $this->_wmi
->Get($strClass);
171 $arrProp = $objWEBM->Properties_
;
172 $arrWEBMCol = $objWEBM->Instances_();
173 foreach ($arrWEBMCol as $objItem) {
174 if (is_array($arrProp)) {
177 $arrInstance = array();
178 foreach ($arrProp as $propItem) {
179 $name = $propItem->Name
;
180 if ( empty($strValue) ||
in_array($name, $strValue)) {
181 $value = $objItem->$name;
182 $arrInstance[$name] = trim($value);
185 $arrData[] = $arrInstance;
191 * Gets information about memory usage
193 * @return array with memory usage data
197 $buffer = $this->_getWMI(
198 "Win32_OperatingSystem",
199 array('TotalVisibleMemorySize', 'FreePhysicalMemory')
202 $mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
203 $mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
204 $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
206 $buffer = $this->_getWMI('Win32_PageFileUsage');
208 $mem['SwapTotal'] = 0;
209 $mem['SwapUsed'] = 0;
210 $mem['SwapPeak'] = 0;
212 foreach ($buffer as $swapdevice) {
213 $mem['SwapTotal'] +
= $swapdevice['AllocatedBaseSize'] * 1024;
214 $mem['SwapUsed'] +
= $swapdevice['CurrentUsage'] * 1024;
215 $mem['SwapPeak'] +
= $swapdevice['PeakUsage'] * 1024;
223 * Linux based SysInfo class
225 * @package PhpMyAdmin-sysinfo
227 class PMA_SysInfoLinux
extends PMA_SysInfo
229 public $os = 'Linux';
232 * Gets load information
234 * @return array with load data
238 $buf = file_get_contents('/proc/stat');
239 $nums = preg_split("/\s+/", substr($buf, 0, strpos($buf, "\n")));
241 'busy' => $nums[1] +
$nums[2] +
$nums[3],
242 'idle' => intval($nums[4])
247 * Checks whether class is supported in this environment
249 * @return true on success
251 public function supported()
253 return is_readable('/proc/meminfo') && is_readable('/proc/stat');
258 * Gets information about memory usage
260 * @return array with memory usage data
266 file_get_contents('/proc/meminfo'),
270 $mem = array_combine($matches[1], $matches[2]);
272 $memTotal = isset($mem['MemTotal']) ?
$mem['MemTotal'] : 0;
273 $memFree = isset($mem['MemFree']) ?
$mem['MemFree'] : 0;
274 $cached = isset($mem['Cached']) ?
$mem['Cached'] : 0;
275 $buffers = isset($mem['Buffers']) ?
$mem['Buffers'] : 0;
276 $swapTotal = isset($mem['SwapTotal']) ?
$mem['SwapTotal'] : 0;
277 $swapFree = isset($mem['SwapFree']) ?
$mem['SwapFree'] : 0;
278 $swapCached = isset($mem['SwapCached']) ?
$mem['SwapCached'] : 0;
281 = $memTotal - $memFree - $cached - $buffers;
283 = $swapTotal - $swapFree - $swapCached;
285 foreach ($mem as $idx => $value) {
286 $mem[$idx] = intval($value);
293 * SunOS based SysInfo class
295 * @package PhpMyAdmin-sysinfo
297 class PMA_SysInfoSunos
extends PMA_SysInfo
299 public $os = 'SunOS';
302 * Read value from kstat
304 * @param string $key Key to read
306 * @return string with value
308 private function _kstat($key)
310 if ($m = shell_exec('kstat -p d ' . $key)) {
311 list($key, $value) = preg_split("/\t/", trim($m), 2);
319 * Gets load information
321 * @return array with load data
323 public function loadavg()
325 $load1 = $this->_kstat('unix:0:system_misc:avenrun_1min');
327 return array('loadavg' => $load1);
331 * Checks whether class is supported in this environment
333 * @return true on success
335 public function supported()
337 return is_readable('/proc/meminfo');
342 * Gets information about memory usage
344 * @return array with memory usage data
346 public function memory()
349 $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
351 = $this->_kstat('unix:0:system_pages:pagestotal') * $pagesize;
353 = $this->_kstat('unix:0:system_pages:pageslocked') * $pagesize;
355 = $this->_kstat('unix:0:system_pages:pagesfree') * $pagesize;
356 $mem['SwapTotal'] = $this->_kstat('unix:0:vminfo:swap_avail') / 1024;
357 $mem['SwapUsed'] = $this->_kstat('unix:0:vminfo:swap_alloc') / 1024;
358 $mem['SwapFree'] = $this->_kstat('unix:0:vminfo:swap_free') / 1024;