我是靠谱客的博主 辛勤灯泡,这篇文章主要介绍php 获得CPU使用率 获得内存使用率数组,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php header("Content-type: text/html; charset=utf-8"); 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:\\.\rootcimv2: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:\\.\rootcimv2") 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 获得CPU使用率 获得内存使用率数组的全部内容,更多相关php内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部