美高梅开户-美高梅官方网站开户

您的位置:美高梅开户 > 美高梅开户 > 获取Linux服务器性能CPU

获取Linux服务器性能CPU

发布时间:2019-11-24 12:01编辑:美高梅开户浏览(84)

    简单的php获取linux服务器状态的代码,不多说-直接上函数:

    数据库配置文件: conn.php

    复制代码 代码如下:function get_used_status(){ $fp = popen('top -b -n 2 | grep -E "^;//获取某一时刻系统cpu和内存使用情况 $rs = ""; while{ $rs .= fread; } pclose; $sys_info = explode;

    <?php

    $tast_info = explode;//进程 数组 $cpu_info = explode; //CPU占有量 数组 $mem_info = explode; //内存占有量 数组

    define("MONITORED_IP", "172.16.0.191");  //被监控的服务器IP地址  也就是本机地址
    define("DB_SERVER", "172.16.7.2");       //存放数据的服务器IP地址
    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('连接数据库失败: ' . mysql_error());
      }
      $flag = mysql_select_db($db_name,$conn);
      if(!$flag){
       echo "<p align='center'>数据库连接出错!</p>";exit();
      }else{
       mysql_query("SET NAMES UTF8");
      }
     }
     
    }
    ?>

    //正在运行的进程数 $tast_running = trim(trim($tast_info[1],'running')); //CPU占有量 $cpu_usage = trim(trim: '),'%us'); //百分比 //内存占有量 $mem_total = trim(trim,'k total'); $mem_used = trim($mem_info[1],'k used'); $mem_usage = round/intval; //百分比

    获取服务器性能CPU、内存、硬盘等使用率  get_used_status.php

    /*硬盘使用率 begin*/ $fp = popen('df -lh | grep -E "^; $rs = fread; pclose; $rs = preg_replace; //把多个空格换成 “_” $hd = explode; $hd_avail = trim; //磁盘可用空间大小 单位G $hd_usage = trim; //挂载点 百分比 //print_r; /*硬盘使用率 end*/ //检测时间 $fp = popen("date "%Y-%m-%d %H:%M"","r"); $rs = fread; pclose; $detection_time = trim; /*获取IP地址 begin*/ /* $fp = popen('ifconfig eth0 | grep -E "; $rs = fread; pclose; $rs = preg_replace("/s{2,}/",' ',trim; //把多个空格换成 “_” $rs = explode; $ip = trim; */ /*获取IP地址 end*/ /* $file_name = "/tmp/data.txt"; // 绝对路径: homedata.dat $file_pointer = fopen; // "w"是一种模式,详见后面 fwrite; // 先把文件剪切为0字节大小, 然后写入 fclose; // 结束 */ return array('cpu_usage'=>$cpu_usage,'mem_usage'=>$mem_usage,'hd_avail'=>$hd_avail,'hd_usage'=>$hd_usage,'tast_running'=>$tast_running,'detection_time'=>$detection_time);}

    <?php
     /*连接数据 begin*/
     include("conn.php");
     $obj_MyConnect = new MyConnect();
     $obj_MyConnect -> connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE); 
     /*连接数据 end*/
     
     function get_used_status(){
      $fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//获取某一时刻系统cpu和内存使用情况
      $rs = "";
      while(!feof($fp)){
       $rs .= fread($fp,1024);
      }
      pclose($fp);
      $sys_info = explode("n",$rs);

    本文由美高梅开户发布于美高梅开户,转载请注明出处:获取Linux服务器性能CPU

    关键词: 美高梅开户