获取服务器性能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)
在cmd下 用批处理语句获取windows的CPU 内存使用情况的语句,找下。然后php有执行 OS 原生脚本的函数,跟你在CMD下执行一样。
执行完,获取返回的信息即可。
<?phpheader("Content-type: text/html charset=utf-8")
//获取当前运行脚本占用内存
function getMemeoryByScript(){
//首先判断系统是否支持memory_get_usage函数
if(function_exists('memory_get_usage')){
return memory_get_usage()
}else{
$pid = getmypid()
//判断是否为Windows服务器
if (strtoupper(substr(PHP_OS,0,3))==='WIN'){
//命令查询pid所占的内存
exec('tasklist /FI "PID eq ' . $pid . '" /FO LIST', $output)
// 取出数字,然后转换为M(默认结果为k)
return preg_replace('/[^0-9]/', '', $output[5]) * 1024
}else{
exec("ps -eo%mem,rss,pid | grep $pid", $output)
$output = explode(" ", $output[0])
return $output[1] * 1024
}
}
}
echo getMemeoryByScript()
//325288
?>
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)