当前位置:网站首页>PHP gets CPU usage, hard disk usage, and memory usage
PHP gets CPU usage, hard disk usage, and memory usage
2022-07-02 05:49:00 【Jill__ er】
cpu Usage rate
- Code implementation :
<?php
function getCpu()
{
exec("top -n 1| grep id| awk {'print $8'}", $out);
var_dump($out);
$cpu_usage = 100 - $out[0]; // There are so many more available
$ret = array('cpu'=>$cpu_usage);
return $ret;
}
$cpu = getCpu();
var_dump($cpu); //CPU Current usage percentage
?>
- Results show :
array(1) {
[0]=>
string(4) "35.0"
}
array(1) {
["cpu"]=>
float(65)
}
Hard disk Usage rate
- Code implementation :
<?php
function getDisk()
{
$result = array();
exec("df", $result);
var_dump($result); // See print area
$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); // See print area
$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;// System hard disk space utilization System disk space utilization
}
$disk = getDisk();
var_dump($disk); // See print area
*/
?>
- Results show :
/*
array(10) {
[0]=>
string(70) " file system 1K- block Already used You can use Already used % Mount point "
[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)
}
Memory usage
- Code implementation :
<?php
function getMemory()
{
$str = shell_exec('more /proc/meminfo');
$pattern = "/(.+):\s*([0-9]+)/";
preg_match_all($pattern, $str, $out);
var_dump($out);
// Total memory - Free memory = Used memory
$mem_val = intval($out[2][0]-$out[2][1]);
// Used memory / Total memory = Memory usage
$mem_usage = intval($out[2][0]-$out[2][1])./.intval($out[2][0]);
$ret = array('mem'=>$mem_usage,'mem_usage'=>$mem_val);
return $ret; // System memory usage
}
$memory = getMemory();
var_dump($memory);
?>
- Results show :
/*
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)
}
*/
边栏推荐
- 【LeetCode】Day92-盛最多水的容器
- Uva548 tree
- Importation de studio visuel
- Alibaba: open source and self-developed liquid cooling data center technology
- 460. LFU cache bidirectional linked list
- The Hong Kong Stock Exchange learned from US stocks and pushed spac: the follow-up of many PE companies could not hide the embarrassment of the world's worst stock market
- all3dp. All Arduino projects in com website (2022.7.1)
- Balsamiq wireframes free installation
- Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
- LCD之MIPI协议的一些说明
猜你喜欢
随机推荐
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond
TypeScript的泛型和泛型约束
Stick to the big screen UI, finereport development diary
正则表达式总结
Zzuli:1062 greatest common divisor
Fabric. JS compact JSON
《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
ThreadLocal memory leak
1035 Password
Software testing Q & A
Usage record of vector
c语言中的几个关键字
Visual Studio導入
Technologists talk about open source: This is not just using love to generate electricity
文件包含漏洞(一)
Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
Fabric. JS iText superscript and subscript
“简单”的无限魔方
Sliding window on the learning road
Pytorch Chinese document