[go: up one dir, main page]

Menu

[e473bf]: / smart.pl  Maximize  Restore  History

Download this file

240 lines (194 with data), 5.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/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";
my $r_tab = read_pipe($fullcmd);
my %data;
# get parameters values
# see also smart-lib code
my $found = 0;
foreach my $line ( @{$r_tab} ) {
chomp $line;
# skip blank lines
next if ( $line eq $EMPTY );
# skip lines until find ID#
if ( $line =~ m/^ID#/ ) {
$found = 1;
next;
}
elsif ($found) {
# 194 Temperature_Celsius 0x0002 137 137 000 Old_age Always - 40 (Lifetime Min/Max 15/55)
my @tab = split_awk($line);
# first elem is a number
# second is the name
my $name = $tab[1];
if ( ( scalar @tab ) >= 10 ) {
# value can be a number
# or something like : 160h23m
if ( $tab[9] =~ m/^(\d+)/ ) {
$data{$name} = $1;
}
}
else {
debug("strange smartctl format : $line");
}
}
} # foreach
#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 -q silent ' . add_ata($device);
my $fullcmd = "$cmd $smartctl_options $device";
my @tabcmd = split_awk($fullcmd);
system @tabcmd;
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'};
if ( !$smartctl_bin ) {
warning('smartctl not configured');
goto FIN;
}
elsif ( !-x $smartctl_bin ) {
warning("smartctl command $smartctl_bin not found");
goto FIN;
}
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: