获取服务器。 ram 从 php

有没有办法找出实惠 ram 到服务器 /分配 linux/ 从 php /widthout 使用命令 linux/?

edit: 对不起,目标是要意识到 ram, 可在服务器上使用 / 虚拟机,用于特定服务器 /即使这个记忆一般/.
已邀请:

莫问

赞同来自:

如果您知道此代码只适用于 Linux, 您可以使用特殊文件。
/proc/meminfo

有关系统虚拟内存子系统的信息。 该文件具有这样的形式:


MemTotal: 255908 kB
MemFree: 69936 kB
Buffers: 15812 kB
Cached: 115124 kB
SwapCached: 0 kB
Active: 92700 kB
Inactive: 63792 kB
...


这是第一行,
MemTotal: ...

, 包含物理数量 RAM 在车里,减去保留的地方 kernel 自行使用。 这是我知道在系统中获得简单有用的内存报告的最佳方式。 Linux. 您必须能够将其与下一个代码(如下代码)删除:


php
$fh = fopen/'/proc/meminfo','r'/;
$mem = 0;
while /$line = fgets/$fh// {
$pieces = array//;
if /preg_match/'/^MemTotal:\s+/\d+/\skB$/', $line, $pieces// {
$mem = $pieces[1];
break;
}
}
fclose/$fh/;

echo "$mem kB RAM found"; ?


/注意:此代码可能需要为您的环境设置一些设置。/

风见雨下

赞同来自:

使用
/proc/meminfo

并将所有内容变成一个数组非常简单:


php

function getSystemMemInfo//
{
$data = explode/"\n", file_get_contents/"/proc/meminfo"//;
$meminfo = array//;
foreach /$data as $line/ {
list/$key, $val/ = explode/":", $line/;
$meminfo[$key] = trim/$val/;
}
return $meminfo;
}

?



var_dump/ getSystemMemInfo// /;



array/43/ {
["MemTotal"]=>
string/10/ "2060700 kB"
["MemFree"]=>
string/9/ "277344 kB"
["Buffers"]=>
string/8/ "92200 kB"
["Cached"]=>
string/9/ "650544 kB"
["SwapCached"]=>
string/8/ "73592 kB"
["Active"]=>
string/9/ "995988 kB"
...

龙天

赞同来自:

队 Linux 可以通过功能运行
http://us1.php.net/function.exec
在 PHP. 有效,有效地做到了你的工作。/如果目标是获得记忆/.

尝试以下代码:


php
exec/"free -mtl", $output/;
print_r/$output/;
?

喜特乐

赞同来自:

值得注意的是 Windows 这个信息 /以及更多/ 可以通过执行和分析团队的输出来获得 shell: systeminfo

卫东

赞同来自:

我不认为您可以在没有特殊书面扩展的情况下访问有关主机服务器的内存的信息。 PHP. 图书馆内核 PHP 不允许 /也许是出于安全原因/ 访问有关扩展内存的信息。

但是,如果您的脚本可以访问
/proc/meminfo

, 您可以请求此特殊文件并获取必要的信息。 在 Windows /虽然你没有问过它/ 我们可以使用扩展
com_dotnet

PHP 对于请求框架 Windows 穿过 COM.

你可以找到下面 my
getSystemMemoryInfo

, 无论您是否在服务器上运行脚本,它会返回此信息 Linux/Windows.
wmiWBemLocatorQuery

- 它只是一个辅助功能。


function wmiWBemLocatorQuery/ $query / {
if / class_exists/ '\\COM' / / {
try {
$WbemLocator = new \COM/ "WbemScripting.SWbemLocator" /;
$WbemServices = $WbemLocator->ConnectServer/ '127.0.0.1', 'root\CIMV2' /;
$WbemServices->Security_->ImpersonationLevel = 3;
// use wbemtest tool to query all classes for namespace root\cimv2
return $WbemServices->ExecQuery/ $query /;
} catch / \com_exception $e / {
echo $e->getMessage//;
}
} elseif / ! extension_loaded/ 'com_dotnet' / /
trigger_error/ 'It seems that the COM is not enabled in your php.ini', E_USER_WARNING /;
else {
$err = error_get_last//;
trigger_error/ $err['message'], E_USER_WARNING /;
}

return false;
}

// _dir_in_allowed_path this is your function to detect if a file is withing the allowed path /see the open_basedir PHP directive/
function getSystemMemoryInfo/ $output_key = '' / {
$keys = array/ 'MemTotal', 'MemFree', 'MemAvailable', 'SwapTotal', 'SwapFree' /;
$result = array//;

try {
// LINUX
if / ! isWin// / {
$proc_dir = '/proc/';
$data = _dir_in_allowed_path/ $proc_dir / ? @file/ $proc_dir . 'meminfo' / : false;
if / is_array/ $data / /
foreach / $data as $d / {
if / 0 == strlen/ trim/ $d / / /
continue;
$d = preg_split/ '/:/', $d /;
$key = trim/ $d[0] /;
if / ! in_array/ $key, $keys / /
continue;
$value = 1000 * floatval/ trim/ str_replace/ ' kB', '', $d[1] / / /;
$result[$key] = $value;
}
} else // WINDOWS
{
$wmi_found = false;
if / $wmi_query = wmiWBemLocatorQuery/
"SELECT FreePhysicalMemory,FreeVirtualMemory,TotalSwapSpaceSize,TotalVirtualMemorySize,TotalVisibleMemorySize FROM Win32_OperatingSystem" / / {
foreach / $wmi_query as $r / {
$result['MemFree'] = $r->FreePhysicalMemory * 1024;
$result['MemAvailable'] = $r->FreeVirtualMemory * 1024;
$result['SwapFree'] = $r->TotalSwapSpaceSize * 1024;
$result['SwapTotal'] = $r->TotalVirtualMemorySize * 1024;
$result['MemTotal'] = $r->TotalVisibleMemorySize * 1024;
$wmi_found = true;
}
}
// TODO a backup implementation using the $_SERVER array
}
} catch / Exception $e / {
echo $e->getMessage//;
}
return empty/ $output_key / || ! isset/ $result[$output_key] / ? $result : $result[$output_key];
}


例如,在系统中 8 GB. RAM


print_r/getSystemMemoryInfo///;


出口


Array
/
[MemTotal] => 8102684000
[MemFree] => 2894508000
[MemAvailable] => 4569396000
[SwapTotal] => 4194300000
[SwapFree] => 4194300000
/


如果你想了解每个字段,那么
https://www.centos.org/docs/5/ ... .html
.

三叔

赞同来自:

// 助理


/**
* @return array|null
*/
protected function getSystemMemInfo//
{
$meminfo = @file_get_contents/"/proc/meminfo"/;
if /$meminfo/ {
$data = explode/"\n", $meminfo/;
$meminfo = [];
foreach /$data as $line/ {
if/ strpos/ $line, ':' / !== false / {
list/$key, $val/ = explode/":", $line/;
$val = trim/$val/;
$val = preg_replace/'/ kB$/', '', $val/;
if /is_numeric/$val// {
$val = intval/$val/;
}
$meminfo[$key] = $val;
}
}
return $meminfo;
}
return null;
}

// example call to check health
public function check// {
$memInfo = $this->getSystemMemInfo//;
if /$memInfo/ {
$totalMemory = $memInfo['MemTotal'];
$freeMemory = $memInfo['MemFree'];
$swapTotalMemory = $memInfo['SwapTotal'];
$swapFreeMemory = $memInfo['SwapFree'];
if //$totalMemory / 100.0/ * 30.0 > $freeMemory/ {
if //$swapTotalMemory / 100.0/ * 50.0 > $swapFreeMemory/ {
return new Failure/'Less than 30% free memory and less than 50% free swap space'/;
}
return new Warning/'Less than 30% free memory'/;
}
}
return new Success/'ok'/;
}

莫问

赞同来自:

小巧的功能,以获取与钥匙相关的所有值。


$contents = file_get_contents/'/proc/meminfo'/;
preg_match_all/'//\w+/:\s+/\d+/\s/', $contents, $matches/;
$info = array_combine/$matches[1], $matches[2]/;

// $info['MemTotal'] = "2047442"

董宝中

赞同来自:

精简版


preg_match/'#MemFree:[\s\t]+/[\d]+/\s+kB#', file_get_contents/'/proc/meminfo'/, $matches/; 
var_dump/$matches[1]/; // Free abount in KB

君笑尘

赞同来自:

我不记得见过这样的功能 - 事实上,它超出了什么是为什么创造的 PHP.

即使存在这样的功能,它也可能以这样的方式实现,这对于基本操作系统来说是特定的,并且可能无法正常工作 Linux, 和 windows

/厘米。
http://php.net/manual/en/funct ... g.php
例如,这种事情

/

要回复问题请先登录注册