linux php cpu,获取Linux服务器性能CPU、内存、硬盘等使用率 PHP
數(shù)據(jù)庫(kù)配置文件: conn.php
define("MONITORED_IP", "172.16.0.191");? //被監(jiān)控的服務(wù)器IP地址? 也就是本機(jī)地址
define("DB_SERVER", "172.16.7.2");?????? //存放數(shù)據(jù)的服務(wù)器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('連接數(shù)據(jù)庫(kù)失敗: ' . mysql_error());
}
$flag = mysql_select_db($db_name,$conn);
if(!$flag){
echo "
數(shù)據(jù)庫(kù)連接出錯(cuò)!
";exit();}else{
mysql_query("SET NAMES UTF8");
}
}
}
?>
獲取服務(wù)器性能CPU、內(nèi)存、硬盤(pán)等使用率? get_used_status.php
/*連接數(shù)據(jù) begin*/
include("conn.php");
$obj_MyConnect = new MyConnect();
$obj_MyConnect -> connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);
/*連接數(shù)據(jù) end*/
function get_used_status(){
$fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//獲取某一時(shí)刻系統(tǒng)cpu和內(nèi)存使用情況
$rs = "";
while(!feof($fp)){
$rs .= fread($fp,1024);
}
pclose($fp);
$sys_info = explode("\n",$rs);
$tast_info = explode(",",$sys_info[3]);//進(jìn)程 數(shù)組
$cpu_info = explode(",",$sys_info[4]);? //CPU占有量? 數(shù)組
$mem_info = explode(",",$sys_info[5]);?//內(nèi)存占有量 數(shù)組
//正在運(yùn)行的進(jìn)程數(shù)
$tast_running = trim(trim($tast_info[1],'running'));
//CPU占有量
$cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us');? //百分比
//內(nèi)存占有量
$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);? //百分比
/*硬盤(pán)使用率 begin*/
$fp = popen('df -lh | grep -E "^(/)"',"r");
$rs = fread($fp,1024);
pclose($fp);
$rs = preg_replace("/\s{2,}/",' ',$rs);? //把多個(gè)空格換成 “_”
$hd = explode(" ",$rs);
$hd_avail = trim($hd[3],'G'); //磁盤(pán)可用空間大小 單位G
$hd_usage = trim($hd[4],'%'); //掛載點(diǎn) 百分比
//print_r($hd);
/*硬盤(pán)使用率 end*/
//檢測(cè)時(shí)間
$fp = popen("date +\"%Y-%m-%d %H:%M\"","r");
$rs = fread($fp,1024);
pclose($fp);
$detection_time = trim($rs);
/*獲取IP地址? begin*/
/*
$fp = popen('ifconfig eth0 | grep -E "(inet addr)"','r');
$rs = fread($fp,1024);
pclose($fp);
$rs = preg_replace("/\s{2,}/",' ',trim($rs));? //把多個(gè)空格換成 “_”
$rs = explode(" ",$rs);
$ip = trim($rs[1],'addr:');
*/
/*獲取IP地址 end*/
/*
$file_name = "/tmp/data.txt"; // 絕對(duì)路徑: homedata.dat
$file_pointer = fopen($file_name, "a+"); // "w"是一種模式,詳見(jiàn)后面
fwrite($file_pointer,$ip); // 先把文件剪切為0字節(jié)大小, 然后寫(xiě)入
fclose($file_pointer); // 結(jié)束
*/
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())."
";
$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 語(yǔ)句執(zhí)行失敗!");
unset($status);
//echo date("Y-m-d H:i:s",time())."
";
?>
總結(jié)
以上是生活随笔為你收集整理的linux php cpu,获取Linux服务器性能CPU、内存、硬盘等使用率 PHP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 苹果邀《钢铁侠》导演做AR头显视频:硬件
- 下一篇: android jni release,