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

Source: Internet
Author: User
    1. Define ("Monitored_ip", "172.16.0.191"); The server IP address that is being monitored is the native address
    2. Define ("Db_server", "172.16.7.2"); The server IP address where the data resides
    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 (' Connection database failed: '. 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 server performance data file get_used_status.php

  1. /**

  2. * Get server performance CPU, memory, hard disk usage rate
  3. * Edit bbs.it-home.org
  4. */
  5. /* Connect 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 a moment of system CPU and memory usage
  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]); Array of CPU occupancy
  20. $mem _info = Explode (",", $sys _info[5]); Memory Occupancy Array

  21. Number of processes that are running

  22. $tast _running = Trim (Trim ($tast _info[1], ' running '));
  23. CPU occupancy
  24. $cpu _usage = Trim (Trim ($cpu _info[0], ' CPU (s): '), '%us '); Percentage
  25. Memory occupancy
  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); Percentage
  29. /* Hard Drive utilization 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 free space Size Unit g
  36. $hd _usage = Trim ($HD [4], '% '); mount point Percentage
  37. Print_r ($HD);
  38. /* Hard Drive utilization 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 address end*/
  54. /*
  55. $file _name = "/tmp/data.txt"; Absolute path: Homedata.dat
  56. $file _pointer = fopen ($file _name, "A +"); "W" is a pattern, see later
  57. Fwrite ($file _pointer, $IP); First cut the file to a size of 0 bytes, then write
  58. Fclose ($file _pointer); End
  59. */
  60. 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);
  61. }
  62. echo Date ("Y-m-d h:i:s", Time ()). "
    ";
  63. $status =get_used_status ();
  64. $sql = "INSERT into performance (Ip,cpu_usage,mem_usage,hd_avail,hd_usage,tast_running,detection_time)";
  65. $sql. = "Value ('". Monitored_ip. "', '". $status [' Cpu_usage ']. "', '". $status [' Mem_usage ']. "', '". $status [' Hd_avail ']. "', '". $status [' HD _usage ']. "', '". $status [' tast_running ']. "', '". $status [' Detection_time ']. "')";
  66. $query = mysql_query ($sql) or Die ("SQL statement execution failed!");
  67. Unset ($status);
  68. echo Date ("Y-m-d h:i:s", Time ()). "
    ";
  69. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.