Rumah >pembangunan bahagian belakang >tutorial php >php windows系统探测程序_PHP教程
function do_command($commandName, $args) {
$buffer = "";
if (false === ($command = find_command($commandName))) return false;
if ($fp = @popen("$command $args", 'r')) {
while (!@feof($fp)){
$buffer .= @fgets($fp, 4096);
}
return trim($buffer);
}
return false;
}
//windows系统探测
function sys_windows() {
$objLocator = new COM("WbemScripting.SWbemLocator");
$wmi = $objLocator->ConnectServer();
$prop = $wmi->get("Win32_PnPEntity");
//CPU
$cpuinfo = GetWMI($wmi,"Win32_Processor", array("Name","L2CacheSize","NumberOfCores"));
$res['cpu']['num'] = $cpuinfo[0]['NumberOfCores'];
if (null == $res['cpu']['num']) {
$res['cpu']['num'] = 1;
}
for ($i=0;$i
$res['cpu']['model'] .= $cpuinfo[0]['Name']."
";
$res['cpu']['cache'] .= $cpuinfo[0]['L2CacheSize']."
";
}
// SYSINFO
$sysinfo = GetWMI($wmi,"Win32_OperatingSystem", array('LastBootUpTime','TotalVisibleMemorySize','FreePhysicalMemory','Caption','CSDVersion','SerialNumber','InstallDate'));
$res['win_n'] = $sysinfo[0]['Caption']." ".$sysinfo[0]['CSDVersion']." 序列号:{$sysinfo[0]['SerialNumber']} 于".date('Y年m月d日H:i:s',strtotime(substr($sysinfo[0]['InstallDate'],0,14)))."安装";
//UPTIME
$res['uptime'] = $sysinfo[0]['LastBootUpTime'];
$sys_ticks = 3600*8 + time() - strtotime(substr($res['uptime'],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) $res['uptime'] = $days."天";
if ($hours !== 0) $res['uptime'] .= $hours."小时";
$res['uptime'] .= $min."分钟";
//MEMORY
$res['memTotal'] = $sysinfo[0]['TotalVisibleMemorySize'];
$res['memFree'] = $sysinfo[0]['FreePhysicalMemory'];
$res['memUsed'] = $res['memTotal'] - $res['memFree'];
$res['memPercent'] = round($res['memUsed'] / $res['memTotal']*100,2);
$swapinfo = GetWMI($wmi,"Win32_PageFileUsage", array('AllocatedBaseSize','CurrentUsage'));
// TODO swp区获取
$res['swapTotal'] = $swapinfo[0][AllocatedBaseSize];
$res['swapUsed'] = $swapinfo[0][CurrentUsage];
$res['swapFree'] = $res['swapTotal'] - $res['swapUsed'];
$res['swapPercent'] = (floatval($res['swapTotal'])!=0)?round($res['swapUsed']/$res['swapTotal']*100,2):0;
// LoadPercentage
$loadinfo = GetWMI($wmi,"Win32_Processor", array("LoadPercentage"));
$res['loadAvg'] = $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;
}