当前位置:网站首页>php获取cpu使用率、硬盘使用、内存使用
php获取cpu使用率、硬盘使用、内存使用
2022-07-02 05:47:00 【杰儿__er】
cpu 使用率
- 代码实现:
<?php
function getCpu()
{
exec("top -n 1| grep id| awk {'print $8'}", $out);
var_dump($out);
$cpu_usage = 100 - $out[0]; //还有这么多可用
$ret = array('cpu'=>$cpu_usage);
return $ret;
}
$cpu = getCpu();
var_dump($cpu); //CPU 当前使用率百分比
?>
- 结果显示:
array(1) {
[0]=>
string(4) "35.0"
}
array(1) {
["cpu"]=>
float(65)
}
硬盘 使用率
- 代码实现:
<?php
function getDisk()
{
$result = array();
exec("df", $result);
var_dump($result); //见打印区域
$s_total = 0;
$s_used = 0;
foreach($result as $value){
if(strripos($value,"/mnt")!==false && strripos($value,"sd")!==false){
preg_match("/([\/\w+]*)(\s*)(\d+\.?\d*\w+)(\s*)(\d+\.?\d*\w+)(\s*)(\d+\.?\d*\w+)(\s*)(\d+\.*\d*%)(\s*)(\/mnt)/i",$value,$match);
var_dump($match); //见打印区域
$s_total += intval($match[3]);
$s_used += intval($match[5]);
}
}
$disk_val = intval($s_used/1024);
$disk_usage = intval($s_used/1024).'/'.intval($s_total/1024);
$ret = array('disk'=>$disk_usage,'disk_usage'=>$disk_val);
return $ret;//系统硬盘空间使用率 系统盘空间使用率
}
$disk = getDisk();
var_dump($disk); //见打印区域
*/
?>
- 结果显示:
/*
array(10) {
[0]=>
string(70) "文件系统 1K-块 已用 可用 已用% 挂载点"
[1]=>
string(51) "/dev/root 14899176 7580252 6542376 54% /"
[2]=>
string(54) "devtmpfs 8293440 0 8293440 0% /dev"
[3]=>
string(58) "tmpfs 8296512 0 8296512 0% /dev/shm"
[4]=>
string(54) "tmpfs 8296512 391232 7905280 5% /run"
[5]=>
string(59) "tmpfs 5120 0 5120 0% /run/lock"
[6]=>
string(64) "tmpfs 8296512 0 8296512 0% /sys/fs/cgroup"
[7]=>
string(55) "/dev/sda1 523248 163956 359292 32% /boot"
[8]=>
string(54) "/dev/sda5 197607880 15855736 171644548 9% /mnt"
[9]=>
string(54) "tmpfs 2097152 102528 1994624 5% /tmp"
}
array(12) {
[0]=>
string(54) "/dev/sda5 197607880 15855736 171644548 9% /mnt"
[1]=>
string(9) "/dev/sda5"
[2]=>
string(6) " "
[3]=>
string(9) "197607880"
[4]=>
string(1) " "
[5]=>
string(8) "15855736"
[6]=>
string(1) " "
[7]=>
string(9) "171644548"
[8]=>
string(4) " "
[9]=>
string(2) "9%"
[10]=>
string(1) " "
[11]=>
string(4) "/mnt"
}
array(2) {
["disk"]=>
string(12) "15484/192976"
["disk_usage"]=>
int(15484)
}
内存使用率
- 代码实现:
<?php
function getMemory()
{
$str = shell_exec('more /proc/meminfo');
$pattern = "/(.+):\s*([0-9]+)/";
preg_match_all($pattern, $str, $out);
var_dump($out);
//总内存-空闲内存=已使用内存
$mem_val = intval($out[2][0]-$out[2][1]);
//已使用内存 / 总的内存 = 内存使用率
$mem_usage = intval($out[2][0]-$out[2][1])./.intval($out[2][0]);
$ret = array('mem'=>$mem_usage,'mem_usage'=>$mem_val);
return $ret; //系统内存使用率
}
$memory = getMemory();
var_dump($memory);
?>
- 结果显示:
/*
array(3) {
[0]=>
array(45) {
[0]=>
string(24) "MemTotal: 16593088"
[1]=>
string(24) "MemFree: 2904768"
[2]=>
string(24) "MemAvailable: 3336832"
[3]=>
string(24) "Buffers: 699520"
[4]=>
string(24) "Cached: 1220096"
[5]=>
string(24) "SwapCached: 0"
[6]=>
string(24) "Active: 3834752"
[7]=>
string(24) "Inactive: 501824"
[8]=>
string(24) "Active(anon): 2939584"
[9]=>
string(24) "Inactive(anon): 433856"
[10]=>
string(24) "Active(file): 895168"
[11]=>
string(24) "Inactive(file): 67968"
[12]=>
string(24) "Unevictable: 0"
[13]=>
string(24) "Mlocked: 0"
[14]=>
string(24) "SwapTotal: 0"
[15]=>
string(24) "SwapFree: 0"
[16]=>
string(24) "Dirty: 1024"
[17]=>
string(24) "Writeback: 0"
[18]=>
string(24) "AnonPages: 2418240"
[19]=>
string(24) "Mapped: 1238848"
[20]=>
string(24) "Shmem: 956608"
[21]=>
string(24) "Slab: 503680"
[22]=>
string(24) "SReclaimable: 144896"
[23]=>
string(24) "SUnreclaim: 358784"
[24]=>
string(24) "KernelStack: 5280"
[25]=>
string(24) "PageTables: 18368"
[26]=>
string(24) "NFS_Unstable: 0"
[27]=>
string(24) "Bounce: 0"
[28]=>
string(24) "WritebackTmp: 0"
[29]=>
string(24) "CommitLimit: 8296512"
[30]=>
string(24) "Committed_AS: 6852224"
[31]=>
string(28) "VmallocTotal: 133009637312"
[32]=>
string(24) "VmallocUsed: 0"
[33]=>
string(24) "VmallocChunk: 0"
[34]=>
string(24) "Percpu: 4608"
[35]=>
string(24) "HardwareCorrupted: 0"
[36]=>
string(24) "AnonHugePages: 0"
[37]=>
string(24) "ShmemHugePages: 0"
[38]=>
string(24) "ShmemPmdMapped: 0"
[39]=>
string(24) "HugePages_Total: 0"
[40]=>
string(24) "HugePages_Free: 0"
[41]=>
string(24) "HugePages_Rsvd: 0"
[42]=>
string(24) "HugePages_Surp: 0"
[43]=>
string(24) "Hugepagesize: 524288"
[44]=>
string(24) "Hugetlb: 0"
}
[1]=>
array(45) {
[0]=>
string(8) "MemTotal"
[1]=>
string(7) "MemFree"
[2]=>
string(12) "MemAvailable"
[3]=>
string(7) "Buffers"
[4]=>
string(6) "Cached"
[5]=>
string(10) "SwapCached"
[6]=>
string(6) "Active"
[7]=>
string(8) "Inactive"
[8]=>
string(12) "Active(anon)"
[9]=>
string(14) "Inactive(anon)"
[10]=>
string(12) "Active(file)"
[11]=>
string(14) "Inactive(file)"
[12]=>
string(11) "Unevictable"
[13]=>
string(7) "Mlocked"
[14]=>
string(9) "SwapTotal"
[15]=>
string(8) "SwapFree"
[16]=>
string(5) "Dirty"
[17]=>
string(9) "Writeback"
[18]=>
string(9) "AnonPages"
[19]=>
string(6) "Mapped"
[20]=>
string(5) "Shmem"
[21]=>
string(4) "Slab"
[22]=>
string(12) "SReclaimable"
[23]=>
string(10) "SUnreclaim"
[24]=>
string(11) "KernelStack"
[25]=>
string(10) "PageTables"
[26]=>
string(12) "NFS_Unstable"
[27]=>
string(6) "Bounce"
[28]=>
string(12) "WritebackTmp"
[29]=>
string(11) "CommitLimit"
[30]=>
string(12) "Committed_AS"
[31]=>
string(12) "VmallocTotal"
[32]=>
string(11) "VmallocUsed"
[33]=>
string(12) "VmallocChunk"
[34]=>
string(6) "Percpu"
[35]=>
string(17) "HardwareCorrupted"
[36]=>
string(13) "AnonHugePages"
[37]=>
string(14) "ShmemHugePages"
[38]=>
string(14) "ShmemPmdMapped"
[39]=>
string(15) "HugePages_Total"
[40]=>
string(14) "HugePages_Free"
[41]=>
string(14) "HugePages_Rsvd"
[42]=>
string(14) "HugePages_Surp"
[43]=>
string(12) "Hugepagesize"
[44]=>
string(7) "Hugetlb"
}
[2]=>
array(45) {
[0]=>
string(8) "16593088"
[1]=>
string(7) "2904768"
[2]=>
string(7) "3336832"
[3]=>
string(6) "699520"
[4]=>
string(7) "1220096"
[5]=>
string(1) "0"
[6]=>
string(7) "3834752"
[7]=>
string(6) "501824"
[8]=>
string(7) "2939584"
[9]=>
string(6) "433856"
[10]=>
string(6) "895168"
[11]=>
string(5) "67968"
[12]=>
string(1) "0"
[13]=>
string(1) "0"
[14]=>
string(1) "0"
[15]=>
string(1) "0"
[16]=>
string(4) "1024"
[17]=>
string(1) "0"
[18]=>
string(7) "2418240"
[19]=>
string(7) "1238848"
[20]=>
string(6) "956608"
[21]=>
string(6) "503680"
[22]=>
string(6) "144896"
[23]=>
string(6) "358784"
[24]=>
string(4) "5280"
[25]=>
string(5) "18368"
[26]=>
string(1) "0"
[27]=>
string(1) "0"
[28]=>
string(1) "0"
[29]=>
string(7) "8296512"
[30]=>
string(7) "6852224"
[31]=>
string(12) "133009637312"
[32]=>
string(1) "0"
[33]=>
string(1) "0"
[34]=>
string(4) "4608"
[35]=>
string(1) "0"
[36]=>
string(1) "0"
[37]=>
string(1) "0"
[38]=>
string(1) "0"
[39]=>
string(1) "0"
[40]=>
string(1) "0"
[41]=>
string(1) "0"
[42]=>
string(1) "0"
[43]=>
string(6) "524288"
[44]=>
string(1) "0"
}
}
array(2) {
["mem"]=>
string(17) "13688320/16593088"
["mem_usage"]=>
int(13688320)
}
*/
边栏推荐
- File contains vulnerabilities (II)
- Practice C language advanced address book design
- Zzuli:1068 binary number
- RNN recurrent neural network
- Fabric. JS free draw rectangle
- Pytorch Basics
- Here comes a new chapter in the series of data conversion when exporting with easyexcel!
- Zzuli:1069 learn from classmate Z
- 【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
- Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
猜你喜欢

Fabric. JS right click menu

File contains vulnerabilities (II)
![Gee series: unit 8 time series analysis in Google Earth engine [time series]](/img/a6/648ff959af93c22dc8605215a90535.jpg)
Gee series: unit 8 time series analysis in Google Earth engine [time series]

all3dp.com网站中全部Arduino项目(2022.7.1)

Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders

Fabric. JS iText superscript and subscript

Visual Studio導入

centos8安裝mysql8.0.22教程

7. Eleven state sets of TCP

Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
随机推荐
7. Eleven state sets of TCP
Pytorch Chinese document
A collection of commonly used plug-ins for idea development tools
brew install * 失败,解决方法
《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
Zzuli:1060 numbers in reverse order
Fabric. JS background is not affected by viewport transformation
Sliding window on the learning road
Installation du tutoriel MySQL 8.0.22 par centos8
[golang syntax] be careful with the copy of slices
Generate QR code
Opencv LBP features
vite如何兼容低版本浏览器
3D printer G code command: complete list and tutorial
Determine whether there is an element in the string type
软件测试答疑篇
Simply encapsulate JS and apply it
Gee series: Unit 4 data import and export in Google Earth engine
Disable access to external entities in XML parsing
Zzuli:1065 count the number of numeric characters