Advisor: mark that 'Rate of reading fixed position' may be wrong, requires further...
[phpmyadmin/thilanka.git] / libraries / sysinfo.lib.php
blob96ac0e2939789b30c8c24eec64bd45d0a9372be2
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Library for extracting information about system memory and cpu. Currently supports all
5 * Windows and Linux plattforms
7 * This code is based on the OS Classes from the phpsysinfo project (http://phpsysinfo.sourceforge.net/)
9 * @package phpMyAdmin
12 /**
13 * @return array
15 function getSysInfo()
17 $supported = array('Linux','WINNT');
19 $sysinfo = array();
21 if (in_array(PHP_OS, $supported)) {
22 return eval("return new ".PHP_OS."();");
25 return $sysinfo;
29 class WINNT
31 private $_wmi;
33 public $os = 'WINNT';
35 public function __construct() {
36 // initialize the wmi object
37 $objLocator = new COM('WbemScripting.SWbemLocator');
38 $this->_wmi = $objLocator->ConnectServer();
41 function loadavg() {
42 $loadavg = "";
43 $sum = 0;
44 $buffer = $this->_getWMI('Win32_Processor', array('LoadPercentage'));
46 foreach ($buffer as $load) {
47 $value = $load['LoadPercentage'];
48 $loadavg .= $value.' ';
49 $sum += $value;
52 return array('loadavg' => $sum / count($buffer));
55 private function _getWMI($strClass, $strValue = array()) {
56 $arrData = array();
57 $value = "";
59 $objWEBM = $this->_wmi->Get($strClass);
60 $arrProp = $objWEBM->Properties_;
61 $arrWEBMCol = $objWEBM->Instances_();
62 foreach ($arrWEBMCol as $objItem) {
63 if (is_array($arrProp)) {
64 reset($arrProp);
66 $arrInstance = array();
67 foreach ($arrProp as $propItem) {
68 if ( empty($strValue)) {
69 eval("\$value = \$objItem->".$propItem->Name.";");
70 $arrInstance[$propItem->Name] = trim($value);
71 } else {
72 if (in_array($propItem->Name, $strValue)) {
73 eval("\$value = \$objItem->".$propItem->Name.";");
74 $arrInstance[$propItem->Name] = trim($value);
78 $arrData[] = $arrInstance;
80 return $arrData;
84 function memory() {
85 $buffer = $this->_getWMI("Win32_OperatingSystem", array('TotalVisibleMemorySize', 'FreePhysicalMemory'));
86 $mem = Array();
87 $mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
88 $mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
89 $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
91 $buffer = $this->_getWMI('Win32_PageFileUsage');
93 $mem['SwapTotal'] = 0;
94 $mem['SwapUsed'] = 0;
95 $mem['SwapPeak'] = 0;
97 foreach ($buffer as $swapdevice) {
98 $mem['SwapTotal'] += $swapdevice['AllocatedBaseSize'] * 1024;
99 $mem['SwapUsed'] += $swapdevice['CurrentUsage'] * 1024;
100 $mem['SwapPeak'] += $swapdevice['PeakUsage'] * 1024;
103 return $mem;
107 class Linux
109 public $os = 'Linux';
111 function loadavg() {
112 $buf = file_get_contents('/proc/stat');
113 $nums=preg_split("/\s+/", substr($buf,0,strpos($buf,"\n")));
114 return Array('busy' => $nums[1]+$nums[2]+$nums[3], 'idle' => intval($nums[4]));
117 function memory() {
118 preg_match_all('/^(MemTotal|MemFree|Cached|Buffers|SwapCached|SwapTotal|SwapFree):\s+(.*)\s*kB/im', file_get_contents('/proc/meminfo'), $matches);
120 $mem = array_combine( $matches[1], $matches[2] );
121 $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers'];
122 $mem['SwapUsed'] = $mem['SwapTotal'] - $mem['SwapFree'] - $mem['SwapCached'];
124 foreach ($mem as $idx=>$value)
125 $mem[$idx] = intval($value);
127 return $mem;