#!/usr/bin/perl -w
###############################################################################
# Webmin Sysstats - load.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 English qw(-no_match_vars);
use RRDs;
# to be used alone or from sysstat.pl without warning
# because require verify the path
if ( !exists $ENV{WEBMINSTAT_TEMP} ) {
use FindBin;
use lib "$FindBin::Bin/../..";
## no critic(RequireBarewordIncludes)
require 'sysstats-aquisition-lib.pl';
## use critic
}
# there are several way to get load
# /proc/loadavg file
# uptime command
# but uptime seems to exists on all unix
my $info = 'uptime';
###############################################################################
sub read_load() {
my ( $load1, $load5, $load15 ) = ( 'U', 'U', 'U' );
my $r_tab = read_pipe($info);
foreach ( @{$r_tab} ) {
s/,//g;
if (m/load averages?: (.*)/) {
( $load1, $load5, $load15 ) = split_awk($1);
last;
}
}
return ( $load1, $load5, $load15 );
}
###############################################################################
my %config;
read_file_cached( 'config', \%config );
my %codes = action_load( \%config );
my @t_load = read_load();
#return unless (@t_load);
my %h_load = (
load1 => $t_load[0],
load5 => $t_load[1],
load15 => $t_load[2],
);
#foreach my $elem (keys %h_load) {
# debug( "$elem -> $h_load{$elem}");
#}
my $pre = $config{'pre'};
my $load_num = 1;
while ( exists $config{ $pre . $load_num } ) {
my @tab = split /,/, $config{ $pre . $load_num };
my $real_load_num = $tab[0];
my $load = $tab[1];
my $load_name = $tab[2];
my $runstop = $tab[4];
#debug( "load = $load");
my $total = 'U';
if ( is_param_runstop($runstop) ) {
if ( exists $h_load{$load} ) {
$total = $h_load{$load};
}
else {
warning("load : can not find $load value");
}
debug("load $load ($load_name $real_load_num): total=$total");
action_param( $load, $total, \%codes );
}
else {
debug("aquisition for $load stopped");
}
my $rrdbase = $real_load_num . '.rrd';
RRDs::update( $rrdbase, "N:$total" );
my $ERR = RRDs::error();
warning("ERROR while updating $rrdbase: $ERR") if $ERR;
$load_num++;
}