国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

分享一段php獲取linux服務器狀態的代碼

2024-05-04 23:23:53
字體:
來源:轉載
供稿:網友

簡單的php獲取linux服務器狀態的代碼,不多說-直接上函數:

復制代碼 代碼如下:


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);

  $tast_info = explode(",",$sys_info[3]);//進程 數組
  $cpu_info = explode(",",$sys_info[4]);  //CPU占有量  數組
  $mem_info = explode(",",$sys_info[5]); //內存占有量 數組

  //正在運行的進程數
  $tast_running = trim(trim($tast_info[1],'running'));
  //CPU占有量
  $cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us');  //百分比

  //內存占有量
  $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);  //百分比
 

  /*硬盤使用率 begin*/
  $fp = popen('df -lh | grep -E "^(/)"',"r");
  $rs = fread($fp,1024);
  pclose($fp);
  $rs = preg_replace("http://s{2,}/",' ',$rs);  //把多個空格換成 “_”
  $hd = explode(" ",$rs);
  $hd_avail = trim($hd[3],'G'); //磁盤可用空間大小 單位G
  $hd_usage = trim($hd[4],'%'); //掛載點 百分比
  //print_r($hd);
  /*硬盤使用率 end*/ 

  //檢測時間
  $fp = popen("date +/"%Y-%m-%d %H:%M/"","r");
  $rs = fread($fp,1024);
  pclose($fp);
  $detection_time = trim($rs);

  /*獲取IP地址  begin*/
  /*
  $fp = popen('ifconfig eth0 | grep -E "(inet addr)"','r');
  $rs = fread($fp,1024);
  pclose($fp);
  $rs = preg_replace("http://s{2,}/",' ',trim($rs));  //把多個空格換成 “_”
  $rs = explode(" ",$rs);
  $ip = trim($rs[1],'addr:');
  */
  /*獲取IP地址 end*/
  /*
  $file_name = "/tmp/data.txt"; // 絕對路徑: homedata.dat
  $file_pointer = fopen($file_name, "a+"); // "w"是一種模式,詳見后面
  fwrite($file_pointer,$ip); // 先把文件剪切為0字節大小, 然后寫入
  fclose($file_pointer); // 結束
  */

  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);
 }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲麻莱县| 通城县| 晋宁县| 江阴市| 木里| 孝义市| 阳朔县| 临洮县| 庄河市| 阿拉善盟| 河西区| 长武县| 大关县| 新丰县| 浮梁县| 板桥市| 县级市| 高台县| 开远市| 铁力市| 西安市| 长沙县| 鄂温| 五寨县| 吴江市| 正阳县| 乌苏市| 汨罗市| 邵东县| 东阿县| 全州县| 铜山县| 临潭县| 昂仁县| 荥经县| 福建省| 廉江市| 西平县| 枣强县| 株洲市| 富川|