2个回答
展开全部
<?php
$str = shell_exec('more /proc/stat');
$pattern = "/(cpu[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($pattern, $str, $out);
echo "共有".count($out[1])."个CPU,每个CPU利用率如下:<br>";
for($n=0;$n<count($out[1]);$n++)
{
echo $out[1][$n]."=".(100*($out[1][$n]+$out[2][$n]+$out[3][$n])/($out[4][$n]+$out[5][$n]+$out[6][$n]+$out[7][$n]))."%<br>";
}
?>
//////////////////////////////////////////////////////////////////////
<?php
$str = shell_exec('more /proc/meminfo');
$pattern = "/(.+):\s*([0-9]+)/";
preg_match_all($pattern, $str, $out);
echo "物理内存总量:".$out[2][0]."<br>";
echo "已使用的内存:".$out[2][1]."<br>";
echo "-----------------------------------------<br>";
echo "内存使用率:".(100*($out[2][0]-$out[2][1])/$out[2][0])."%<br>";
?>
//////////////////////////////////////////////////////////////////////
<?php
$str = shell_exec('more /proc/net/dev');
$pattern = "/(eth[0-9]+):\s*([0-9]+)\s+([0-9]+)\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($pattern, $str, $out);
echo "共有".count($out[1])."个网络接口,每个网络接口利用率如下:<br>";
for($n=0;$n<count($out[1]);$n++)
{
echo $out[1][$n].":收到 ".$out[3][$n]." 个数据包,发送 ".$out[11][$n]." 个数据包<br>";
}
?>
$str = shell_exec('more /proc/stat');
$pattern = "/(cpu[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($pattern, $str, $out);
echo "共有".count($out[1])."个CPU,每个CPU利用率如下:<br>";
for($n=0;$n<count($out[1]);$n++)
{
echo $out[1][$n]."=".(100*($out[1][$n]+$out[2][$n]+$out[3][$n])/($out[4][$n]+$out[5][$n]+$out[6][$n]+$out[7][$n]))."%<br>";
}
?>
//////////////////////////////////////////////////////////////////////
<?php
$str = shell_exec('more /proc/meminfo');
$pattern = "/(.+):\s*([0-9]+)/";
preg_match_all($pattern, $str, $out);
echo "物理内存总量:".$out[2][0]."<br>";
echo "已使用的内存:".$out[2][1]."<br>";
echo "-----------------------------------------<br>";
echo "内存使用率:".(100*($out[2][0]-$out[2][1])/$out[2][0])."%<br>";
?>
//////////////////////////////////////////////////////////////////////
<?php
$str = shell_exec('more /proc/net/dev');
$pattern = "/(eth[0-9]+):\s*([0-9]+)\s+([0-9]+)\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($pattern, $str, $out);
echo "共有".count($out[1])."个网络接口,每个网络接口利用率如下:<br>";
for($n=0;$n<count($out[1]);$n++)
{
echo $out[1][$n].":收到 ".$out[3][$n]." 个数据包,发送 ".$out[11][$n]." 个数据包<br>";
}
?>
2016-05-12 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
参考如下:
获取服务器性能CPU、内存、硬盘等使用率 get_used_status.php
<?php
include("conn.php");
$obj_MyConnect = new MyConnect();
$obj_MyConnect -> connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);
function get_used_status(){
$fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//获取某一时刻系统cpu和内存使用情况
$rs = "";
while(!feof($fp)){
$rs .= fread($fp,1024);
}
pclose($fp);
$sys_info = explode("\n",$rs);
$tast_info = explode(",",$sys_info[3]);//进程 数组
$cpu_info = explode(",",$sys_info[4]); //CPU占有量 数组
$mem_info = explode(",",$sys_info[5]); //内存占有量 数组
//正在运行的进程数
$tast_running = trim(trim($tast_info[1],'running'));
//CPU占有量
$cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //百分比
//内存占有量
$mem_total = trim(trim($mem_info[0],'Mem: '),'k total');
$mem_used = trim($mem_info[1],'k used');
$mem_usage = round(100*intval($mem_used)/intval($mem_total),2); //百分比
$fp = popen('df -lh | grep -E "^(/)"',"r");
$rs = fread($fp,1024);
pclose($fp);
$rs = preg_replace("/\s{2,}/",' ',$rs); //把多个空格换成 “_”
$hd = explode(" ",$rs);
$hd_avail = trim($hd[3],'G'); //磁盘可用空间大小 单位G
$hd_usage = trim($hd[4],'%'); //挂载点 百分比
//print_r($hd);
//检测时间
$fp = popen("date +"%Y-%m-%d %H:%M"","r");
$rs = fread($fp,1024);
pclose($fp);
$detection_time = trim($rs);
获取服务器性能CPU、内存、硬盘等使用率 get_used_status.php
<?php
include("conn.php");
$obj_MyConnect = new MyConnect();
$obj_MyConnect -> connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);
function get_used_status(){
$fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//获取某一时刻系统cpu和内存使用情况
$rs = "";
while(!feof($fp)){
$rs .= fread($fp,1024);
}
pclose($fp);
$sys_info = explode("\n",$rs);
$tast_info = explode(",",$sys_info[3]);//进程 数组
$cpu_info = explode(",",$sys_info[4]); //CPU占有量 数组
$mem_info = explode(",",$sys_info[5]); //内存占有量 数组
//正在运行的进程数
$tast_running = trim(trim($tast_info[1],'running'));
//CPU占有量
$cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //百分比
//内存占有量
$mem_total = trim(trim($mem_info[0],'Mem: '),'k total');
$mem_used = trim($mem_info[1],'k used');
$mem_usage = round(100*intval($mem_used)/intval($mem_total),2); //百分比
$fp = popen('df -lh | grep -E "^(/)"',"r");
$rs = fread($fp,1024);
pclose($fp);
$rs = preg_replace("/\s{2,}/",' ',$rs); //把多个空格换成 “_”
$hd = explode(" ",$rs);
$hd_avail = trim($hd[3],'G'); //磁盘可用空间大小 单位G
$hd_usage = trim($hd[4],'%'); //挂载点 百分比
//print_r($hd);
//检测时间
$fp = popen("date +"%Y-%m-%d %H:%M"","r");
$rs = fread($fp,1024);
pclose($fp);
$detection_time = trim($rs);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询