Home > Backend Development > PHP Tutorial > PHP implementation code to obtain Linux server CPU, memory, and hard disk usage

PHP implementation code to obtain Linux server CPU, memory, and hard disk usage

WBOY
Release: 2016-07-25 08:59:31
Original
2999 people have browsed it
  1. define("MONITORED_IP", "172.16.0.191"); //The monitored server IP address is also the local address
  2. define("DB_SERVER", "172.16.7.2"); //The IP address of the server where the data is stored
  3. define("DB_USER", "root");
  4. define("DB_PWD","111111");
  5. define("DB_NMAE","performance");
  6. class MyConnect{
  7. public function connect($db_server,$db_user,$db_pwd,$db_name){
  8. $conn = mysql_connect($db_server,$db_user,$db_pwd);
  9. if (!$conn){
  10. die('Failed to connect to database: ' . mysql_error());
  11. }
  12. $flag = mysql_select_db($db_name,$conn);
  13. if(!$flag){
  14. echo "

    Database connection error!";exit();

  15. }else{
  16. mysql_query("SET NAMES UTF8");
  17. }
  18. }
  19. }
  20. ?>
Copy code

2. Get the file get_used_status for server performance data. php

  1. /**

  2. * Get server performance CPU, memory, hard disk and other usage rates
  3. * Edit bbs.it-home.org
  4. */
  5. /*Connection data begin*/
  6. include("conn.php");
  7. $obj_MyConnect = new MyConnect() ;
  8. $obj_MyConnect -> connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);
  9. /*Connect data end*/
  10. function get_used_status(){
  11. $fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//Get the system cpu and memory usage at a certain moment
  12. $rs = "";
  13. while(!feof($fp)){
  14. $rs .= fread($fp,1024);
  15. }
  16. pclose($fp);
  17. $sys_info = explode("n",$rs);

  18. $tast_info = explode(", ",$sys_info[3]);//Process array

  19. $cpu_info = explode(",",$sys_info[4]); //CPU occupancy array
  20. $mem_info = explode(",",$sys_info[5 ]); //Memory occupancy array

  21. //Number of running processes

  22. $tast_running = trim(trim($tast_info[1],'running'));
  23. / /CPU usage
  24. $cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //Percentage
  25. //Memory usage
  26. $mem_total = trim(trim ($mem_info[0],'Mem: '),'k total');
  27. $mem_used = trim($mem_info[1],'k used');
  28. $mem_usage = round(100*intval($mem_used) /intval($mem_total),2); //Percent
  29. /*Hard disk usage begin*/

  30. $fp = popen('df -lh | grep -E "^(/ )"',"r");
  31. $rs = fread($fp,1024);
  32. pclose($fp);
  33. $rs = preg_replace("/s{2,}/",' ',$rs) ; //Replace multiple spaces with "_"
  34. $hd = explode(" ",$rs);
  35. $hd_avail = trim($hd[3],'G'); //Disk available space size unit G
  36. $hd_usage = trim($hd[4],'%'); //Mount point percentage
  37. //print_r($hd);
  38. /*Hard disk usage end*/
  39. //Detection time
  40. $fp = popen("date +"%Y-%m-%d %H:%M"","r");
  41. $rs = fread($fp,1024);
  42. pclose($fp);
  43. $detection_time = trim($rs);
  44. /*Get IP address begin*/
  45. /*
  46. $fp = popen('ifconfig eth0 | grep -E "(inet addr)"','r');
  47. $rs = fread($fp,1024);
  48. pclose($fp);
  49. $rs = preg_replace("/s{2,}/",' ',trim($rs)); //Replace multiple spaces with " _”
  50. $rs = explode(" ",$rs);
  51. $ip = trim($rs[1],'addr:');
  52. */
  53. /*Get IP addressend*/
  54. /*
  55. $ file_name = "/tmp/data.txt"; // Absolute path: homedata.dat
  56. $file_pointer = fopen($file_name, "a+"); // "w" is a mode, see below for details
  57. fwrite($ file_pointer,$ip); // First cut the file to 0 bytes, then write
  58. fclose($file_pointer); // End
  59. */
  60. return array('cpu_usage'=>$cpu_usage,' mem_usage'=> /echo date("Y-m-d H:i:s",time())."
    ";
  61. $status=get_used_status();
  62. $sql = "insert into performance(ip,cpu_usage,mem_usage, hd_avail,hd_usage,tast_running,detection_time) ";
  63. $sql .= " value('".MONITORED_IP."','".$status['cpu_usage']."','".$status['mem_usage'] ."','".$status['hd_avail']."','".$status['hd_usage']."','".$status['tast_running']."','".$ status['detection_time']."')";
  64. $query = mysql_query($sql) or die("SQL statement execution failed!");
  65. unset($status);
  66. //echo date("Y-m-d H :i:s",time())."
    ";
  67. ?>

  68. Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template