![sar:可用内存统计信息(包括缓冲区、缓存)](https://linux22.com/image/800900/sar%EF%BC%9A%E5%8F%AF%E7%94%A8%E5%86%85%E5%AD%98%E7%BB%9F%E8%AE%A1%E4%BF%A1%E6%81%AF%EF%BC%88%E5%8C%85%E6%8B%AC%E7%BC%93%E5%86%B2%E5%8C%BA%E3%80%81%E7%BC%93%E5%AD%98%EF%BC%89.png)
该free
命令以两种形式提供可用内存量:完全可用内存(线Mem
)和用于缓存和缓冲区的可支配内存(-/+ buffers/cache
线):
-bash-3.2$ free -m
total used free shared buffers cached
Mem: 16057 15173 884 0 17 2520
-/+ buffers/cache: 12635 3422
Swap: 12287 4937 7350
在此示例中,不包括缓冲区和缓存的可用内存为 884 MB,包括缓冲区和缓存的可用内存为 3422 MB。
我如何获得第二个测量值(可用内存,包括缓冲区和缓存)sar
?
答案1
标志-r
将为sar
您提供 kbmfree、kbmemused、kbbuffers 和 kbcached。遗憾的是,它不会像命令中的 -/+ buffers/cache 列那样将它们总计起来free
,因此您必须通过 awk 或 perl 脚本对其进行管道传输以将它们总计起来。试试这个:
sar -r |\
awk '
{
if (NR > 3 && $3 != "kbmemfree" && $3 != "") {
if ($1 != "Average:") {
kbmemfree = $3
kbmemused = $4
kbbuffers = $6
kbcached = $7
}
else {
kbmemfree = $2
kbmemused = $3
kbbuffers = $5
kbcached = $6
}
kbfree_plus_bufcache = kbmemfree + kbbuffers + kbcached
kbused_minus_bufcache = kbmemused - kbbuffers - kbcached
printf("%s %15d %15d\n", $0, kbfree_plus_bufcache, kbused_minus_bufcache)
} else if ($3 == "kbmemfree") {
printf("%s %15s %15s\n", $0, "kbfree+bufcache", "kbused-bufcache")
} else {
printf("%s\n", $0)
}
}'