-
-
define("MONITORED_IP", "172.16.0.191"); //被監控的伺服器🎜> define("DB_SERVER", "172.16.7.2"); //存放資料的伺服器IP位址
- define("DB_USER", "root");
- define("DB_PWD","111111");
- define("DB_PWD","111111");
- define("DB_NMAE","performance");
-
- class MyConnect{
- public function connect($db_server,$db_user,$db_pwd,$db_name){
-
- $connn = mysql_connect($db_server,$db_user,$db_pwd);
- if (!$conn){
- die('連線資料庫失敗: ' . mysql_error());
- }
- $flag = mysql_select_db($db_name,$conn);
- if(!$flag){
- echo "
資料庫連線出錯! ";exit();
- } else{
- mysql_query("SET NAMES UTF8");
- }
- }
- }
- ?>
複製程式碼
複製程式碼2、取得伺服器效能資料的檔案get_used_status.php
-
-
-
/**
- * 取得伺服器效能CPU、記憶體、硬碟等使用率
- * Edit bbs.it-home.org
- */
- /*連線資料*/
- include("conn.php");
- $obj_MyConnect = new MyConnect();
- $obj_MyConnect -> connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);
- /*連接資料end>/
-
- 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); //百分比
-
/*硬碟使用率begin*/
- $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);
- /*硬碟使用率end*/
-
- //偵測時間
- $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) ); //把多個空格換成「_」
- $rs = explode(" ",$rs);
- $ip = trim($rs[1],'addr:');
- */
- /*取得IP位址end*/
- /*
- $file_name = "/tmp/data.txt"; // 絕對路徑: homedata.dat
- $file_pointer = fopen( $file_name, "a+"); // "w"是一種模式,詳見後面
- fwrite($file_pointer,$ip); // 先把檔案剪下為0位元組大小, 然後寫入
- fclose($file_pointer); // 結束
- */
-
- return array('cpu_usage'=>$cpu_usage,'mem_usage'=>$mem_usage,'hd_avail'pu_usage,'mem_usage'=>$mem_usage,'hd_avail'pu_usage, avail' hd_usage'=>$hd_usage,'tast_running'=>$tast_running,'detection_time'=>$detection_time);
- }
-
- //echo date("Y-m-d H:i:s",time(timei:s",time(time ))."
";
-
- $status=get_used_status();
-
- $sql = "insert into performance(ip,cpu_usage,mem_usage,hd_avail,hd_usage,dad_running) ";
- $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())."
";
- ?>
複製程式碼
|