#!/usr/bin/perl -w
###############################################################################
# Webmin Sysstats - mem.pl
#
# Webmin Sysstats Module
# Copyright (C) 2002 by Eric Gerbier
# Bug reports to: gerbier@users.sourceforge.net
# $Id$
#
# 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
# (at your option) 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.
#
###############################################################################
use strict;
use warnings;
# to be used alone or from sysstat.pl without warning
# because require verify the path
require '../../gen-lib.pl' if ( !exists $ENV{WEBMINSTAT_TEMP} );
use RRDs;
my $info = '/proc/meminfo';
###############################################################################
# fix for 2.6 kernel : MemShared does not exist any more, so set it to 0
# to avoid some undefined values
my (
$mem_total, $mem_used, $swap_total, $swap_used,
$mem_shared, $mem_buffers, $mem_cached
) = ( 'U', 'U', 'U', 'U', 0, 'U', 'U' );
my ( $swap_free, $mem_free );
my $fh_proc;
if ( open( $fh_proc, '<', $info ) ) {
while (<$fh_proc>) {
# old code : do not work on 2.6 kernels
# if (m/^Mem:/) {
# my @tab = split;
# $mtotal = $tab[1];
# $mused = $tab[2] - $tab[5] - $tab[6];
# $mshared = $tab[4];
# $mbuffers = $tab[5];
# $mcached = $tab[6];
# }
# elsif (m/^Swap:/) {
# my @tab = split;
# $stotal = $tab[1];
# $sused = $tab[2];
# }
# new code , compatible with 2.6 kernel
if (m/^MemTotal:\s*(\d+)/) {
$mem_total = $1;
}
elsif (m/^MemFree:\s*(\d+)/) {
$mem_free = $1;
}
elsif (m/^Buffers:\s*(\d+)/) {
$mem_buffers = $1;
}
elsif (m/^Cached:\s*(\d+)/) {
$mem_cached = $1;
}
elsif (m/^MemShared:\s*(\d+)/) {
$mem_shared = $1;
}
elsif (m/^SwapTotal:\s*(\d+)/) {
$swap_total = $1;
}
elsif (m/^SwapFree:\s*(\d+)/) {
$swap_free = $1;
}
}
close($fh_proc);
}
else {
warning("can not open $info : $!");
}
#debug(
#"mem : memtotal=$mtotal memused=$mused memshared=$mshared membuffer=$mbuffers memcached=$mcached swaptotal=$stotal swapused=$sused" );
$swap_used = $swap_total - $swap_free;
$mem_used = $mem_total - $mem_free - $mem_buffers - $mem_cached;
debug(
"mem : memtotal=$mem_total memused=$mem_used memshared=$mem_shared membuffer=$mem_buffers memcached=$mem_cached swaptotal=$swap_total swapused=$swap_used"
);
RRDs::update( 'mem.rrd', "N:$mem_total:$mem_used:$swap_total:$swap_used" );
my $ERR = RRDs::error();
warning("ERROR while updating mem.rrd: $ERR") if $ERR;
RRDs::update( 'mem2.rrd', "N:$mem_shared:$mem_buffers:$mem_cached" );
$ERR = RRDs::error();
warning("ERROR while updating mem2.rrd: $ERR") if $ERR;