search
Homephp教程php手册php检测服务器系统信息[可以查CPU]

php检测服务器系统信息[可以查CPU]

Jun 13, 2016 am 09:57 AM
@cpufalsefunctionlinuxphpstrsysinformationCanservercheckDetectionsystem

function sys_linux() {
 // CPU
 if (false === ($str = @file("/proc/cpuinfo"))) return false;
 $str = implode("", $str);
 @preg_match_all("/models+names{0,}:+s{0,}([ws)(@.]+)([rn]+)/s", $str, $model);
 @preg_match_all("/caches+sizes{0,}:+s{0,}([d.]+s{0,}[A-Z]+[rn]+)/", $str, $cache);
 if (false !== is_array($model[1])) {
  $res['cpu']['num'] = sizeof($model[1]);
  for($i = 0; $i    $res['cpu']['model'][] = $model[1][$i];
   $res['cpu']['cache'][] = $cache[1][$i];
  }
  if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("
", $res['cpu']['model']);
  if (false !== is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("
", $res['cpu']['cache']);
 }

 // UPTIME
 if (false === ($str = @file("/proc/uptime"))) return false;
 $str = explode(" ", implode("", $str));
 $str = trim($str[0]);
 $min = $str / 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
 if (false === ($str = @file("/proc/meminfo"))) return false;
 $str = implode("", $str);
 preg_match_all("/MemTotals{0,}:+s{0,}([d.]+).+?MemFrees{0,}:+s{0,}([d.]+).+?SwapTotals{0,}:+s{0,}([d.]+).+?SwapFrees{0,}:+s{0,}([d.]+)/s", $str, $buf);

 $res['memTotal'] = round($buf[1][0]/1024, 2);
 $res['memFree'] = round($buf[2][0]/1024, 2);
 $res['memUsed'] = ($res['memTotal']-$res['memFree']);
 $res['memPercent'] = (floatval($res['memTotal'])!=0)?round($res['memUsed']/$res['memTotal']*100,2):0;

 $res['swapTotal'] = round($buf[3][0]/1024, 2);
 $res['swapFree'] = round($buf[4][0]/1024, 2);
 $res['swapUsed'] = ($res['swapTotal']-$res['swapFree']);
 $res['swapPercent'] = (floatval($res['swapTotal'])!=0)?round($res['swapUsed']/$res['swapTotal']*100,2):0;

 // LOAD AVG
 if (false === ($str = @file("/proc/loadavg"))) return false;
 $str = explode(" ", implode("", $str));
 $str = array_chunk($str, 4);
 $res['loadAvg'] = implode(" ", $str[0]);

 return $res;}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function