#!/usr/bin/perl -w
###############################################################################
# Webmin Sysstats - disk.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;
no warnings 'redefine';
use RRDs;
use English '-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 $EMPTY = EMPTY();
# used for rrd names
#my %type_name = ( bloc => $EMPTY, inode => 'i', );
###############################################################################
# caches : use get_df from sysstats-gen-lib
# only 2 calls by run
my $bloc_cache = get_df();
my $inode_cache = get_df('-i');
###############################################################################
# ask the system info on file system
sub read_df($;$) {
my $vol = shift @_; # file system
my $opt = shift @_ || $EMPTY; # options : -i for inodes
my @res = ($opt) ? @{$inode_cache} : @{$bloc_cache};
# search for vol in cache
my $value;
foreach my $ligne (@res) {
my @tab = split_awk($ligne);
my $last_col = $#tab;
if ( $tab[$last_col] eq $vol ) {
$value = $ligne;
last;
}
}
if ( !$value ) {
warning("volume $vol not mounted");
return;
}
else {
return $value;
}
}
###############################################################################
# main
###############################################################################
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;
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];
my $bloc_capa = 'U';
my $inode_capa = 'U';
# a call for each scanned fs to avoid unwanted time-out on nfs
if ( is_param_runstop($runstop) ) {
# blocs
#######
# on linux :
# Sys. de fich. 1024-blocks Used Available Capacity Monté sur
# /dev/sda8 9621848 1399180 7733892 16% /mnt/data
# on bsd
# Filesystem 1024-blocks Used Avail Capacity Mounted on
# /dev/ad3s1d 2008622 457044 1390890 25% /var
my $df = read_df($vol);
if ( defined $df ) {
my @liste = split_awk($df);
my $bloc_capac = $liste[4];
# check for % to check output format
if ( $bloc_capac =~ m/^(\d+)%/ ) {
$bloc_capa = $1;
# old obsolete code
# the filesystem reserve space for root (default 5%)
# total = used + avail + reserved
# so to match real capacity, we have to use
# used + reserved = total - avail
debug(
"$module_name $vol ($real_vol_num) blocs :capacity=$bloc_capa%"
);
action_param( $vol . '_b', $bloc_capa, \%codes );
}
else {
warning("bad output for blocs : $df");
}
}
else {
warning("$vol ($real_vol_num) blocs : no value from df");
}
# inodes
########
# on linux : 5 fields
# Sys. de fich. Inodes IUtil. ILib. %IUti. Monté sur
# /dev/sda10 1228800 225276 1003524 19% /
# on bsd : much more
# Filesystem 1024-blocks Used Avail Capacity iused ifree %iused Mounted on
# /dev/ad3s1d 2008622 457044 1390890 25% 20799 261823 7% /var
# on reiserfs
# /dev/md3 0 0 0 - /home
$df = read_df( $vol, '-i' );
if ( defined $df ) {
my @liste = split_awk($df);
my $last_col = $#liste;
my $inode_capac = $liste[ $last_col - 1 ];
if ( $inode_capac =~ m/^(\d+)%/ ) {
$inode_capa = $1;
debug(
"$module_name $vol ($real_vol_num) inodes : capacity=$inode_capa%"
);
action_param( $vol . '_i', $inode_capa, \%codes );
}
elsif ( $inode_capac eq DASH() ) {
debug("no inodes value on $df");
}
else {
warning("bad output for inodes : $df");
}
}
else {
debug(
"$module_name $vol ($real_vol_num) inodes : no value from df");
}
}
else {
debug("aquisition for $vol stopped");
}
# the format of stored data was changed with 0.24 release
# to avoid several rrd file for one parameter : 1.rrd i1.rrd
# to be able to graph 'old' data, we keep the 2 database for
# at least a year
# and the graph sub use the following algo
# if bloc <= 100
# then new data
# else old data
my $rrdbase = $real_vol_num . '.rrd';
RRDs::update( $rrdbase, "N:$bloc_capa:$inode_capa" );
my $ERR = RRDs::error();
warning("ERROR while updating $rrdbase database : $ERR") if $ERR;
# old inodes rrd contains the same data for compatibility with old schema
# may be suppress in 2012, one year after webminstat 2.0
# $rrdbase = $type_name{'inode'} . $real_vol_num . '.rrd';
# if ( -f $rrdbase ) {
#
# RRDs::update( $rrdbase, "N:$bloc_capa:$inode_capa" );
# $ERR = RRDs::error();
# warning("ERROR while updating $rrdbase database : $ERR") if $ERR;
# }
$vol_num++;
}