首頁 > 後端開發 > php教程 > php取得linux伺服器CPU、記憶體、硬碟使用率的實作程式碼

php取得linux伺服器CPU、記憶體、硬碟使用率的實作程式碼

WBOY
發布: 2016-07-25 08:59:31
原創
2999 人瀏覽過
  1. define("MONITORED_IP", "172.16.0.191"); //被監控的伺服器🎜> define("DB_SERVER", "172.16.7.2"); //存放資料的伺服器IP位址
  2. define("DB_USER", "root");
  3. define("DB_PWD","111111");
  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. $connn = mysql_connect($db_server,$db_user,$db_pwd);
  9. if (!$conn){
  10. die('連線資料庫失敗: ' . mysql_error());
  11. }
  12. $flag = mysql_select_db($db_name,$conn);
  13. if(!$flag){
  14. echo "

    資料庫連線出錯!

    ";exit();
  15. } else{
  16. mysql_query("SET NAMES UTF8");
  17. }
  18. }
  19. }
  20. ?>
複製程式碼

複製程式碼2、取得伺服器效能資料的檔案get_used_status.php
  1. /**

  2. * 取得伺服器效能CPU、記憶體、硬碟等使用率
  3. * Edit bbs.it-home.org
  4. */
  5. /*連線資料*/
  6. include("conn.php");
  7. $obj_MyConnect = new MyConnect();
  8. $obj_MyConnect -> connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);
  9. /*連接資料end>/
  10. function get_used_status(){
  11. $fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//取得某一時刻系統cpu與記憶體使用量
  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]); //行程數組

  19. $cpu_info = explode(",",$sys_info[4]); //CPU佔有量數組
  20. $mem_info = explode(",",$sys_info[5]); //記憶體佔有量數組
  21. //正在運行的進程數

  22. $tast_running = trim(trim($tast_info[1],'running'));
  23. //CPU佔有量
  24. $cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //百分比
  25. //記憶體佔有量
  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); //百分比
  29. /*硬碟使用率begin*/

  30. $fp = popen('df -lh | grep -E "^(/)"',"r");
  31. $rs = fread($fp,1024);
  32. pclose($fp) ;
  33. $rs = preg_replace("/s{2,}/",' ',$rs); //把多個空格換成「_」
  34. $hd = explode(" ",$rs );
  35. $hd_avail = trim($hd[3],'G'); //磁碟可用空間大小單位G
  36. $hd_usage = trim($hd[4],'%'); //掛載點百分比
  37. //print_r($hd);
  38. /*硬碟使用率end*/
  39. //偵測時間
  40. $fp = popen("date +"%Y -%m-%d %H:%M"","r");
  41. $rs = fread($fp,1024);
  42. pclose($fp);
  43. $detection_time = trim( $rs);
  44. /*取得IP位址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) ); //把多個空格換成「_」
  50. $rs = explode(" ",$rs);
  51. $ip = trim($rs[1],'addr:');
  52. */
  53. /*取得IP位址end*/
  54. /*
  55. $file_name = "/tmp/data.txt"; // 絕對路徑: homedata.dat
  56. $file_pointer = fopen( $file_name, "a+"); // "w"是一種模式,詳見後面
  57. fwrite($file_pointer,$ip); // 先把檔案剪下為0位元組大小, 然後寫入
  58. fclose($file_pointer); // 結束
  59. */
  60. 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);
  61. }
  62. //echo date("Y-m-d H:i:s",time(timei:s",time(time ))."
    ";
  63. $status=get_used_status();
  64. $sql = "insert into performance(ip,cpu_usage,mem_usage,hd_avail,hd_usage,dad_running) ";
  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 語句執行失敗!");
  67. unset($status);
  68. //echo date(" Y-m-d H:i:s",time())."
    ";
  69. ?>
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板