#!/usr/bin/perl -w
###############################################################################
#
# Webmin Sysstats Module
# Copyright (C) 2004 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;
use English qw(-no_match_vars);
unset_warnings();
require RRDs;
set_warnings();
# from web-lib.pl
## no critic (ProhibitPackageVars)
use vars qw(%config %text %in $in $cb $tb %access );
## use critic
my $module_name = 'load';
my $fscript = $module_name . '.pl';
my $pre = 'vol';
my $data_name = 'total';
my $EMPTY = EMPTY();
###############################################################################
sub install_module($$) {
my ( $minfo, $defaults ) = ( $_[0], $_[1] );
debug_sub_begin();
my @colors = generate_colors();
my %my_config = init_module_config( $module_name, 1 );
$my_config{'pre'} = $pre;
$my_config{'sample_rate'} = 1;
$my_config{'keep'} = 0; # dynamique
$my_config{'cf'} = 'MAX';
$my_config{'graph_type'} = 1;
$my_config{'nb_graph'} = 2;
# allways initialize databases
my @vol_list = tab_load();
my $db_uid = 1;
foreach my $vol (@vol_list) {
create_base( $minfo, \%my_config, $db_uid );
# create config file
my ( $vol_number, $vol_name ) = split /:/, $vol;
if ( !$vol_name ) {
$vol_name = $vol_number;
}
else {
# is there any "," training ?
$vol_name =~ s/,//;
$vol_name =~ s/ //;
}
$my_config{ $pre . $db_uid } =
pack_config( $db_uid, $vol_number, $vol_name,
$colors[ ( $db_uid - 1 ) % scalar @colors ],
0, 0 );
$db_uid++;
}
$my_config{'db_uid'} = $db_uid;
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 $cf = $my_config->{'cf'};
# take care of cgi parameters
my ( $graph_type, $c_display ) = get_cgi_graph($my_config);
my @rrd_detals;
gprint_title( $module_name, 'load', \@rrd_detals );
# and a line for the limit of overload
my $limit = find_num_cpus();
push @rrd_detals, 'HRULE:' . $limit . '#FF0000';
my $vol_num = 1;
my $first_graph = 1;
VOL: while ( defined $my_config->{ $pre . $vol_num } ) {
my $vol_nb = $pre . $vol_num;
my ( $vol_id, $vol_number, $vol_name, $vol_config, $runstop, $display )
= unpack_config( $my_config->{$vol_nb} );
if ($runstop) {
$vol_num++;
next VOL;
}
if ( $display == $c_display ) {
add_def( \@rrd_detals, $vol_nb, $minfo->{'dbtables'} . $vol_id,
$data_name, $cf );
if ( $graph_type == 0 ) {
# histograms
my $type;
if ($first_graph) {
$first_graph = 0;
$type = 'AREA';
}
else {
$type = 'STACK';
}
gprint_param( \@rrd_detals, $vol_nb, $vol_name, $vol_config,
$type );
}
else {
# lines
gprint_param( \@rrd_detals, $vol_nb, $vol_name, $vol_config );
}
}
$vol_num++;
}
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();
mod_config_opt( $minfo, $my_config, $pre );
begin_html_config(1);
# general
change_database( $minfo, $my_config );
change_local_params($my_config);
change_display($my_config);
change_scale( $minfo, $my_config );
change_default( $my_config->{'graph_type'},
$text{'word_line'}, $text{'word_bar'} );
my @columns = (
'module_subtitle_load_name', 'module_display_name',
'word_subtitle_disp_colour', 'word_subtitle_controls',
);
end_html_config(@columns);
my @vol_list = tab_load();
my $vol_num = 1;
my $vol_sname;
while ( defined $my_config->{ $pre . $vol_num } ) {
my ( $vol_id, $vol_number, $vol_name, $vol_config, $runstop, $display )
= unpack_config( $my_config->{ $pre . $vol_num } );
my ( $vol_colour, $vol_line ) = get_line_config($vol_config);
foreach my $l (@vol_list) {
my ( $n, $name ) = split /:/, $l;
if ( $n eq $vol_number ) {
$vol_sname = $name;
last;
}
}
if ( !$vol_sname ) {
$vol_sname = $vol_name;
}
begin_row($cb); # row 1
# name
print
"<td><strong><a href=\"edit_module.cgi?idx=$in{'idx'}&opt=edit&vol_num=$vol_num\">$vol_sname</a></strong></td>\n";
print "<td>$vol_name</td>\n";
# color
print "<td><font color=\"#$vol_colour\"><strong>";
print $text{ 'colour_' . $vol_colour }
? $text{ 'colour_' . $vol_colour }
: "<pre>#$vol_colour </pre>";
print "</strong></font></td>\n";
# controls
print '<td align=right>';
display_parameter_line( $vol_num, $runstop, $display, $my_config,
$pre );
print "</td>\n";
end_row(); # row 1
$vol_num++;
}
end_table();
if ( ( exists $in{'opt'} ) and ( $in{'opt'} eq 'edit' ) ) {
my ( $vol_id, $vol_number, $vol_name, $vol_config, $runstop, $display )
= unpack_config( $my_config->{ $pre . $in{'vol_num'} } );
begin_table( 'edition', 'border' ); # table 1
begin_row($tb); # row 1
title_row( $text{'module_load'} );
end_row(); # row 1
begin_row($cb); # row 2
print '<td>';
print '<form action="save_config.cgi">';
print '<table ' . w100() . ">\n"; # table 2
begin_row(); # row 3
title_row( $text{'module_word_load'}, w15() );
title_row( $vol_name, 'nowrap' );
end_row(); # row 3
modify_colour( $vol_config, $display, $runstop, $my_config, $pre );
# name
print_input( $text{'module_display_name'},
'vol_name_def', $vol_name, 0, 0, 1 );
print_button( $pre, $in{'idx'}, 'update_vol', $in{'vol_num'},
$text{'word_update'} );
print "</table>\n"; # table 2
print "</form>\n";
print "</td>\n";
end_row(); # row 2
end_table(); # table 1
}
else {
# new ones
my @vol_unknown = filter_known( $my_config, @vol_list );
begin_table( 'new_config', 'border' ); # table 1
begin_row($tb); # row 1
title_row( $text{'module_new_load'} );
end_row(); # row 1
begin_row($cb); # row 1
print '<td>';
print '<form action="save_config.cgi">';
print '<table ' . w100() . ">\n"; # table 2
begin_row(); # row 3
title_row( $text{'module_word_load'}, w15() );
my $i = 0;
print '<td nowrap><strong>';
print "<select name=\"new_vol_def\">\n";
print '<option value="' . $i++ . "\">$text{'word_select'}\n";
foreach my $vlist (@vol_unknown) {
print '<option value="' . $i++ . "\">$vlist\n";
}
print '</select>';
print '</strong></td>';
end_row(); # row 3
modify_colour( '00ff00', 0, 0, $my_config, $pre );
# name
print_input( $text{'module_display_name'},
'new_vol_name_def', q{}, 0, 0, 1 );
print_button( $pre, $in{'idx'}, 'new_vol', $vol_num,
$text{'word_addnew'} );
print "</table>\n"; # table 2
print "</form>\n";
print "</td>\n";
end_row(); # row 2
end_table(); # table 1
}
end_table();
print "</table>\n";
debug_sub_end();
return;
}
###############################################################################
sub mod_config_save($$$) {
my ( $minfo, $my_config, $defaults ) = ( $_[0], $_[1], $_[2] );
debug_sub_begin();
if ( $in{'opt'} eq 'new_vol' ) {
if (
( $in{'new_vol_name_def'} eq $EMPTY )
or ( ( $in{'vol_colour1_def'} eq 'nocolour' )
and ( $in{'vol_colour2_def'} eq $EMPTY ) )
)
{
error( $text{'save_einvaled_selection'} );
}
create_base( $minfo, $my_config, $my_config->{'db_uid'} );
my @vol_list = filter_known( $my_config, tab_load() );
my $vol_number = @vol_list[ $in{'new_vol_def'} - 1 ];
$vol_number =~ s/://;
$my_config->{ $pre . $in{$pre} } = pack_config(
$my_config->{'db_uid'}++,
$vol_number,
$in{'new_vol_name_def'},
set_line_config(
get_colourselect( 'vol_colour', $EMPTY ),
get_lineselect('vol_line')
),
0,
$in{'display_def'}
);
write_config( $minfo, $my_config );
myredirect("edit_module.cgi?idx=$in{'idx'}");
}
elsif ( $in{'opt'} eq 'update_vol' ) {
if ( ( $in{'vol_colour1_def'} eq 'nocolour' )
and ( $in{'vol_colour2_def'} eq $EMPTY ) )
{
error( $text{'save_einvaled_selection'} );
}
my ( $vol_id, $vol_number, $vol_name, $vol_colour, $runstop, $display )
= unpack_config( $my_config->{ $pre . $in{$pre} } );
$my_config->{ $pre . $in{$pre} } = pack_config(
$vol_id,
$vol_number,
$in{'vol_name_def'},
set_line_config(
get_colourselect( 'vol_colour', $EMPTY ),
get_lineselect('vol_line')
),
$in{'runstop_def'},
$in{'display_def'}
);
# test for display : force to create an dummy label
test_for_new_display($my_config);
write_config( $minfo, $my_config );
myredirect("edit_module.cgi?idx=$in{'idx'}");
}
elsif ( $in{'opt'} eq 'general' ) {
test_database_change( $minfo, $my_config );
test_local_params($my_config);
test_display_change($my_config);
test_scale_change( $minfo, $my_config );
test_default_change($my_config);
write_config( $minfo, $my_config );
}
else {
error($in);
}
debug_sub_end();
return;
}
###############################################################################
sub tab_load () {
debug_sub_begin();
my @tab = ( 'load1', 'load5', 'load15' );
debug_sub_end();
return @tab;
}
###############################################################################
sub create_base($$$) {
my $minfo = shift @_;
my $my_config = shift @_;
my $db_uid = shift @_;
debug_sub_begin();
my $cf = $my_config->{'cf'};
my $heartbeat = compute_heartbeat( $my_config->{'sample_rate'} );
my $base = $minfo->{'dbtables'} . $db_uid . '.rrd';
create_rrd( $base, "DS:$data_name:GAUGE:$heartbeat:0:U", $cf );
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 preupgrade($$$) {
my ( $minfo, $my_config, $defaults ) = ( $_[0], $_[1], $_[2] );
debug_sub_begin();
change_ds( $minfo, $module_name, $data_name );
debug_sub_end();
return;
}
###############################################################################
sub postupgrade($$$) {
my ( $minfo, $my_config, $defaults ) = ( $_[0], $_[1], $_[2] );
debug_sub_begin();
my $changes = 0;
# copy and rename old database
my $oldbase = 'load.rrd';
if ( -f $minfo->{'dbtables'} . $oldbase ) {
update_base( $minfo, $oldbase, '1.rrd' );
mod_cf( 'AVERAGE', 'MAX', ( $minfo->{'dbtables'} . '/1.rrd' ) );
$changes++;
}
$changes += change_pre( $my_config, $module_name, $pre );
write_config( $minfo, $my_config ) if ($changes);
debug_sub_end();
return;
}
###############################################################################
# get/set_display/runstop must be defined to allow mod_config_opt to work
sub toggle_display($) {
my $line = shift @_;
my ( $vol_id, $vol_number, $vol_name, $vol_config, $runstop, $display ) =
unpack_config($line);
$display = toggle_param_display($display);
$line = pack_config( $vol_id, $vol_number, $vol_name, $vol_config, $runstop,
$display );
return $line;
}
###############################################################################
sub toggle_runstop($) {
my $line = shift @_;
my ( $vol_id, $vol_number, $vol_name, $vol_config, $runstop, $display ) =
unpack_config($line);
$runstop = toggle_param_runstop($runstop);
$line = pack_config( $vol_id, $vol_number, $vol_name, $vol_config, $runstop,
$display );
return $line;
}
###############################################################################
sub list_params($) {
my $my_config = $_[0];
return list_used_param($my_config);
}
###############################################################################
1;