#!/usr/bin/perl
# rrd-ram.pl
# v2.4 - Nicolas AGIUS 03/2010
# Usage : rrd-ram.pl
#
###################################################################################
## Sonde RRD pour l'utilisation mémoire
#
# Copyright (C) 2006 Nicolas AGIUS <nicolas_agius@yahoo.fr>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
###################################################################################


use POSIX;


open(MEM,"</proc/meminfo") or die "Cannot open meminfo";
while(<MEM>)
{
	if(/MemTotal.\s+(\d+)/)
	{
		$memtotal=$1;
	}
	elsif(/MemFree.\s+(\d+)/)
	{
		$memfree=$1;
	}
	elsif(/Buffers.\s+(\d+)/)
	{
		$buffers=$1;
	}
	elsif(/Cached.\s+(\d+)/)
	{
		$cached=$1;
	}
	elsif(/SwapTotal.\s+(\d+)/)
	{
		$swaptotal=$1;
	}
	elsif(/SwapFree.\s+(\d+)/)
	{
		$swapfree=$1;
	}
}
close(MEM);

# Protection against "divide by zero"
if($swaptotal == 0)
{
	$swaptotal=1;
	$swapfree=1;
}


$used=($memtotal-$memfree-$buffers-$cached)*100/$memtotal;
$buffer=($buffers+$cached)*100/$memtotal;
$swap=($swaptotal-$swapfree)*100/$swaptotal;

printf("RAM_USED=%.2f\n",$used);
printf("BUFFER=%.2f\n",$buffer);
printf("SWAP_USED=%.2f\n",$swap);


exit 0;
