ContributedUtilities: HElog_smart.pl

File HElog_smart.pl, 1.9 KB (added by Gabriele Pohl, 13 years ago)

Perl script that appends output of smartctl -a to file name built from Model.Serial.log

Line 
1#!/usr/bin/perl -w
2# HElog_smart: Write smartctl -a to file name built from Model.Serial.log HE: 18.2.2011
3# Copyright c 2010 according to GPL v.2 by H.Ellenberger, CH-3360 Herzogenbuchsee.
4
5use strict;
6use POSIX qw(strftime);
7
8my $log_dir = "/root/SMART"; # Files are created there
9
10sub trm { # Replace spaces, remove \n
11 my $txt = shift; $txt =~ s/ /_/g; $txt =~ s/\n//g;
12 return $txt
13}
14
15sub ck_one{ # Check one drive given by @_
16 my $cmd = "smartctl -a @_";
17 my @res = `$cmd`;
18 my $stat = $?; $cmd="'$cmd' -> ";
19 if ($stat) {print "$cmd ERROR= $stat\n"; return;}
20 my $fnam;
21 foreach (@res) {
22 my $line = $_; # Split lines returned by smartctl
23 if ($line =~ s/Device Model: //){
24 $fnam = trm $line; # Extract Model
25 }elsif ($line =~ s/Serial Number: //){
26 $line = trm $line; # Extract Serial no.
27 $fnam = "$log_dir/$fnam.$line.log"; # Build path/name
28 }
29 }
30 my $dat = strftime("%d.%m.%Y-%H:%M",localtime(time()));
31 my $health = `smartctl -H @_`; # Get health info
32 $stat = $?;
33
34 my $FIL; open($FIL, ">>", $fnam) or die "Error creating $fnam\n"; # Open result file and append
35 print $FIL "**** $dat ***********************************************************\n";
36 if ($stat) {
37 print $FIL "Error $stat checking health of @_\n";
38 $health = "DEFECTIVE";
39 } else {
40 if ($health =~ /PASSED/) {$health = "Health OK" }
41 else {$health = "ERROR: $health"; }
42 }
43 print $FIL "$health\n";
44 print $FIL @res;
45 close $FIL;
46 print "$cmd $health\n"; # Show progress
47}
48
49print "$0 v=18.2.2011 \n";
50my @res = `ls -l /dev/disk/by-id/scsi-*`; # Get only SCSI/SATA
51my $stat = $?;
52if ($stat){print "ERROR $stat: $!\n"; exit 1;}
53foreach (@res){
54 my $line = $_;
55 if (! ($line =~ /part/)){ # Skip those of partitions
56 if ($line =~ /([^->])-> ..\/..\/(.+)/ ) { # Get drive only like 'sda'
57 ck_one "/dev/$2";
58 }
59 }
60}
61print "Done.\n";
62# --- eof ---