Tuesday, August 4, 2015

Sar: Free memory statistics (including buffers, cache)



The free commands gives the amount of free memory in two forms: completely free memory (the Mem line) and disposable memory used for caches and buffers (the -/+ buffers/cache line):



-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



In this example, there are 884 MB of free memory excluding buffers and cache, and 3422 MB of free memory including buffers and cache.



How can I get the second measurement (free memory, including buffers and cache) in sar?


Answer



The -r flag for sar will give you kbmfree, kbmemused, kbbuffers, and kbcached. It doesn't, unfortunately, total them like the -/+ buffers/cache column like the free command, so you will have to pipe it through an awk or perl script to total them. Try this:



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)
}
}'

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...