package Monitor::lxMeminfo;

require 5.002;

use strict;
use FileHandle;

@Monitor::lxMeminfo::ISA = qw(Monitor::Monitor);

sub new
{
    my ($this, $comm) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};

    bless($self, $class);

    return($self);
}



sub check
{
    my ($self) = @_;
    my %vars = ();

    open( INFO, "</proc/meminfo" );
    while( <INFO> ) {
	chomp;
	if( /^([^\s\t]+):[\s\t]*(\d+) (.)B/ ) {
	    my( $title, $size, $unit ) = ($1,$2,$3);
	    # will it ever be something else than kB?
	    if( $unit eq "k" ) { $size *= 1024; }
	    elsif( $unit eq "M" ) { $size *= 1024*1024; }
	    $self->set_variable( "", "linux.meminfo.$title", $size );
	    $vars{$title} = $size;
	}
    }
    close INFO;
    if( (defined $vars{"SwapTotal"}) 
	&& $vars{"SwapTotal"}
	&& defined $vars{"SwapFree"} ) {
	$self->set_variable( "", "linux.meminfo.%SwapFree", 
	  int(100*$vars{"SwapFree"}/$vars{"SwapTotal"}) );
    }
}

1;
