欢迎来到DESIPO.COM,外贸电子商务整合专家!登录 | 免费注册 ?
会员优惠 | 软件下载 | 客服中心 | 收藏本站 | 购物车[0件]


一个CPU和内存使用的获取PHP函数

编辑时间2019年 12月 20日 查看全部评论 写评论
一个CPU和内存使用的获取PHP函数

一个CPU和内存使用的获取函数

function amh_module_init_cpu()
{
$data = array();
$cpuinfo = file_get_contents('/proc/cpuinfo');
$physical_list = array();
$physical_sum = 0;
$cores_sum = 0;
$siblings_sum = 0;

preg_match("/model name\s*:(.*)/", $cpuinfo, $name);
preg_match("/vendor_id\s*:(.*)/", $cpuinfo, $vendor);
preg_match("/cpu family\s*:(.*)/", $cpuinfo, $family);
preg_match("/cpu MHz\s*:(.*)/", $cpuinfo, $MHz);
preg_match("/cache size\s*:(.*)/", $cpuinfo, $cache);
$name = isset($name[1]) ? $name[1] : 'No';
$vendor = isset($vendor[1]) ? $vendor[1] : 'No';
$family = isset($family[1]) ? $family[1] : 'No';
$MHz = isset($MHz[1]) ? number_format($MHz[1]/1000, 2) : 'No';
$cache = isset($cache[1]) ? $cache[1] : 'No';

$p_list = explode("\nprocessor", $cpuinfo);
foreach ($p_list as $key=>$val)
{
preg_match("/physical id\s*:(.*)/", $val, $physical);
preg_match("/cpu cores\s*:(.*)/", $val, $cores);
preg_match("/siblings\s*:(.*)/", $val, $siblings);
if (isset($physical[1]))
{
if (!in_array($physical[1], $physical_list))
{
$physical_sum += 1;
if (isset($cores[1]))
$cores_sum += $cores[1];

if (isset($siblings[1]))
$siblings_sum += $siblings[1];
}
$physical_list[] = $physical[1];
}
}
$data['name'] = $name;
$data['physical_sum'] = $physical_sum > 0 ? $physical_sum : 1;
$data['cores_sum'] = $cores_sum > 0 ? $cores_sum : 1;
$data['siblings_sum'] = $siblings_sum > 0 ? $siblings_sum : 1;

$data['MHz'] = $MHz;
$data['cache'] = $cache;
$data['vendor'] = $vendor;
$data['family'] = $family;

$use_cpu = 0.1;
$result = explode("\n", shell_exec('ps aux'));
foreach ($result as $key=>$val)
{
$val = preg_replace("/\s+/", " ", $val);
$val = (explode(' ', $val));
$use_cpu += isset($val[2]) ? $val[2] : 0;
}
$use_cpu = $data['siblings_sum'] > 0 ? ($use_cpu / $data['siblings_sum']) : $use_cpu;
$use_cpu = $use_cpu > 100 ? 99 : $use_cpu; // 文字值

$usp_cpu = $use_cpu < 3 ? 3 : $use_cpu; // 图表值
$usp_cpu = $usp_cpu > 100 ? 99 : $usp_cpu;
$free_cpu = 100 - $use_cpu;

$data['usp_cpu'] = $usp_cpu;
$data['free_cpu'] = $free_cpu;

$result = explode("\n", shell_exec('free -m'));
$mem_list = preg_replace("/\s+/", " ", $result[1]);
$mem_arr = explode(' ', $mem_list);
$data['total'] = $mem_arr[1];
$data['used'] = (strpos($result[0], 'buff/cache') !== false) ? $mem_arr[2] : ($mem_arr[2] - $mem_arr[5] - $mem_arr[6]);
$data['used_p'] = number_format($data['used'] / $data['total'], 2) * 100;
$data['free'] = $data['total'] - $data['used'];
$data['free_p'] = 100-$data['used_p'];
$data['cached'] = (strpos($result[0], 'buff/cache') !== false) ? $mem_arr[5] : ($mem_arr[5] + $mem_arr[6]);
return $data;
}
标签:
没有评论
Copyright © 2012-2013 迪赛普科技 版权所有

合作邮箱:admin@desipo.com 合作QQ:76052083