服务器资源使用情况

class SystemInfoWindows
{
    /**
     * 判断指定路径下指定文件是否存在,如不存在则创建
     * @param string $fileName 文件名
     * @param string $content 文件内容
     * @return string 返回文件路径
     */
    private function getFilePath($fileName, $content)
    {
        $path = dirname(__FILE__,3).'/runtime/sys/' . $fileName;
        if (!file_exists($path)) {
            file_put_contents($path, $content);
        }
        return $path;
    }

    /**
     * 获得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)"
        );
    }

    /**
     * 获得总内存及可用物理内存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"
        );
    }

    /**
     * 获得window系统CPU使用率
     * @return Number
     */
    private function get_W_CpuUsage()
    {
        $path = $this->getCupUsageVbsPath();
        exec("cscript -nologo $path", $usage);
        return $usage[0];
    }

    /**
     * Notes:获取linux系统cpu使用率
     */
    private function get_L_CpuUsage () {
        $str = shell_exec('more /proc/stat');
        $pattern = "/(cpu[0-9]?)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)/";
        preg_match_all($pattern, $str, $out);

        $cpu_data = explode(' ',$out[0][0]);
        $cpu_array = array();
        $cpu_array['user'] = $cpu_data[2];
        $cpu_array['nice'] = $cpu_data[3];
        $cpu_array['system'] = $cpu_data[4];
        $cpu_array['idle'] = $cpu_data[5];
        $cpu_array['iowait'] = $cpu_data[6];
        $cpu_array['irrq'] = $cpu_data[7];
        $cpu_array['softirq'] = $cpu_data[8];

        $usage = 100 * ($cpu_array['user'] + $cpu_array['nice'] + $cpu_array['system']) / ($cpu_array['user'] + $cpu_array['nice'] + $cpu_array['system'] + $cpu_array['idle']);

        return number_format($usage,1);
    }

    /**
     * 获取window内存使用率
     * @return array
     */
    private function get_W_MemoryUsage () {
        $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['usage'];
    }

    /**
     * Notes: 获取linux内存使用率
     */
    private function get_L_MemoryUsage () {
        //内存使用率
        $str = shell_exec('more /proc/meminfo');
        $pattern = "/(.+):\s*([0-9]+)/";
        preg_match_all($pattern, $str, $out);
        $cmem = $out[2][1]; //当前使用量 $amem
        $amem = $out[2][0]; //内存总量
        $sage = number_format(100 * ($cmem / $amem),1);
        $sage_fei = number_format(100 - $sage,1);
        return $sage_fei;
    }

    /**
     * Notes: 获取CPU使用率
     * @return Number|string
     */
    public function getCpuUe () {
        $is_win = strtoupper(substr(PHP_OS,0,3))==='WIN' ? true : false;

        if ($is_win === false) {
            return $this->get_L_CpuUsage();
        }
        return $this->get_W_CpuUsage();
    }

    /**
     * Notes: 获取内存使用率
     * @return array|string
     */
    public function getCpuMe () {
        $is_win = strtoupper(substr(PHP_OS,0,3))==='WIN' ? true : false;
        if ($is_win === false) {
            return $this->get_L_MemoryUsage();
        }
        return $this->get_W_MemoryUsage();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炒鸡时光机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值