Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / sysinfo.lib.php
blob14fcdfd29b8fc9f255fed4e7c975f51783e7dfee
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
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 sysinfo class
46 function PMA_getSysInfo()
48 $php_os = PMA_getSysInfoOs();
49 $supported = array('Linux', 'WINNT', 'SunOS');
51 $sysinfo = array();
53 if (in_array($php_os, $supported)) {
54 $ret = eval("return new PMA_SysInfo" . $php_os . "();");
55 if ($ret->supported()) {
56 return $ret;
60 return new PMA_SysInfo;
63 /**
64 * Basic sysinfo class not providing any real data.
66 * @package PhpMyAdmin-sysinfo
68 class PMA_SysInfo
70 public $os = PHP_OS;
72 /**
73 * Gets load information
75 * @return array with load data
77 public function loadavg()
79 return array('loadavg' => 0);
82 /**
83 * Gets information about memory usage
85 * @return array with memory usage data
87 public function memory()
89 return array();
92 /**
93 * Checks whether class is supported in this environment
95 * @return true on success
97 public function supported()
99 return true;
104 * Windows NT based SysInfo class
106 * @package PhpMyAdmin-sysinfo
108 class PMA_SysInfoWinnt extends PMA_SysInfo
110 private $_wmi;
112 public $os = 'WINNT';
115 * Constructor to access to wmi database.
117 public function __construct()
119 if (!class_exists('COM')) {
120 $this->_wmi = null;
121 } else {
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
133 function loadavg()
135 $loadavg = "";
136 $sum = 0;
137 $buffer = $this->_getWMI('Win32_Processor', array('LoadPercentage'));
139 foreach ($buffer as $load) {
140 $value = $load['LoadPercentage'];
141 $loadavg .= $value . ' ';
142 $sum += $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())
168 $arrData = array();
169 $value = "";
171 $objWEBM = $this->_wmi->Get($strClass);
172 $arrProp = $objWEBM->Properties_;
173 $arrWEBMCol = $objWEBM->Instances_();
174 foreach ($arrWEBMCol as $objItem) {
175 if (is_array($arrProp)) {
176 reset($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);
183 } else {
184 if (in_array($propItem->Name, $strValue)) {
185 eval("\$value = \$objItem->" . $propItem->Name . ";");
186 $arrInstance[$propItem->Name] = trim($value);
190 $arrData[] = $arrInstance;
192 return $arrData;
196 * Gets information about memory usage
198 * @return array with memory usage data
200 function memory()
202 $buffer = $this->_getWMI(
203 "Win32_OperatingSystem",
204 array('TotalVisibleMemorySize', 'FreePhysicalMemory')
206 $mem = Array();
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;
223 return $mem;
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
241 function loadavg()
243 $buf = file_get_contents('/proc/stat');
244 $nums = preg_split("/\s+/", substr($buf, 0, strpos($buf, "\n")));
245 return Array(
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
267 function memory()
269 preg_match_all(
270 MEMORY_REGEXP,
271 file_get_contents('/proc/meminfo'),
272 $matches
275 $mem = array_combine($matches[1], $matches[2]);
276 $mem['MemUsed']
277 = $mem['MemTotal'] - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers'];
278 $mem['SwapUsed']
279 = $mem['SwapTotal'] - $mem['SwapFree'] - $mem['SwapCached'];
281 foreach ($mem as $idx => $value) {
282 $mem[$idx] = intval($value);
284 return $mem;
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);
308 return $value;
309 } else {
310 return '';
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()
344 preg_match_all(
345 MEMORY_REGEXP,
346 file_get_contents('/proc/meminfo'),
347 $matches
350 $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
351 $mem['MemTotal']
352 = $this->_kstat('unix:0:system_pages:pagestotal') * $pagesize;
353 $mem['MemUsed']
354 = $this->_kstat('unix:0:system_pages:pageslocked') * $pagesize;
355 $mem['MemFree']
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;
361 return $mem;