2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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/)
17 $supported = array('Linux','WINNT');
21 if (in_array(PHP_OS
, $supported)) {
22 return eval("return new ".PHP_OS
."();");
35 public function __construct() {
36 // initialize the wmi object
37 $objLocator = new COM('WbemScripting.SWbemLocator');
38 $this->_wmi
= $objLocator->ConnectServer();
44 $buffer = $this->_getWMI('Win32_Processor', array('LoadPercentage'));
46 foreach ($buffer as $load) {
47 $value = $load['LoadPercentage'];
48 $loadavg .= $value.' ';
52 return array('loadavg' => $sum / count($buffer));
55 private function _getWMI($strClass, $strValue = array()) {
59 $objWEBM = $this->_wmi
->Get($strClass);
60 $arrProp = $objWEBM->Properties_
;
61 $arrWEBMCol = $objWEBM->Instances_();
62 foreach ($arrWEBMCol as $objItem) {
63 if (is_array($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);
72 if (in_array($propItem->Name
, $strValue)) {
73 eval("\$value = \$objItem->".$propItem->Name
.";");
74 $arrInstance[$propItem->Name
] = trim($value);
78 $arrData[] = $arrInstance;
85 $buffer = $this->_getWMI("Win32_OperatingSystem", array('TotalVisibleMemorySize', 'FreePhysicalMemory'));
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;
97 foreach ($buffer as $swapdevice) {
98 $mem['SwapTotal'] +
= $swapdevice['AllocatedBaseSize'] * 1024;
99 $mem['SwapUsed'] +
= $swapdevice['CurrentUsage'] * 1024;
100 $mem['SwapPeak'] +
= $swapdevice['PeakUsage'] * 1024;
109 public $os = 'Linux';
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]));
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);