#!/usr/bin/perl -w # # Plugin to monitor memory usage. # # Slab cache memory checking added by Mike Fedyk # # Parameters: # # config (required) # autoconf (optional - only used by lrrd-config) # # Magic markers (optional - only used by lrrd-config and some # installation scripts): #%# family=auto #%# capabilities=autoconf if ($ARGV[0] and $ARGV[0] eq "autoconf") { if (-r "/proc/meminfo" && -r "/proc/slabinfo") { print "yes\n"; exit 0; } else { print "no (/proc/meminfo or /proc/slabinfo) found\n"; exit 1; } } my %mems; &fetch_meminfo; if ($ARGV[0] and $ARGV[0] eq "config") { print "graph_args --base 1024 -l 0 --vertical-label Bytes --upper-limit ", $mems{'MemTotal'}, "\n"; print "graph_title Memory usage\n"; print "graph_order apps slab swap_cache cached buffers free swap\n"; print "apps.label apps\n"; print "apps.draw AREA\n"; print "buffers.label buffers\n"; print "buffers.draw STACK\n"; print "slab.label slab_cache\n"; print "slab.draw STACK\n"; print "swap.label swap\n"; print "swap.draw STACK\n"; print "cached.label cache\n"; print "cached.draw STACK\n"; print "free.label unused\n"; print "free.draw STACK\n"; if (exists $mems{'SwapCached'}) { print "swap_cache.label swap_cache\n"; print "swap_cache.draw STACK\n"; } if (exists $mems{'Committed_AS'}) { print "committed.label committed\n"; print "committed.draw LINE2\n"; print "committed.warn ", ($mems{SwapTotal}+$mems{MemTotal})*1024, "\n"; } exit 0; } print "apps.value ", $mems{MemTotal}-$mems{MemFree}-$mems{Buffers}-$mems{Cached}-$mems{Slab}, "\n"; print "free.value ", $mems{MemFree}, "\n"; print "buffers.value ", $mems{Buffers}, "\n"; print "cached.value ", $mems{Cached}, "\n"; print "swap.value ", $mems{SwapTotal}-$mems{SwapFree}, "\n"; print "slab.value ", $mems{Slab}, "\n"; if (exists $mems{'SwapCached'}) { print "swap_cache.value ", $mems{SwapCached}, "\n"; } if (exists $mems{'Committed_AS'}) { print "committed.value ", $mems{'Committed_AS'}, "\n"; } sub fetch_meminfo { my (@memline, @swapline); open (IN, "/proc/meminfo") || die "Could not open /proc/meminfo for reading: $!"; while () { if (/^(\w+):\s*(\d+)\s+kb/i) { $mems{$1} = $2 * 1024; } elsif (/^Mem:\s+(.+)$/) { @memline = split; } elsif (/^Swap:\s+(.+)$/) { @swapline = split; } } close (IN); if (!$mems{Slab}) { $mems{Slab} = &fetch_slabinfo; } if (!$mems{MemTotal}) { $mems{MemTotal} = $memline[1]; $mems{MemFree} = $memline[3]; $mems{Buffers} = $memline[5]; $mems{Cached} = $memline[6]; $mems{SwapTotal} = $swapline[1]; $mems{SwapFree} = $swapline[3]; } } sub fetch_slabinfo { open (IN, "/proc/slabinfo") || die "Could not open /proc/slabinfo for reading: $!"; my $tot_slab_pages = 0; while () { if (!/^slabinfo/) { @slabinfo = split; $tot_slab_pages += $slabinfo[5]; } } close (IN); return $tot_slab_pages * 4096 }