PHP 获取linux服务器性能CPU内存硬盘进程等使用率
Posted ylong52
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 获取linux服务器性能CPU内存硬盘进程等使用率相关的知识,希望对你有一定的参考价值。
数据库配置文件: conn.php <?php define("MONITORED_IP", "172.16.0.191"); //被监控的服务器IP地址 也就是本机地址 define("DB_SERVER", "172.16.7.2"); //存放数据的服务器IP地址 define("DB_USER", "root"); define("DB_PWD","111111"); define("DB_NMAE","performance"); class MyConnect{ public function connect($db_server,$db_user,$db_pwd,$db_name){ $conn = mysql_connect($db_server,$db_user,$db_pwd); if (!$conn){ die(‘连接数据库失败: ‘ . mysql_error()); } $flag = mysql_select_db($db_name,$conn); if(!$flag){ echo "<p align=‘center‘>数据库连接出错!</p>";exit(); }else{ mysql_query("SET NAMES UTF8"); } } } ?> 获取服务器性能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); return array(‘cpu_usage‘=>$cpu_usage,‘mem_usage‘=>$mem_usage,‘hd_avail‘=>$hd_avail,‘hd_usage‘=>$hd_usage,‘tast_running‘=>$tast_running,‘detection_time‘=>$detection_time); } //echo date("Y-m-d H:i:s",time())."<br>"; $status=get_used_status(); $sql = "insert into performance(ip,cpu_usage,mem_usage,hd_avail,hd_usage,tast_running,detection_time) "; $sql .= " value(‘".MONITORED_IP."‘,‘".$status[‘cpu_usage‘]."‘,‘".$status[‘mem_usage‘]."‘,‘".$status[‘hd_avail‘]."‘,‘".$status[‘hd_usage‘]."‘,‘".$status[‘tast_running‘]."‘,‘".$status[‘detection_time‘]."‘)"; $query = mysql_query($sql) or die("SQL 语句执行失败!"); unset($status); //echo date("Y-m-d H:i:s",time())."<br>"; ?> 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); return array(‘cpu_usage‘=>$cpu_usage,‘mem_usage‘=>$mem_usage,‘hd_avail‘=>$hd_avail,‘hd_usage‘=>$hd_usage,‘tast_running‘=>$tast_running,‘detection_time‘=>$detection_time); } //echo date("Y-m-d H:i:s",time())."<br>"; $status=get_used_status(); $sql = "insert into performance(ip,cpu_usage,mem_usage,hd_avail,hd_usage,tast_running,detection_time) "; $sql .= " value(‘".MONITORED_IP."‘,‘".$status[‘cpu_usage‘]."‘,‘".$status[‘mem_usage‘]."‘,‘".$status[‘hd_avail‘]."‘,‘".$status[‘hd_usage‘]."‘,‘".$status[‘tast_running‘]."‘,‘".$status[‘detection_time‘]."‘)"; $query = mysql_query($sql) or die("SQL 语句执行失败!"); unset($status); //echo date("Y-m-d H:i:s",time())."<br>"; ?>
以上是关于PHP 获取linux服务器性能CPU内存硬盘进程等使用率的主要内容,如果未能解决你的问题,请参考以下文章
如何使用PHP实时监控Linux服务器的cpu,内存,硬盘信息