#!/usr/bin/perl -w
#
# OBM Plugin to monitor memory usage, based on memoty plugin of munin
# This plugin is more simple, vise used an unused memory
# OBM developper: Sylvain Garcia
#
# Origional Author: Jimmy Olsen
# Contributors:
# Mike Fedyk: Slab, SwapCached, PageTables, VmallocUsed, Mapped, Active, Inactive, 2.4 Rmap & 2.6
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - only used by munin-config)
#%# family=auto
#%# capabilities=autoconf

if ($ARGV[0] and $ARGV[0] eq "autoconf")
{
	if (-r "/proc/meminfo")
	{
		print "yes\n";
		exit 0;
	}
	else
	{
		print "/proc/meminfo not 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 System\n";
	print "graph_category system\n";
	print "graph_info This graph shows what the machine uses its memory for.\n";
	print "graph_order ";
	print "used ";
	print "free ";
	print "\n";
	print "used.label used\n";
	print "used.draw AREA\n";
	print "used.info Memory used by user-space applications.\n";
	print "free.label unused\n";
	print "free.draw STACK\n";
	print "free.info Wasted memory. Memory that is not used for anything at all.\n";
	exit 0;
}

print "used.value ", $mems{'MemTotal'}
	-$mems{'MemFree'}
	-$mems{'Cached'}
	,"\n";
print "free.value ", $mems{'MemFree'} +$mems{'Cached'} , "\n";


sub fetch_meminfo
{
	open (IN, "/proc/meminfo") || die "Could not open /proc/meminfo for reading: $!";
	while (<IN>)
	{
		if (/^(\w+):\s*(\d+)\s+kb/i)
		{
			$mems{"$1"} = $2 * 1024;
		}
	}
	close (IN);
}

# vim:syntax=perl:ts=8
