1. 程式人生 > >php獲取Linux和windos伺服器系統執行資訊指令碼(硬碟使用率和cpu記憶體資訊)

php獲取Linux和windos伺服器系統執行資訊指令碼(硬碟使用率和cpu記憶體資訊)

將網上的Linux和Windows獲取系統資訊的方法進行整合,統計當前目錄下的硬碟使用率和cpu記憶體資訊。

<?php
/**
 * 伺服器系統執行資訊
 */
$path=__DIR__;//儲存位置"

header('content-Type: text/html; charset=utf-8');
date_default_timezone_set('PRC'); //設定中國時區

$info = new SystemInfo();
$info->path=$path;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    $status=$info->getWinInfo();
}else{
    $status=$info->getLinuxInfo();
}
echo json_encode($status);


/**
 * 系統資訊獲取
 * Class SystemInfo
 */
class SystemInfo
{
    public $path=__DIR__;
    private $info=array(
        'cpu_usage'=>'',//cpu使用量 %
        'mem_usage'=>'',//記憶體使用量 %
        'hd_total'=>'',//磁碟總空間大小 單位m
        'hd_free'=>'',//磁碟可用空間大小 單位m
    );

    /**
     * windows判斷指定路徑下指定檔案是否存在,如不存在則建立
     * @param string $fileName 檔名
     * @param string $content 檔案內容
     * @return string 返回檔案路徑
     */
    private function getFilePath($fileName, $content)
    {
        $path = dirname(__FILE__) . "\\$fileName";
        if (!file_exists($path)) {
            file_put_contents($path, $content);
        }
        return $path;
    }
    /**
     * windows獲得cpu使用率vbs檔案生成函式
     * @return string 返回vbs檔案路徑
     */
    private function getCupUsageVbsPath()
    {
        return $this->getFilePath(
            'cpu_usage.vbs',
            "On Error Resume Next
    Set objProc = GetObject(\"winmgmts:\\\\.\\root\cimv2:win32_processor='cpu0'\")
    WScript.Echo(objProc.LoadPercentage)"
        );
    }
    /**
     * windows獲得總記憶體及可用實體記憶體JSON vbs檔案生成函式
     * @return string 返回vbs檔案路徑
     */
    private function getMemoryUsageVbsPath()
    {
        return $this->getFilePath(
            'memory_usage.vbs',
            "On Error Resume Next
    Set objWMI = GetObject(\"winmgmts:\\\\.\\root\cimv2\")
    Set colOS = objWMI.InstancesOf(\"Win32_OperatingSystem\")
    For Each objOS in colOS
     Wscript.Echo(\"{\"\"TotalVisibleMemorySize\"\":\" & objOS.TotalVisibleMemorySize & \",\"\"FreePhysicalMemory\"\":\" & objOS.FreePhysicalMemory & \"}\")
    Next"
        );
    }
    /**
     * windows獲得CPU使用率
     * @return Number
     */
    private function getCpuUsage()
    {
        $path = $this->getCupUsageVbsPath();
        exec("cscript -nologo $path", $usage);
        return $usage[0];
    }
    /**
     * windows獲得記憶體使用率陣列
     * @return array
     */
    private function getMemoryUsage()
    {
        $path = $this->getMemoryUsageVbsPath();
        exec("cscript -nologo $path", $usage);
        $memory = json_decode($usage[0], true);
        $memory['usage'] = Round((($memory['TotalVisibleMemorySize'] - $memory['FreePhysicalMemory']) / $memory['TotalVisibleMemorySize']) * 100);
        return $memory;
    }

    private function getDiskinfo(){
        //硬碟使用率
        $free = disk_free_space($this->path) ;
        $total = disk_total_space($this->path);
        $free = ceil($free/1024/1024);
        $total = ceil($total/1024/1024);

        return array('hd_total'=>$total,'hd_free'=>$free);
    }

    /**
     * windows資訊統計
     * @return array
     */
    public function getWinInfo(){
        $cpu = $this->getCpuUsage();
        $memory = $this->getMemoryUsage();
        $hd = $this->getDiskinfo();
        $this->info['cpu_usage']=$cpu;
        $this->info['mem_usage']=$memory['usage'];
        $this->info['hd_total']= $hd['hd_total'];
        $this->info['hd_free']= $hd['hd_free'];
        return $this->info;
    }

    /**
     * linux系統資訊統計
     * @return array
     */
    public function getLinuxInfo(){
        $fp = popen('top -b -n 1 | grep -E "^(Cpu|Mem)"',"r");//獲取某一時刻系統cpu和記憶體使用情況
        $rs = "";
        while(!feof($fp)){
            $rs .= fread($fp,1024);
        }
        pclose($fp);
        $sys_info = explode("\n",$rs);

        $cpu_info = explode(",",$sys_info[0]); //CPU佔有量 陣列
        $mem_info = explode(",",$sys_info[1]); //記憶體佔有量 陣列

        //CPU佔有量
        $cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //百分比
        $this->info['cpu_usage']=$cpu_usage;

        //記憶體佔有量
        $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); //百分比
        $this->info['mem_usage']=$mem_usage;

        //硬碟使用率
        $hd = $this->getDiskinfo();
        $this->info['hd_total']= $hd['hd_total'];
        $this->info['hd_free']= $hd['hd_free'];

        return $this->info;
    }
}