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 sysinfo class
46 function PMA_getSysInfo()
48 $php_os = PMA_getSysInfoOs();
49 $supported = array('Linux', 'WINNT', 'SunOS');
53 if (in_array($php_os, $supported)) {
54 $ret = eval("return new PMA_SysInfo" . $php_os . "();");
55 if ($ret->supported()) {
60 return new PMA_SysInfo
;
64 * Basic sysinfo class not providing any real data.
66 * @package PhpMyAdmin-sysinfo
73 * Gets load information
75 * @return array with load data
77 public function loadavg()
79 return array('loadavg' => 0);
83 * Gets information about memory usage
85 * @return array with memory usage data
87 public function memory()
93 * Checks whether class is supported in this environment
95 * @return true on success
97 public function supported()
104 * Windows NT based SysInfo class
106 * @package PhpMyAdmin-sysinfo
108 class PMA_SysInfoWinnt
extends PMA_SysInfo
112 public $os = 'WINNT';
115 * Constructor to access to wmi database.
117 public function __construct()
119 if (!class_exists('COM')) {
122 // initialize the wmi object
123 $objLocator = new COM('WbemScripting.SWbemLocator');
124 $this->_wmi
= $objLocator->ConnectServer();
129 * Gets load information
131 * @return array with load data
137 $buffer = $this->_getWMI('Win32_Processor', array('LoadPercentage'));
139 foreach ($buffer as $load) {
140 $value = $load['LoadPercentage'];
141 $loadavg .= $value . ' ';
145 return array('loadavg' => $sum / count($buffer));
149 * Checks whether class is supported in this environment
151 * @return true on success
153 public function supported()
155 return !is_null($this->_wmi
);
159 * Reads data from WMI
161 * @param string $strClass Class to read
162 * @param array $strValue Values to read
164 * @return arrray with results
166 private function _getWMI($strClass, $strValue = array())
171 $objWEBM = $this->_wmi
->Get($strClass);
172 $arrProp = $objWEBM->Properties_
;
173 $arrWEBMCol = $objWEBM->Instances_();
174 foreach ($arrWEBMCol as $objItem) {
175 if (is_array($arrProp)) {
178 $arrInstance = array();
179 foreach ($arrProp as $propItem) {
180 if ( empty($strValue)) {
181 eval("\$value = \$objItem->" . $propItem->Name
. ";");
182 $arrInstance[$propItem->Name
] = trim($value);
184 if (in_array($propItem->Name
, $strValue)) {
185 eval("\$value = \$objItem->" . $propItem->Name
. ";");
186 $arrInstance[$propItem->Name
] = trim($value);
190 $arrData[] = $arrInstance;
196 * Gets information about memory usage
198 * @return array with memory usage data
202 $buffer = $this->_getWMI(
203 "Win32_OperatingSystem",
204 array('TotalVisibleMemorySize', 'FreePhysicalMemory')
207 $mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
208 $mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
209 $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
211 $buffer = $this->_getWMI('Win32_PageFileUsage');
213 $mem['SwapTotal'] = 0;
214 $mem['SwapUsed'] = 0;
215 $mem['SwapPeak'] = 0;
217 foreach ($buffer as $swapdevice) {
218 $mem['SwapTotal'] +
= $swapdevice['AllocatedBaseSize'] * 1024;
219 $mem['SwapUsed'] +
= $swapdevice['CurrentUsage'] * 1024;
220 $mem['SwapPeak'] +
= $swapdevice['PeakUsage'] * 1024;
228 * Linux based SysInfo class
230 * @package PhpMyAdmin-sysinfo
232 class PMA_SysInfoLinux
extends PMA_SysInfo
234 public $os = 'Linux';
237 * Gets load information
239 * @return array with load data
243 $buf = file_get_contents('/proc/stat');
244 $nums = preg_split("/\s+/", substr($buf, 0, strpos($buf, "\n")));
246 'busy' => $nums[1] +
$nums[2] +
$nums[3],
247 'idle' => intval($nums[4])
252 * Checks whether class is supported in this environment
254 * @return true on success
256 public function supported()
258 return is_readable('/proc/meminfo') && is_readable('/proc/stat');
263 * Gets information about memory usage
265 * @return array with memory usage data
271 file_get_contents('/proc/meminfo'),
275 $mem = array_combine($matches[1], $matches[2]);
277 = $mem['MemTotal'] - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers'];
279 = $mem['SwapTotal'] - $mem['SwapFree'] - $mem['SwapCached'];
281 foreach ($mem as $idx => $value) {
282 $mem[$idx] = intval($value);
289 * SunOS based SysInfo class
291 * @package PhpMyAdmin-sysinfo
293 class PMA_SysInfoSunos
extends PMA_SysInfo
295 public $os = 'SunOS';
298 * Read value from kstat
300 * @param string $key Key to read
302 * @return string with value
304 private function _kstat($key)
306 if ($m = shell_exec('kstat -p d '.$key)) {
307 list($key, $value) = preg_split("/\t/", trim($m), 2);
315 * Gets load information
317 * @return array with load data
319 public function loadavg()
321 $load1 = $this->_kstat('unix:0:system_misc:avenrun_1min');
323 return array('loadavg' => $load1);
327 * Checks whether class is supported in this environment
329 * @return true on success
331 public function supported()
333 return is_readable('/proc/meminfo');
338 * Gets information about memory usage
340 * @return array with memory usage data
342 public function memory()
346 file_get_contents('/proc/meminfo'),
350 $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
352 = $this->_kstat('unix:0:system_pages:pagestotal') * $pagesize;
354 = $this->_kstat('unix:0:system_pages:pageslocked') * $pagesize;
356 = $this->_kstat('unix:0:system_pages:pagesfree') * $pagesize;
357 $mem['SwapTotal'] = $this->_kstat('unix:0:vminfo:swap_avail') / 1024;
358 $mem['SwapUsed'] = $this->_kstat('unix:0:vminfo:swap_alloc') / 1024;
359 $mem['SwapFree'] = $this->_kstat('unix:0:vminfo:swap_free') / 1024;