#!/usr/bin/perl -w
###############################################################################
# Webmin Sysstats - ups.pl
#
# Webmin Sysstats Module
# Copyright (C) 2003 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);
# 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
}
use RRDs;
###############################################################################
sub getupsstatus($$) {
my $server = shift @_;
my $ups_cmd = shift @_;
my $cmd = "$ups_cmd $server";
my $r_tab = read_pipe($cmd);
my %result;
foreach ( @{$r_tab} ) {
if (m/^(.*):\s+([+-]?\d+\.?\d*)/) {
$result{$1} = $2;
}
}
return \%result;
}
###############################################################################
my %config;
read_file_cached( 'config', \%config );
my $param = 'vol';
my $vol_num = 1;
my $server = $config{'server'};
my $ups_cmd = $config{'upsc'};
if ( !$ups_cmd ) {
warning('unconfigured upsc command');
goto FIN;
}
elsif ( !-x $ups_cmd ) {
warning("upsc command $ups_cmd not found");
goto FIN;
}
elsif ( !$server ) {
warning('ups : no server defined');
goto FIN;
}
else {
my $result = getupsstatus( $server, $ups_cmd );
if ( !defined $result ) {
warning('no data from ups acquisition');
goto FIN;
}
while ( exists $config{ $param . $vol_num } ) {
my @tab = split /,/, $config{ $param . $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 ( exists $result->{$vol} ) {
$total = $result->{$vol};
debug("ups $vol ($real_vol_num) : val=$total");
}
else {
debug("ups $vol ($real_vol_num) does not exists in data");
}
}
else {
debug("aquisition for $vol stopped");
}
my $rrdbase = $real_vol_num . '.rrd';
RRDs::update( $rrdbase, "N:$total" );
my $ERR = RRDs::error();
warning("ups : ERROR while updating $rrdbase: $ERR") if $ERR;
$vol_num++;
}
}
# a label to quit
#I can not use return when executing this script standalone,
# so I had to use goto
FIN: