如何在 PHP 中获取系统信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4577433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get system info in PHP?
提问by Phil
I want to get the system memory usage (and also HDD space info) in PHP. Is there any way to do without invoking commands using system
calls?
我想在 PHP 中获取系统内存使用情况(以及硬盘空间信息)。有没有办法不用调用来调用命令system
?
Note: I am not looking for the script memory usage, but the system memory usage.
注意:我不是在寻找脚本内存使用情况,而是系统内存使用情况。
采纳答案by nimrodm
On Linux, you can read /proc/meminfo
to get information on total and available system memory (just cat /proc/meminfo
-- it's a simple text file you can easily parse).
在 Linux 上,您可以阅读/proc/meminfo
以获取有关总内存和可用系统内存的信息(只是cat /proc/meminfo
——这是一个可以轻松解析的简单文本文件)。
I'm not sure you can get filesystem information from procfs, by try looking at /proc/sys/fs
for interesting information.
我不确定您是否可以通过尝试查找/proc/sys/fs
有趣的信息来从 procfs 获取文件系统信息。
回答by Sarfraz
You are looking for phpSysInfo:
您正在寻找phpSysInfo:
phpSysInfo is a PHP script that displays information about the host being accessed. It will displays things like :
phpSysInfo 是一个 PHP 脚本,用于显示有关正在访问的主机的信息。它将显示如下内容:
- Uptime
- CPU
- Memory
- SCSI, IDE, PCI
- Ethernet
- Floppy
- Video Information
- 正常运行时间
- 中央处理器
- 记忆
- SCSI、IDE、PCI
- 以太网
- 软盘
- 视频信息
回答by Phil
This is putting out everything about cpu, ram, hdd and network in JSON format. (good for handling it with jQuery)
这是以 JSON 格式输出有关 cpu、ram、hdd 和网络的所有内容。(适合用 jQuery 处理它)
<?php
//cpu stat
$prevVal = shell_exec("cat /proc/stat");
$prevArr = explode(' ',trim($prevVal));
$prevTotal = $prevArr[2] + $prevArr[3] + $prevArr[4] + $prevArr[5];
$prevIdle = $prevArr[5];
usleep(0.15 * 1000000);
$val = shell_exec("cat /proc/stat");
$arr = explode(' ', trim($val));
$total = $arr[2] + $arr[3] + $arr[4] + $arr[5];
$idle = $arr[5];
$intervalTotal = intval($total - $prevTotal);
$stat['cpu'] = intval(100 * (($intervalTotal - ($idle - $prevIdle)) / $intervalTotal));
$cpu_result = shell_exec("cat /proc/cpuinfo | grep model\ name");
$stat['cpu_model'] = strstr($cpu_result, "\n", true);
$stat['cpu_model'] = str_replace("model name : ", "", $stat['cpu_model']);
//memory stat
$stat['mem_percent'] = round(shell_exec("free | grep Mem | awk '{print / * 100.0}'"), 2);
$mem_result = shell_exec("cat /proc/meminfo | grep MemTotal");
$stat['mem_total'] = round(preg_replace("#[^0-9]+(?:\.[0-9]*)?#", "", $mem_result) / 1024 / 1024, 3);
$mem_result = shell_exec("cat /proc/meminfo | grep MemFree");
$stat['mem_free'] = round(preg_replace("#[^0-9]+(?:\.[0-9]*)?#", "", $mem_result) / 1024 / 1024, 3);
$stat['mem_used'] = $stat['mem_total'] - $stat['mem_free'];
//hdd stat
$stat['hdd_free'] = round(disk_free_space("/") / 1024 / 1024 / 1024, 2);
$stat['hdd_total'] = round(disk_total_space("/") / 1024 / 1024/ 1024, 2);
$stat['hdd_used'] = $stat['hdd_total'] - $stat['hdd_free'];
$stat['hdd_percent'] = round(sprintf('%.2f',($stat['hdd_used'] / $stat['hdd_total']) * 100), 2);
//network stat
$stat['network_rx'] = round(trim(file_get_contents("/sys/class/net/eth0/statistics/rx_bytes")) / 1024/ 1024/ 1024, 2);
$stat['network_tx'] = round(trim(file_get_contents("/sys/class/net/eth0/statistics/tx_bytes")) / 1024/ 1024/ 1024, 2);
//output headers
header('Content-type: text/json');
header('Content-type: application/json');
//output data by json
echo
"{\"cpu\": " . $stat['cpu'] . ", \"cpu_model\": \"" . $stat['cpu_model'] . "\"" . //cpu stats
", \"mem_percent\": " . $stat['mem_percent'] . ", \"mem_total\":" . $stat['mem_total'] . ", \"mem_used\":" . $stat['mem_used'] . ", \"mem_free\":" . $stat['mem_free'] . //mem stats
", \"hdd_free\":" . $stat['hdd_free'] . ", \"hdd_total\":" . $stat['hdd_total'] . ", \"hdd_used\":" . $stat['hdd_used'] . ", \"hdd_percent\":" . $stat['hdd_percent'] . ", " . //hdd stats
"\"network_rx\":" . $stat['network_rx'] . ", \"network_tx\":" . $stat['network_tx'] . //network stats
"}";
?>
回答by makra
Linfoprovides most important system status information. You just need a system where /proc
and /sys
are mounted (most unix-like system, I guess).
Linfo提供最重要的系统状态信息。你只需要一个系统,/proc
并/sys
安装(大多数的类Unix系统,我猜)。
From the website: Information Shown
来自网站:显示的信息
- CPU Type
- RAM Usage
- PCI/USB devices
- Hard drives
- File system mounts
- Network devices
- Temps/Voltages via hddtemp/mbmon
- Software raid arrays (either by mdadm or gmirror)
- System load / number of processes, threads
- Linux distribution, if possible
- CPU类型
- 内存使用
- PCI/USB 设备
- 硬盘驱动器
- 文件系统挂载
- 网络设备
- 通过 hddtemp/mbmon 的温度/电压
- 软件 raid 阵列(通过 mdadm 或 gmirror)
- 系统负载/进程数、线程数
- 如果可能,Linux 发行版