我需要在剥离的Linux设备中获得真正的可用内存。我希望得到真正的自由记忆 获取可用内存不考虑缓冲区/缓存,所以我似乎没有ram,而我还有很多剩余的应用程序。
我正在使用snmp从Perl脚本中获取此值,并且我可以使用这些oid:
> HOST-RESOURCES-MIB::hrStorageDescr.1 = STRING: Physical memory
> HOST-RESOURCES-MIB::hrStorageDescr.3 = STRING: Virtual memory
> HOST-RESOURCES-MIB::hrStorageDescr.6 = STRING: Memory buffers
> HOST-RESOURCES-MIB::hrStorageDescr.7 = STRING: Cached memory
> HOST-RESOURCES-MIB::hrStorageDescr.10 = STRING: Swap space
> HOST-RESOURCES-MIB::hrStorageDescr.31 = STRING: /
> HOST-RESOURCES-MIB::hrStorageDescr.32 = STRING: /boot
> HOST-RESOURCES-MIB::hrStorageDescr.33 = STRING: /var/log
> HOST-RESOURCES-MIB::hrStorageAllocationUnits.1 = INTEGER: 1024 Bytes
> HOST-RESOURCES-MIB::hrStorageAllocationUnits.3 = INTEGER: 1024 Bytes
> HOST-RESOURCES-MIB::hrStorageAllocationUnits.6 = INTEGER: 1024 Bytes
> HOST-RESOURCES-MIB::hrStorageAllocationUnits.7 = INTEGER: 1024 Bytes
> HOST-RESOURCES-MIB::hrStorageAllocationUnits.10 = INTEGER: 1024 Bytes
> HOST-RESOURCES-MIB::hrStorageAllocationUnits.31 = INTEGER: 4096 Bytes
> HOST-RESOURCES-MIB::hrStorageAllocationUnits.32 = INTEGER: 1024 Bytes
> HOST-RESOURCES-MIB::hrStorageAllocationUnits.33 = INTEGER: 4096 Bytes
> HOST-RESOURCES-MIB::hrStorageSize.1 = INTEGER: 8240104
> HOST-RESOURCES-MIB::hrStorageSize.3 = INTEGER: 16626024
> HOST-RESOURCES-MIB::hrStorageSize.6 = INTEGER: 8240104
> HOST-RESOURCES-MIB::hrStorageSize.7 = INTEGER: 4697308
> HOST-RESOURCES-MIB::hrStorageSize.10 = INTEGER: 8385920
> HOST-RESOURCES-MIB::hrStorageSize.31 = INTEGER: 4062954
> HOST-RESOURCES-MIB::hrStorageSize.32 = INTEGER: 295561
> HOST-RESOURCES-MIB::hrStorageSize.33 = INTEGER: 33011530
> HOST-RESOURCES-MIB::hrStorageUsed.1 = INTEGER: 5610512
> HOST-RESOURCES-MIB::hrStorageUsed.3 = INTEGER: 5610512
> HOST-RESOURCES-MIB::hrStorageUsed.6 = INTEGER: 326360
> HOST-RESOURCES-MIB::hrStorageUsed.7 = INTEGER: 4697308
> HOST-RESOURCES-MIB::hrStorageUsed.10 = INTEGER: 0
> HOST-RESOURCES-MIB::hrStorageUsed.31 = INTEGER: 1673253
> HOST-RESOURCES-MIB::hrStorageUsed.32 = INTEGER: 24061
> HOST-RESOURCES-MIB::hrStorageUsed.33 = INTEGER: 19467049
我无法获得Linux中确切的RAM使用量我必须添加存储在缓存和缓冲区中的物理空闲内存吗? 在Perl中执行此操作的最佳方法是什么? hrStorage是最好的还是最好使用de UCD-SNMP_MIB(.1.3.6.1.4.1.2021.4)?
编辑:我创建了以下基于注释的scriptlet来计算实际使用的内存百分比。你认为我能做的最好还是我应该做的其他事情?
my $memRealTotalOID = '.1.3.6.1.4.1.2021.4.5.0';
my $memRealFreeOID = '.1.3.6.1.4.1.2021.4.6.0';
my $memRealCachedOID = '.1.3.6.1.4.1.2021.4.15.0';
my $memRealBuffersOID = '.1.3.6.1.4.1.2021.4.14.0';
my ($session, $error) = Net::SNMP->session(
-hostname => $np->opts->host,
-community => $np->opts->community,
);
if (!defined $session) {
$np->nagios_exit (WARNING, $error)
#printf "ERROR: %s.\n", $error;
}
my $memRealFree = $session->get_request(-varbindlist => [ $memRealFreeOID],);
my $memRealTotal = $session->get_request(-varbindlist => [ $memRealTotalOID],);
my $memRealCached = $session->get_request(-varbindlist => [ $memRealCachedOID],);
my $memRealBuffers = $session->get_request(-varbindlist => [ $memRealBuffersOID],);
$session->close();
my $buffers = $memRealBuffers->{$memRealBuffersOID};
my $cache = $memRealCached->{$memRealCachedOID};
my $total= $memRealTotal->{$memRealTotalOID};
my $free = $memRealFree->{$memRealFreeOID};
my $memRealUsed = $total - $free;
my $memRealUsedMB = round ($memRealUsed / 1024);
my $totalMB = round($total / 1024);
my $realTPercent = (($memRealUsed - $buffers - $cache)/ $total) * 100;
my $realPercent = sprintf "%.2f", $realTPercent;
答案 0 :(得分:3)
hrStorageUsed
包含缓冲区和缓存,因此您可以像这样估算总可用内存:
totFree = total - used + buffers + cache
例如,使用以下值:
$ snmpwalk -Os -v1 -cpublic localhost hrStorage | grep '\.[167] ='
hrStorageIndex.1 = INTEGER: 1
hrStorageIndex.6 = INTEGER: 6
hrStorageIndex.7 = INTEGER: 7
hrStorageType.1 = OID: hrStorageRam
hrStorageType.6 = OID: hrStorageOther
hrStorageType.7 = OID: hrStorageOther
hrStorageDescr.1 = STRING: Physical memory
hrStorageDescr.6 = STRING: Memory buffers
hrStorageDescr.7 = STRING: Cached memory
hrStorageAllocationUnits.1 = INTEGER: 1024 Bytes
hrStorageAllocationUnits.6 = INTEGER: 1024 Bytes
hrStorageAllocationUnits.7 = INTEGER: 1024 Bytes
hrStorageSize.1 = INTEGER: 1016436
hrStorageSize.6 = INTEGER: 1016436
hrStorageSize.7 = INTEGER: 436156
hrStorageUsed.1 = INTEGER: 882112
hrStorageUsed.6 = INTEGER: 103056
hrStorageUsed.7 = INTEGER: 436156
我们有:
totFree = hrStorageSize.1 - hrStorageUsed.1 + hrStorageUsed.6 + hrStorageUsed.7
= 1016436 - 882112 + 103056 + 436156
= 673536 KB
但等等,这不是free(1)
所说的:
$ free -k
total used free shared buff/cache available
Mem: 1016436 235480 135680 51272 645276 555492
Swap: 1048572 113076 935496
根据这个,我们实际上有:
totFree = free + buff/cache
= 135680 + 645276
= 780956 KB
为什么会出现差异?事实证明,free
和HOST-RESOURCES-MIB都从/proc/meminfo
获取数据,但使用的指标略有不同。来自man free
:
<强>缓冲器强>
内核缓冲区使用的内存(/ proc / meminfo中的缓冲区)
<强>缓存强>
页面缓存和slab使用的内存(/ proc / meminfo中的Cached和Slab)
所以free
包括缓存中的slab分配,而HOST-RESOURCES-MIB则不包括。如果我们得到平板分配:
$ grep Slab /proc/meminfo
Slab: 106064 kB
并将其添加到我们从SNMP获得的内容中,我们有:
totFree = 673536 + 106064
= 779600 KB
更接近我们从free
(780956 KB)获得的内容。
然而,实际上有一个更好的指标。再次来自man free
:
可用强>
估计可用于启动新应用程序的内存量,无需交换。与缓存或空闲字段提供的数据不同,此字段考虑了页面缓存,并且由于正在使用的项目,并非所有可回收的内存块都将被回收(MemAvailable在/ proc / meminfo中,在内核3.14上可用,模拟在内核2.6.27+,否则与免费相同)
请注意,在我的示例中,可用内存(555492 KB)明显低于free + buffers + cache(780956 KB)的总和。
不幸的是,我找不到报告此值的MIB(UCD-SNMP-MIB也没有),所以你可能会坚持我在开始时显示的粗略估计。