-
- define("MONITORED_IP", "172.16.0.191"); //The monitored server IP address is also the local address
- define("DB_SERVER", "172.16.7.2"); //The IP address of the server where the data is stored
- 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('Failed to connect to database: ' . mysql_error());
- }
- $flag = mysql_select_db($db_name,$conn);
- if(!$flag){
- echo "
Database connection error! p>";exit();
- }else{
- mysql_query("SET NAMES UTF8");
- }
- }
- }
- ?>
Copy code
2. Get the file get_used_status for server performance data. php
-
-
/** - * Get server performance CPU, memory, hard disk and other usage rates
- * Edit bbs.it-home.org
- */
- /*Connection data begin*/
- include("conn.php");
- $obj_MyConnect = new MyConnect() ;
- $obj_MyConnect -> connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);
- /*Connect data end*/
-
- function get_used_status(){
- $fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//Get the system cpu and memory usage at a certain moment
- $rs = "";
- while(!feof($fp)){
- $rs .= fread($fp,1024);
- }
- pclose($fp);
- $sys_info = explode("n",$rs);
$tast_info = explode(", ",$sys_info[3]);//Process array
- $cpu_info = explode(",",$sys_info[4]); //CPU occupancy array
- $mem_info = explode(",",$sys_info[5 ]); //Memory occupancy array
//Number of running processes
- $tast_running = trim(trim($tast_info[1],'running'));
-
-
- / /CPU usage
- $cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //Percentage
-
- //Memory usage
- $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); //Percent
-
/*Hard disk usage begin*/
- $fp = popen('df -lh | grep -E "^(/ )"',"r");
- $rs = fread($fp,1024);
- pclose($fp);
- $rs = preg_replace("/s{2,}/",' ',$rs) ; //Replace multiple spaces with "_"
- $hd = explode(" ",$rs);
- $hd_avail = trim($hd[3],'G'); //Disk available space size unit G
- $hd_usage = trim($hd[4],'%'); //Mount point percentage
- //print_r($hd);
- /*Hard disk usage end*/
-
- //Detection time
- $fp = popen("date +"%Y-%m-%d %H:%M"","r");
- $rs = fread($fp,1024);
- pclose($fp);
- $detection_time = trim($rs);
-
- /*Get IP address begin*/
- /*
- $fp = popen('ifconfig eth0 | grep -E "(inet addr)"','r');
- $rs = fread($fp,1024);
- pclose($fp);
- $rs = preg_replace("/s{2,}/",' ',trim($rs)); //Replace multiple spaces with " _”
- $rs = explode(" ",$rs);
- $ip = trim($rs[1],'addr:');
- */
- /*Get IP addressend*/
- /*
- $ file_name = "/tmp/data.txt"; // Absolute path: homedata.dat
- $file_pointer = fopen($file_name, "a+"); // "w" is a mode, see below for details
- fwrite($ file_pointer,$ip); // First cut the file to 0 bytes, then write
- fclose($file_pointer); // End
- */
-
- return array('cpu_usage'=>$cpu_usage,' mem_usage'=> /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 statement execution failed!");
- unset($status);
-
- //echo date("Y-m-d H :i:s",time())."
";
- ?>
-
-
-
- Copy code
|