0
点赞
收藏
分享

微信扫一扫

Linux-CentOS PHP 获取内存/CPU/负载/网络带宽数据包/磁盘IO读写等监控指标


基于Linux CentOS系统获取内存、CPU、负载、网络带宽、网络数据包率、磁盘空间、磁盘IO、磁盘读写等指标

不说原理,直接上代码

1、代码

<?php
/**
* 系统监控数据
*/
class SystemMonitor
{
/**
* 服务器运行时间
*
* @return string
*/
public function GetUpTime()
{
if (false === ($str = file_get_contents("/proc/uptime")))
return '';

$upTime = '';
$str = explode(" ", $str);
$str = trim($str[0]);
$min = $str / 60;
$hours = $min / 60;
$days = (int)($hours / 24);
$hours = $hours % 24;
$min = $min % 60;

if ($days !== 0)
{
$upTime = $days . "天";
}
if ($hours !== 0)
{
$upTime .= $hours . "小时";
}

return $upTime . $min . "分钟";
}

/**
* 内存信息
*
* @param bool $bFormat 格式化
*
* @return array
*/
public function GetMem(bool $bFormat = false)
{
if (false === ($str = file_get_contents("/proc/meminfo")))
return [];

preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $mems);
preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);

$mtotal = $mems[1][0] * 1024;
$mfree = $mems[2][0] * 1024;
$mbuffers = $buffers[1][0] * 1024;
$mcached = $mems[3][0] * 1024;
$stotal = $mems[4][0] * 1024;
$sfree = $mems[5][0] * 1024;
$mused = $mtotal - $mfree;
$sused = $stotal - $sfree;
$mrealused = $mtotal - $mfree - $mcached - $mbuffers; //真实内存使用

$rtn['mTotal'] = !$bFormat ? $mtotal : $this->size_format($mtotal, 1);
$rtn['mFree'] = !$bFormat ? $mfree : $this->size_format($mfree, 1);
$rtn['mBuffers'] = !$bFormat ? $mbuffers : $this->size_format($mbuffers, 1);
$rtn['mCached'] = !$bFormat ? $mcached : $this->size_format($mcached, 1);
$rtn['mUsed'] = !$bFormat ? ($mtotal - $mfree) : $this->size_format($mtotal - $mfree, 1);
$rtn['mPercent'] = (floatval($mtotal) != 0) ? round($mused / $mtotal * 100, 1) : 0;
$rtn['mRealUsed'] = !$bFormat ? $mrealused : $this->size_format($mrealused, 1);
$rtn['mRealFree'] = !$bFormat ? ($mtotal - $mrealused) : $this->size_format($mtotal - $mrealused, 1);//真实空闲
$rtn['mRealPercent'] = (floatval($mtotal) != 0) ? round($mrealused / $mtotal * 100, 1) : 0; //真实内存使用率
$rtn['mCachedPercent'] = (floatval($mcached) != 0) ? round($mcached / $mtotal * 100, 1) : 0; //Cached内存使用率
$rtn['swapTotal'] = !$bFormat ? $stotal : $this->size_format($stotal, 1);
$rtn['swapFree'] = !$bFormat ? $sfree : $this->size_format($sfree, 1);
$rtn['swapUsed'] = !$bFormat ? $sused : $this->size_format($sused, 1);
$rtn['swapPercent'] = (floatval($stotal) != 0) ? round($sused / $stotal * 100, 1) : 0;

return $rtn;
}

/**
* 获取CPU使用率
*
* @return bool|array
*/
public function GetCPU()
{
$cpuinfo1 = $this->GetCPUInfo();
if ($cpuinfo1)
{
sleep(1);
$cpuinfo2 = $this->GetCPUInfo();

$time = $cpuinfo2['time'] - $cpuinfo1['time'];
$total = $cpuinfo2['total'] - $cpuinfo1['total'];
$percent = round($time / $total, 4);
$percent = $percent * 100;

return [
'total' => $percent,
];
}

return [];
}

/**
* 获取系统负载
*
* @return array|false|string[]
*/
public function GetLoad()
{
if (false === ($str = file_get_contents("/proc/loadavg")))
return [];

$loads = explode(' ', $str);
if ($loads)
{
return [
'1m' => $loads[0],
'5m' => $loads[1],
'15m' => $loads[2],
];
}

return [];
}

private function GetCPUInfo()
{
if (false === ($str = file_get_contents("/proc/stat")))
return false;

$cpu = [];
$mode = "/(cpu)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)/";
preg_match_all($mode, $str, $cpu);
$total = $cpu[2][0] + $cpu[3][0] + $cpu[4][0] + $cpu[5][0] + $cpu[6][0] + $cpu[7][0] + $cpu[8][0] + $cpu[9][0];
$time = $cpu[2][0] + $cpu[3][0] + $cpu[4][0] + $cpu[6][0] + $cpu[7][0] + $cpu[8][0] + $cpu[9][0];

//or $cpu_rate = (($out[2][0] + $out[3][0]) / ($out[4][0] + $out[5][0] + $out[6][0] + $out[7][0]))*100;

return [
'total' => $total,
'time' => $time,
];
}

/**
* 获取网络数据
*
* @param bool $bFormat
*
* @return array
*/
public function GetNetwork(bool $bFormat = false)
{
$rtn = [];
$netstat = file_get_contents('/proc/net/dev');
if (false === $netstat)
{
return [];
}

$bufe = preg_split("/\n/", $netstat, -1, PREG_SPLIT_NO_EMPTY);
foreach ($bufe as $buf)
{
if (preg_match('/:/', $buf))
{
list($dev_name, $stats_list) = preg_split('/:/', $buf, 2);
$dev_name = trim($dev_name);

$stats = preg_split('/\s+/', trim($stats_list));
$rtn[$dev_name]['name'] = $dev_name;
$rtn[$dev_name]['in_rate'] = !$bFormat ? $stats[0] : $this->netSize($stats[0]);
$rtn[$dev_name]['in_packets'] = $stats[1];
$rtn[$dev_name]['in_errors'] = $stats[2];
$rtn[$dev_name]['in_drop'] = $stats[3];

$rtn[$dev_name]['out_traffic'] = !$bFormat ? $stats[8] : $this->netSize($stats[8]);
$rtn[$dev_name]['out_packets'] = $stats[9];
$rtn[$dev_name]['out_errors'] = $stats[10];
$rtn[$dev_name]['out_drop'] = $stats[11];
}
}

return $rtn;
}

/**
* 磁盘信息
*
* @param string $disk
*
* @return array
*/
public function GetDisk(string $disk='sda', string $dir='/dev/sda1')
{
$hddTotal = disk_total_space($dir);
$hddFree = disk_free_space($dir);
$hddUsed = $hddTotal - $hddFree;
$hddPercent = (floatval($hddTotal) != 0) ? round($hddUsed / $hddTotal * 100, 2) : 0;

$rtn = [
'total' => $hddTotal,
'free' => $hddFree,
'used' => $hddUsed,
'percent' => $hddPercent,
'readbytes' => 0,
'writebytes' => 0,
'readiops' => 0,
'writeiops' => 0,
];

try
{
//https://www.kernel.org/doc/Documentation/block/stat.txt
$nowTime = microtime(true);
$diskStat = file_get_contents("/sys/class/block/$disk/stat");
if ($diskStat)
{
$stats = preg_split('/\s+/', trim($diskStat));
if ($stats)
{
$cacheStats = Cache::get('mdisk:'. $disk);
if ($cacheStats)
{
$rIO = $stats[0] - $cacheStats[0];
$rSectors = $stats[2] - $cacheStats[2];
$wIO = $stats[4] - $cacheStats[4];
$wSectors = $stats[6] - $cacheStats[6];
$timeMid = $nowTime - Cache::get('mdiskt:' . $disk);

$rtn['readiops'] = round($rIO / $timeMid, 2);
$rtn['writeiops'] = round($wIO / $timeMid, 2);
$rtn['readbytes'] = round($rSectors * 512 / $timeMid, 2);
$rtn['writebytes'] = round($wSectors * 512 / $timeMid, 2);
}
Cache::set('mdisk:'. $disk, $stats, 120);
Cache::set('mdiskt:' . $disk, $nowTime, 120);
}
}
}
catch (\Throwable $e)
{

}

return $rtn;
}


private function size_format($bytes, $decimals = 2)
{
$quant = array(
'TB' => 1099511627776, // pow( 1024, 4)
'GB' => 1073741824, // pow( 1024, 3)
'MB' => 1048576, // pow( 1024, 2)
'KB' => 1024, // pow( 1024, 1)
'B ' => 1,
);

foreach ($quant as $unit => $mag)
{
if (doubleval($bytes) >= $mag)
{
return number_format($bytes / $mag, $decimals) . ' ' . $unit;
}
}

return false;
}

function netSize($size, $decimals = 2)
{
if ($size < 1024)
{
$unit = "Bbps";
}
else if ($size < 10240)
{
$size = round($size / 1024, $decimals);
$unit = "Kbps";
}
else if ($size < 102400)
{
$size = round($size / 1024, $decimals);
$unit = "Kbps";
}
else if ($size < 1048576)
{
$size = round($size / 1024, $decimals);
$unit = "Kbps";
}
else if ($size < 10485760)
{
$size = round($size / 1048576, $decimals);
$unit = "Mbps";
}
else if ($size < 104857600)
{
$size = round($size / 1048576, $decimals);
$unit = "Mbps";
}
else if ($size < 1073741824)
{
$size = round($size / 1048576, $decimals);
$unit = "Mbps";
}
else
{
$size = round($size / 1073741824, $decimals);
$unit = "Gbps";
}

$size .= $unit;

return $size;
}
}

2、测试

$systemIns = new SystemMonitor();

dump( $systemIns->GetUpTime());
dump( $systemIns->GetMem(true));
dump( $systemIns->GetCPU());
dump( $systemIns->GetLoad());
dump( $systemIns->GetNetwork(true));
dump( $systemIns->GetDisk('sda', '/mnt/volume1'));

服务器运行时间

"23小时27分钟"

内存

array:14 [
"mTotal" => "7.4 GB"
"mFree" => "6.4 GB"
"mBuffers" => "3.2 MB"
"mCached" => "607.4 MB"
"mUsed" => "1.1 GB"
"mPercent" => 14.4
"mRealUsed" => "484.3 MB"
"mRealFree" => "7.0 GB"
"mRealPercent" => 6.4
"mCachedPercent" => 8.0
"swapTotal" => "7.7 GB"
"swapFree" => "7.7 GB"
"swapUsed" => false
"swapPercent" => 0.0
]

CPU

array:1 [
"total" => 0.25
]

负载

array:3 [
"1m" => "2.50"
"5m" => "0.89"
"15m" => "0.32"
]

网络

array:3 [
"lo" => array:9 [
"name" => "lo"
"in_rate" => "840Bbps"
"in_packets" => "12"
"in_errors" => "0"
"in_drop" => "0"
"out_traffic" => "840Bbps"
"out_packets" => "12"
"out_errors" => "0"
"out_drop" => "0"
]
"wlp1s0" => array:9 [
"name" => "wlp1s0"
"in_rate" => "0Bbps"
"in_packets" => "0"
"in_errors" => "0"
"in_drop" => "0"
"out_traffic" => "0Bbps"
"out_packets" => "0"
"out_errors" => "0"
"out_drop" => "0"
]
"enp2s0" => array:9 [
"name" => "enp2s0"
"in_rate" => "172.16Mbps"
"in_packets" => "287089"
"in_errors" => "0"
"in_drop" => "0"
"out_traffic" => "13.53Mbps"
"out_packets" => "155388"
"out_errors" => "0"
"out_drop" => "0"
]
]

磁盘

array:8 [
"total" => 882289623040.0
"free" => 847537909760.0
"used" => 34751713280.0
"percent" => 3.94
"readbytes" => 0
"writebytes" => 0
"readiops" => 0
"writeiops" => 0
]

在线源码:
​​​https://github.com/hacfins/thinkphp6-api/blob/develop/extend/command/SystemMonitor.php​​

几个函数来自ThinkPHP,直接复制源码简单调整即可



举报

相关推荐

0 条评论