#!/usr/bin/perl -w
###############################################################################
#
# 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 File::Basename;
#use Data::Dumper;
# 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;
my $EMPTY = EMPTY();
my $SPACE = SPACE();
###############################################################################
# try to detect ata disk to add an option
sub add_ata($) {
my $device = shift @_; # /dev/hda
my $trunk_device = basename($device);
if ( $trunk_device =~ m/^sd/ ) {
return '-d ata ';
}
else {
return $EMPTY;
}
}
###############################################################################
# get smart parameters
sub get_smart_param($$$) {
my $cmd = shift @_; # smartctl with path
my $smartctl_options = shift @_;
my $device = shift @_; # /dev/hda
debug($device);
$smartctl_options .= ' -A ' . add_ata($device);
my $fullcmd = "$cmd $smartctl_options $device" . stderr_debug();
my %data;
my $fh;
## no critic (ProhibitTwoArgOpen,RequireBriefOpen)
if ( open $fh, "$fullcmd |" ) {
# get parameters values
# see also smart-lib code
my $found = 0;
while ( my $line = <$fh> ) {
chomp $line;
# skip blank lines
next if ( $line eq $EMPTY );
#next if ( $line =~ m/^$/ );
# skip lines until find ID#
if ( $line =~ m/^ID#/ ) {
$found = 1;
next;
}
elsif ($found) {
my @tab = split_awk($line);
# first elem is a number
# second is the name
my $name = $tab[1];
# value can be a number
# or something like : 160h23m
if ( $tab[9] =~ m/^(\d+)/ ) {
$data{$name} = $1;
}
}
}
close $fh or warning("can not close $fullcmd : $ERRNO");
}
else {
warning("can not open $fullcmd : $ERRNO");
}
#print Dumper(\%data);
return %data;
}
###############################################################################
# get smart global status
sub get_smart_status($$$) {
my $cmd = shift @_;
my $smartctl_options = shift @_;
my $device = shift @_;
debug($device);
$smartctl_options .= ' -H ' . add_ata($device);
my $fullcmd = "$cmd $smartctl_options $device >/dev/null" . stderr_debug();
# todo : change to prototype $cmd, @smartctl_options
system $fullcmd ;
my $retcode = $CHILD_ERROR;
# status analysis
# my $e_commandline = 1 if ( $retcode & 0x0100 );
# my $e_devopen = 1 if ( $retcode & 0x0200 );
# my $e_chksum = 1 if ( $retcode & 0x0400 );
# my $e_disk_failing = 1 if ( $retcode & 0x0800 );
# my $e_prefail = 1 if ( $retcode & 0x1000 );
# my $e_mayprefail = 1 if ( $retcode & 0x2000 );
# my $e_errlog = 1 if ( $retcode & 0x4000 );
# my $e_selftestlog = 1 if ( $retcode & 0x8000 );
# get return code
my $status = 0;
if ( $retcode == -1 ) {
warning("failed to execute: $ERRNO");
}
elsif ( $retcode & 127 ) {
warning('child died with signal '
. ( $retcode & 127 )
. $SPACE
. ( $retcode & 128 ) ? 'with' : 'without' . 'coredump' );
}
else {
$status = $retcode >> 8;
debug("child exited with value $status");
}
return $status;
}
###############################################################################
my %config;
read_file_cached( 'config', \%config );
my $smartctl_bin = $config{'smartctl'};
my $smartctl_opt = $config{'smartctl_options'};
my $param = 'vol';
my $vol_num = 1;
my %devices; # data cache
while ( exists $config{ $param . $vol_num } ) {
my @tab = split /,/, $config{ $param . $vol_num };
my $real_vol_num = $tab[0];
my $vol = $tab[1];
my $device = $tab[2];
my $runstop = $tab[4];
#debug( "volume = $vol");
if ( is_param_runstop($runstop) ) {
my ( $trunk_device, $parm ) = split /_/, $vol, 2;
my $value = 'U';
if ( $parm eq 'status' ) {
$value = get_smart_status( $smartctl_bin, $smartctl_opt, $device );
}
else {
my %data;
# only one query for a device
if ( exists $devices{$trunk_device} ) {
#already exists in cache
%data = %{ $devices{$trunk_device} };
}
else {
# new device
%data =
get_smart_param( $smartctl_bin, $smartctl_opt, $device );
%{ $devices{$trunk_device} } = %data;
}
if ( exists $data{$parm} ) {
$value = $data{$parm};
}
else {
warning("can not find $parm on $device");
}
}
debug("smart $device $parm ($real_vol_num) : value=$value");
my $rrdbase = $real_vol_num . '.rrd';
RRDs::update( $rrdbase, "N:$value" );
my $ERR = RRDs::error();
warning("ERROR while updating $rrdbase: $ERR") if $ERR;
}
else {
debug("aquisition for $vol stopped");
}
$vol_num++;
} # while
# a label to quit
#I can not use return when executing this script standalone,
# so I had to use goto
FIN: