added japanese language
[openemr.git] / phpmyadmin / libraries / sysinfo.lib.php
blobf938a034944499b8c09fbb182ab79b11c278ae09
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 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();
168 $value = "";
170 $objWEBM = $this->_wmi->Get($strClass);
171 $arrProp = $objWEBM->Properties_;
172 $arrWEBMCol = $objWEBM->Instances_();
173 foreach ($arrWEBMCol as $objItem) {
174 if (is_array($arrProp)) {
175 reset($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;
187 return $arrData;
191 * Gets information about memory usage
193 * @return array with memory usage data
195 function memory()
197 $buffer = $this->_getWMI(
198 "Win32_OperatingSystem",
199 array('TotalVisibleMemorySize', 'FreePhysicalMemory')
201 $mem = Array();
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;
218 return $mem;
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
236 function loadavg()
238 $buf = file_get_contents('/proc/stat');
239 $nums = preg_split("/\s+/", substr($buf, 0, strpos($buf, "\n")));
240 return Array(
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
262 function memory()
264 preg_match_all(
265 MEMORY_REGEXP,
266 file_get_contents('/proc/meminfo'),
267 $matches
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;
280 $mem['MemUsed']
281 = $memTotal - $memFree - $cached - $buffers;
282 $mem['SwapUsed']
283 = $swapTotal - $swapFree - $swapCached;
285 foreach ($mem as $idx => $value) {
286 $mem[$idx] = intval($value);
288 return $mem;
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);
312 return $value;
313 } else {
314 return '';
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()
348 $mem = array();
349 $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
350 $mem['MemTotal']
351 = $this->_kstat('unix:0:system_pages:pagestotal') * $pagesize;
352 $mem['MemUsed']
353 = $this->_kstat('unix:0:system_pages:pageslocked') * $pagesize;
354 $mem['MemFree']
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;
360 return $mem;