- Define ("Monitored_ip", "172.16.0.191"); The server IP address that is being monitored is the native address
- Define ("Db_server", "172.16.7.2"); The server IP address where the data resides
- 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 (' Connection database failed: '. mysql_error ());
- }
- $flag = mysql_select_db ($db _name, $conn);
- if (! $flag) {
- echo "
Database connection Error! "; exit ();
- }else{
- mysql_query ("SET NAMES UTF8");
- }
- }
- }
- ?>
Copy Code2. Get the server performance data file get_used_status.php
/**
- * Get server performance CPU, memory, hard disk usage rate
- * Edit bbs.it-home.org
- */
- /* Connect 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 a moment of system CPU and memory usage
- $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]); Array of CPU occupancy
- $mem _info = Explode (",", $sys _info[5]); Memory Occupancy Array
Number of processes that are running
- $tast _running = Trim (Trim ($tast _info[1], ' running '));
- CPU occupancy
- $cpu _usage = Trim (Trim ($cpu _info[0], ' CPU (s): '), '%us '); Percentage
- Memory occupancy
- $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); Percentage
/* Hard Drive utilization 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 free space Size Unit g
- $hd _usage = Trim ($HD [4], '% '); mount point Percentage
- Print_r ($HD);
- /* Hard Drive utilization 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 address end*/
- /*
- $file _name = "/tmp/data.txt"; Absolute path: Homedata.dat
- $file _pointer = fopen ($file _name, "A +"); "W" is a pattern, see later
- Fwrite ($file _pointer, $IP); First cut the file to a size of 0 bytes, then write
- Fclose ($file _pointer); End
- */
- The return array (' cpu_usage ' = = $cpu _usage, ' mem_usage ' and ' _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 statement execution failed!");
- Unset ($status);
- echo Date ("Y-m-d h:i:s", Time ()). "
";
- ?>
Copy Code |