#!/usr/bin/perl -w
###############################################################################
# Webmin Sysstats - mem-lib.pl
#
# Webmin Sysstats Module
# Copyright (C) 2001 by David Bouius
# 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;
unset_warnings();
require RRDs;
set_warnings();
# web-lib.pl
use vars qw( %config %in %text $cb);
# sysstat-lib.pl
use subs qw(&debug debug_sub_begin debug_sub_end &add_rra &install_script );
my $database = 'mem.rrd';
my $database2 = 'mem2.rrd';
my $fscript = 'mem.pl';
###############################################################################
sub install_module($$) {
my ( $minfo, $defaults ) = ( $_[0], $_[1] );
debug_sub_begin();
my %my_config;
$my_config{'name'} = 'mem';
$my_config{'sample_rate'} = 1;
$my_config{'graph_type'} = 1;
$my_config{'nb_graph'} = 2;
$my_config{'runstop'} = 0;
$my_config{'cf'} = 'AVERAGE';
$my_config{'keep'} = 1;
my $heartbeat = compute_heartbeat( $my_config{'sample_rate'} );
my $cf = $my_config{'cf'};
if ( !-r $minfo->{'dbtables'} . $database ) {
my $cmd = $minfo->{'dbtables'} . $database;
$cmd .= get_create_common();
$cmd .= " DS:mtotal:GAUGE:$heartbeat:0:U";
$cmd .= " DS:mused:GAUGE:$heartbeat:0:U";
$cmd .= " DS:stotal:GAUGE:$heartbeat:0:U";
$cmd .= " DS:sused:GAUGE:$heartbeat:0:U";
$cmd .= add_rra($cf);
debug("rrdtool create $cmd");
my @cmd = split( ' ', $cmd );
RRDs::create(@cmd);
my $ERR = RRDs::error();
warning("can not create database $database : $ERR") if $ERR;
}
if ( !-r $minfo->{'dbtables'} . $database2 ) {
my $cmd = $minfo->{'dbtables'} . $database2;
$cmd .= get_create_common();
$cmd .= " DS:mshared:GAUGE:$heartbeat:0:U";
$cmd .= " DS:mbuffers:GAUGE:$heartbeat:0:U";
$cmd .= " DS:mcached:GAUGE:$heartbeat:0:U";
$cmd .= add_rra($cf);
debug("rrdtool create $cmd");
my @cmd = split( ' ', $cmd );
RRDs::create(@cmd);
my $ERR = RRDs::error();
warning("can not create database $database2 : $ERR") if $ERR;
}
$my_config{'mem'} = pack_config(
$defaults->{'real_colour'}, $defaults->{'swap_colour'},
$defaults->{'shared_colour'}, $defaults->{'buffer_colour'},
$defaults->{'cached_colour'}, $defaults->{'free_colour'}
);
write_config( $minfo, \%my_config );
install_script( $minfo, $fscript );
debug_sub_end();
return 1;
}
###############################################################################
sub graph_data($$) {
my ( $minfo, $my_config ) = ( $_[0], $_[1] );
debug_sub_begin();
my (
$real_config, $swap_config, $shared_config,
$buffer_config, $cached_config, $free_config
) = unpack_config( $my_config->{'mem'} );
my ( $real_colour, $real_line ) = get_line_config($real_config);
my ( $swap_colour, $swap_line ) = get_line_config($swap_config);
my ( $shared_colour, $shared_line ) = get_line_config($shared_config);
my ( $buffer_colour, $buffer_line ) = get_line_config($buffer_config);
my ( $cached_colour, $cached_line ) = get_line_config($cached_config);
my ( $free_colour, $free_line ) = get_line_config($free_config);
my $cf = $my_config->{'cf'};
# cgi parameters
my ( $graph_type, $c_display ) = get_cgi_graph($my_config);
my @rrd_detals = ( '--rigid', '-u', 100, '-v', '% use' );
push( @rrd_detals,
'DEF:mtotal=' . $minfo->{'dbtables'} . $database . ':mtotal:' . $cf );
push( @rrd_detals,
'DEF:mused=' . $minfo->{'dbtables'} . $database . ':mused:' . $cf );
push( @rrd_detals,
'DEF:stotal=' . $minfo->{'dbtables'} . $database . ':stotal:' . $cf );
push( @rrd_detals,
'DEF:sused=' . $minfo->{'dbtables'} . $database . ':sused:' . $cf );
push( @rrd_detals,
'DEF:mbuffers='
. $minfo->{'dbtables'}
. $database2
. ':mbuffers:'
. $cf );
push( @rrd_detals,
'DEF:mcached='
. $minfo->{'dbtables'}
. $database2
. ':mcached:'
. $cf );
push( @rrd_detals, 'CDEF:memfree=mtotal,mused,-,mbuffers,-,mcached,-' );
#'CDEF:memfree=mtotal,mused,-,mshared,-,mbuffers,-,mcached,-' );
push( @rrd_detals, 'CDEF:mmed=mused,100,*,mtotal,/' );
#push( @rrd_detals, 'CDEF:msha=mshared,100,*,mtotal,/' );
push( @rrd_detals, 'CDEF:mbuf=mbuffers,100,*,mtotal,/' );
push( @rrd_detals, 'CDEF:mca=mcached,100,*,mtotal,/' );
#push ( @rrd_detals, 'CDEF:mfree=memfree,100,*,mtotal,/' );
#push( @rrd_detals, 'CDEF:mfree=100,mmed,-,msha,-,mbuf,-,mca,-' );
push( @rrd_detals, 'CDEF:mfree=100,mmed,-,mbuf,-,mca,-' );
push( @rrd_detals, 'CDEF:smed=sused,100,*,stotal,/' );
push( @rrd_detals,
"COMMENT:Type Max Min $text{'graph_average'}\\n" );
if ( $graph_type == 1 ) {
# lines
push( @rrd_detals, $real_line . ':mmed#' . $real_colour . ':Programs' );
gprint( 'mmed', \@rrd_detals );
#push( @rrd_detals,
# $shared_line . ':msha#' . $shared_colour . ':shared' );
#gprint( 'msha', \@rrd_detals );
push( @rrd_detals,
$buffer_line . ':mbuf#' . $buffer_colour . ':buffers' );
gprint( 'mbuf', \@rrd_detals );
push( @rrd_detals,
$cached_line . ':mca#' . $cached_colour . ':cached' );
gprint( 'mca', \@rrd_detals );
push( @rrd_detals, $free_line . ':mfree#' . $free_colour . ':free' );
gprint( 'mfree', \@rrd_detals );
push( @rrd_detals, $swap_line . ':smed#' . $swap_colour . ':Swap' );
gprint( 'smed', \@rrd_detals );
}
else {
# histograms
push( @rrd_detals, 'AREA:mmed#' . $real_colour . ':Programs' );
gprint( 'mmed', \@rrd_detals );
#push( @rrd_detals, 'STACK:msha#' . $shared_colour . ':shared' );
#gprint( 'msha', \@rrd_detals );
push( @rrd_detals, 'STACK:mbuf#' . $buffer_colour . ':buffers' );
gprint( 'mbuf', \@rrd_detals );
push( @rrd_detals, 'STACK:mca#' . $cached_colour . ':cached' );
gprint( 'mca', \@rrd_detals );
push( @rrd_detals, 'STACK:mfree#' . $free_colour . ':free' );
gprint( 'mfree', \@rrd_detals );
push( @rrd_detals, $swap_line . ':smed#' . $swap_colour . ':Swap' );
gprint( 'smed', \@rrd_detals );
}
graph_common( $minfo, $my_config, @rrd_detals );
debug_sub_end();
return;
}
###############################################################################
sub mod_config($$$) {
my ( $minfo, $my_config, $defaults ) = ( $_[0], $_[1], $_[2] );
debug_sub_begin();
my (
$real_config, $swap_config, $shared_config,
$buffer_config, $cached_config, $free_config
) = unpack_config( $my_config->{'mem'} );
my ( $real_colour, $real_line ) = get_line_config($real_config);
my ( $swap_colour, $swap_line ) = get_line_config($swap_config);
my ( $shared_colour, $shared_line ) = get_line_config($shared_config);
my ( $buffer_colour, $buffer_line ) = get_line_config($buffer_config);
my ( $cached_colour, $cached_line ) = get_line_config($cached_config);
my ( $free_colour, $free_line ) = get_line_config($free_config);
begin_html_config();
change_database( $minfo, $my_config );
change_local_params($my_config);
change_scale( $minfo, $my_config );
change_default( $my_config->{'graph_type'},
$text{'word_line'}, $text{'word_bar'} );
separator();
# colors
titre_table( $text{'module_subtitle'} );
begin_table('colors'); # colors
# memory
print "<tr>\n";
title_row( $text{'module_word_real'}, w15() );
option_colourselect( 'real', $real_colour, $defaults->{'real_colour'} );
option_lineselect( 'real', $real_line );
print "</tr>\n";
# buffers
print "<tr>\n";
title_row( $text{'module_word_buffers'}, w15() );
option_colourselect( 'buffer', $buffer_colour,
$defaults->{'buffer_colour'} );
option_lineselect( 'buffer', $buffer_line );
print "</tr>\n";
# cached
print "<tr>\n";
title_row( $text{'module_word_cached'}, w15() );
option_colourselect( 'cached', $cached_colour,
$defaults->{'cached_colour'} );
option_lineselect( 'cached', $cached_line );
print "</tr>\n";
# free
print "<tr>\n";
title_row( $text{'module_word_free'}, w15() );
option_colourselect( 'free', $free_colour, $defaults->{'free_colour'} );
option_lineselect( 'free', $free_line );
print "</tr>\n";
# swap
print "<tr>\n";
title_row( $text{'module_word_swap'} );
option_colourselect( 'swap', $swap_colour, $defaults->{'swap_colour'} );
option_lineselect( 'swap', $swap_line );
print "</tr>\n";
end_table(); # colors
# button
end_html_config();
debug_sub_end();
return;
}
###############################################################################
sub mod_config_save($$$) {
my ( $minfo, $my_config, $defaults ) = ( $_[0], $_[1], $_[2] );
debug_sub_begin();
$my_config->{'mem'} = pack_config(
line_col( 'real', $defaults->{'real_colour'} ),
line_col( 'swap', $defaults->{'swap_colour'} ),
line_col( 'shared', $defaults->{'shared_colour'} ),
line_col( 'buffer', $defaults->{'buffer_colour'} ),
line_col( 'cached', $defaults->{'cached_colour'} ),
line_col( 'free', $defaults->{'free_colour'} ),
);
test_database_change( $minfo, $my_config );
test_local_params($my_config);
test_scale_change( $minfo, $my_config );
test_default_change($my_config);
write_config( $minfo, $my_config );
debug_sub_end();
return;
}
###############################################################################
sub write_config($$) {
my ( $minfo, $my_config ) = ( $_[0], $_[1] );
debug_sub_begin();
common_write_config( $minfo, $my_config );
debug_sub_end();
return;
}
###############################################################################
sub upgrade($$$) {
my ( $minfo, $my_config, $defaults ) = ( $_[0], $_[1], $_[2] );
debug_sub_begin();
debug_sub_end();
return;
}
###############################################################################
1;