php在windows环境下获得cpu内存实时使用率(推荐)
作者:asseek 时间:2023-11-15 04:44:23
后台实时监控服务器的CUP与内存占用率的场景很常见,虽然没做过,但是着手写代码之前我真没想到会花2个多小时才最终实现。
网上虽然搜 PHP CUP 内存 使用率 这一类关键字出来的文章一大把,但大都是在Linux环境下,windows下只有提出靠cmd语句实现的观点,但少有实际直接可行的代码,辗转试了一遍 wmic , systeminfo 各种并不熟稔的cmd命令,并没有找到可行的方案,最终通过百度知道 的一篇答案找到利用vbs获取的实现方式,稍加改善,可以在windows环境下直接使用。
类代码
class SystemInfoWindows
{
/**
* 判断指定路径下指定文件是否存在,如不存在则创建
* @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;
}
/**
* 获得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"
);
}
/**
* 获得CPU使用率
* @return Number
*/
public function getCpuUsage()
{
$path = $this->getCupUsageVbsPath();
exec("cscript -nologo $path", $usage);
return $usage[0];
}
/**
* 获得内存使用率数组
* @return array
*/
public 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;
}
}
调用方式
$info = new SystemInfoWindows();
$cpu = $info->getCpuUsage();
$memory = $info->getMemoryUsage();
echo "当前系统CPU使用率:{$cpu}%,内存使用率{$memory['usage']}%";
总结
以上所述是小编给大家介绍的php在windows环境下获得cpu内存实时使用率,希望对大家有所帮助.
来源:https://www.jianshu.com/p/1629a9cd55ae
标签:php,cpu,内存
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一个css垂直水平居中布局,css效果
2008-11-03 11:40:00
详解MySQL监控工具 mysql-monitor
2024-01-25 23:03:16
![](https://img.aspxhome.com/file/2023/3/116783_0s.jpg)
一次MySQL慢查询导致的故障
2024-01-21 12:18:17
![](https://img.aspxhome.com/file/2023/5/78095_0s.png)
Python supervisor强大的进程管理工具的使用
2021-04-15 09:11:58
![](https://img.aspxhome.com/file/2023/0/75150_0s.png)
python网络编程学习笔记(10):webpy框架
2022-07-10 05:43:14
![](https://img.aspxhome.com/file/2023/4/121414_0s.jpg)
利用Tensorflow的队列多线程读取数据方式
2021-12-24 17:32:30
![](https://img.aspxhome.com/file/2023/3/110243_0s.jpg)
ThinkPHP5集成JS-SDK实现微信自定义分享功能
2023-06-10 13:59:47
Python常用模块介绍
2021-01-03 10:11:53
jquery和css3中的选择器nth-child使用方法和用途示例
2024-04-25 13:11:35
oracle 彻底删除方法
2024-01-19 10:20:57
前端html+css实现动态生日快乐代码
2024-05-10 10:58:12
![](https://img.aspxhome.com/file/2023/9/127699_0s.png)
php 静态页面中显示动态内容
2023-11-18 22:09:22
python迭代器常见用法实例分析
2023-07-12 02:40:54
Python multiprocess pool模块报错pickling error问题解决方法分析
2021-02-21 04:44:50
SQL Server 中导入导出数据三方法比较
2009-01-21 14:22:00
SQLserver查询数据类型为ntext是空或NULL值的方法
2024-01-24 18:40:52
Python使用tablib生成excel文件的简单实现方法
2021-02-24 09:09:00
![](https://img.aspxhome.com/file/2023/2/112032_0s.png)
MySQL获取当前时间的多种方式总结
2024-01-21 02:29:34
![](https://img.aspxhome.com/file/2023/5/79035_0s.png)
JS模拟实现京东快递单号查询
2024-04-18 09:45:44
用python自动生成日历
2022-08-28 14:28:58
![](https://img.aspxhome.com/file/2023/4/72174_0s.png)