通过实例化COM类,调用windows主机的COM组件实现系统硬件和软件具体信息,并返回数组信息。只能在windows的服务器上使用,linux没有COM组件。
通过COM组件可以获取系统CPU信息、内存信息、系统信息等。把数据在后台轮询调用可以实时查看到系统详情,便于服务器信息监控。
详细代码:
ConnectServer(); $prop = $wmi->get("Win32_PnPEntity"); //CPU $cpuinfo = GetWMI($wmi,"Win32_Processor", array("Name","L2CacheSize","NumberOfCores")); $res['CPU个数'] = $cpuinfo[0]['NumberOfCores']; if (null == $res['CPU个数']) { $res['CPU个数'] = 1; } for ($i=0;$i<$res['cpu']['num'];$i++){ $res['CPU型号'] .= $cpuinfo[0]['Name']."
"; $res['二级缓存'] .= $cpuinfo[0]['L2CacheSize']."
"; } // SYSINFO $sysinfo = GetWMI($wmi,"Win32_OperatingSystem", array('LastBootUpTime','TotalVisibleMemorySize', 'FreePhysicalMemory','Caption','CSDVersion','SerialNumber','InstallDate')); $res[' *** 作系统版本'] = $sysinfo[0]['Caption']." ".$sysinfo[0]['CSDVersion']; $res[' *** 作系统序列号'] = "{$sysinfo[0]['SerialNumber']} 于" .date('Y年m月d日H:i:s',strtotime(substr($sysinfo[0]['InstallDate'],0,14)))."安装"; //UPTIME $res['最后重启时间'] = $sysinfo[0]['LastBootUpTime']; $sys_ticks = 3600*8 + time() - strtotime(substr($res['最后重启时间'],0,14)); $min = $sys_ticks / 60; $hours = $min / 60; $days = floor($hours / 24); $hours = floor($hours - ($days * 24)); $min = floor($min - ($days * 60 * 24) - ($hours * 60)); if ($days !== 0) $ress['day'] = $days."天"; if ($hours !== 0) $ress['hours'] .= $hours."小时"; $res['最后重启时间'] = $ress['day'].$ress['hours'].$min."分钟"; //MEMORY $res['物理内存'] = $sysinfo[0]['TotalVisibleMemorySize']; $res['剩余内存'] = $sysinfo[0]['FreePhysicalMemory']; $res['已使用内存'] = $res['物理内存'] - $res['剩余内存']; $res['使用率'] = round($res['已使用内存'] / $res['物理内存']*100,2); $swapinfo = GetWMI($wmi,"Win32_PageFileUsage", array('AllocatedbaseSize','CurrentUsage')); // TODO swp区获取 $res['交换分区'] = $swapinfo[0][AllocatedbaseSize]; $res['已经使用'] = $swapinfo[0][CurrentUsage]; $res['剩余内存'] = $res['swapTotal'] - $res['swapUsed']; $res['使用率'] = (floatval($res['swapTotal'])!=0)?round($res['swapUsed']/$res['swapTotal']*100,2):0; // LoadPercentage $loadinfo = GetWMI($wmi,"Win32_Processor", array("LoadPercentage")); $res['系统平均负载'] = $loadinfo[0]['LoadPercentage']; return $res; } function GetWMI($wmi,$strClass, $strValue = array()) { $arrData = array(); $objWEBM = $wmi->Get($strClass); $arrProp = $objWEBM->Properties_; $arrWEBMCol = $objWEBM->Instances_(); foreach($arrWEBMCol as $objItem) { @reset($arrProp); $arrInstance = array(); foreach($arrProp as $propItem) { eval("$value = $objItem->" . $propItem->Name . ";"); if (empty($strValue)) { $arrInstance[$propItem->Name] = trim($value); } else { if (in_array($propItem->Name, $strValue)) { $arrInstance[$propItem->Name] = trim($value); } } } $arrData[] = $arrInstance; } return $arrData; } //调用 //返回数组 $res=sys_windows(); ?>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)