#!/usr/bin/perl -w
###############################################################################
# Webmin Sysstats - users.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;
use RRDs;
use English qw(-no_match_vars);
use FindBin;
# to be used alone or from sysstat.pl without warning
# because require verify the path
if ( !exists $ENV{WEBMINSTAT_TEMP} ) {
use lib "$FindBin::Bin/../..";
## no critic (RequireBarewordIncludes)
require 'sysstats-aquisition-lib.pl';
## use critic
}
my $module_name;
my $info = 'w -h';
###############################################################################
sub read_users() {
my $pipe = read_pipe_stream($info);
my %hash;
while (<$pipe>) {
my @tab = split_awk($_);
$hash{ $tab[0] }++;
}
return \%hash;
}
###############################################################################
my %config;
read_file_cached( 'config', \%config );
$module_name = $config{'name'};
debug( "run $module_name version " . $config{'version'} );
my %codes = action_load( \%config );
my $pre = $config{'pre'};
my $vol_num = 1;
my $users_conn = read_users();
if ( !$users_conn ) {
warning("no data from $info");
goto FIN;
}
my $other_base;
while ( exists $config{ $pre . $vol_num } ) {
my @tab = split /,/, $config{ $pre . $vol_num };
my $real_vol_num = $tab[0];
my $vol = $tab[1];
my $runstop = $tab[3];
#debug( "volume = $vol");
my $total = 'U';
if ( is_param_runstop($runstop) ) {
if ( $vol ne 'other' ) {
if ( exists $users_conn->{$vol} ) {
$total = $users_conn->{$vol};
}
else {
$total = 0;
}
# remove lines for others count
delete $users_conn->{$vol};
#debug( 'reste ' . scalar(@users_conn) );
debug("$module_name $vol ($real_vol_num) : total=$total");
action_param( $vol, $total, \%codes );
my $rrdbase = $real_vol_num . '.rrd';
RRDs::update( $rrdbase, "N:$total" );
my $ERR = RRDs::error();
warning("ERROR while updating $rrdbase database : $ERR") if $ERR;
}
else {
# keep database number
$other_base = $real_vol_num;
}
}
else {
debug("aquisition for $vol stopped");
}
$vol_num++;
} # while
# special case : other
if ($other_base) {
#debug( 'special other case';
my $total = 0;
foreach my $key ( keys %{$users_conn} ) {
$total += $users_conn->{$key};
}
debug("$module_name others ($other_base) : total=$total");
action_param( 'other', $total, \%codes );
my $rrdbase = $other_base . '.rrd';
RRDs::update( $rrdbase, "N:$total" );
my $ERR = RRDs::error();
warning("ERROR while updating $rrdbase database : $ERR") if $ERR;
}
# a label to quit
#I can not use return when executing this script standalone,
# so I had to use goto
FIN: