/[sysadmin-cookbook]/recepies/smart/smart-test-relocate.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /recepies/smart/smart-test-relocate.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 288 by dpavlin, Wed Jan 25 17:05:33 2012 UTC revision 304 by dpavlin, Sun Nov 24 21:03:59 2013 UTC
# Line 9  my $rewrite_sectors = 16; Line 9  my $rewrite_sectors = 16;
9    
10  my $test_started = 0;  my $test_started = 0;
11    
12    sub write_sector {
13            my $s = shift;
14            system "hdparm --write-sector $s --yes-i-know-what-i-am-doing $drive";
15    }
16    
17  sub smart_test {  sub smart_test {
18          my $sector = shift;          my $sector = shift;
19          my $cmd = "smartctl -t select,$sector-max $drive";          my $cmd = "smartctl -t select,$sector-max $drive";
# Line 28  sub smart { Line 33  sub smart {
33                          my $sector = $1;                          my $sector = $1;
34                          print "rewrite sector: $sector\n";                          print "rewrite sector: $sector\n";
35                          foreach my $s ( $sector .. $sector + $rewrite_sectors ) {                          foreach my $s ( $sector .. $sector + $rewrite_sectors ) {
36                                  system "hdparm --write-sector $s --yes-i-know-what-i-am-doing $drive";                                  write_sector $s;
37                          }                          }
38                          smart_test $sector;                          smart_test $sector;
39                          return 1;                          return 1;
# Line 36  sub smart { Line 41  sub smart {
41                          $test_started = 1;                          $test_started = 1;
42                          return 1;                          return 1;
43                  } elsif ( m/Not_testing/ ) {                  } elsif ( m/Not_testing/ ) {
44                          return 1 if $test_started;                          if ( $test_started ) {
45                                    smart_last_error();
46                                    return 1 if $test_started;
47                            }
48                          smart_test 0; # first-time invocation                          smart_test 0; # first-time invocation
49                  }                  }
50          }          }
51          return 0;          return 0;
52  }  }
53    
54    sub smart_last_error {
55            my $cmd = "smartctl -l selftest $drive";
56            warn "$cmd\n";
57            open(my $fh, '-|', $cmd);
58            while(<$fh>) {
59                    chomp;
60                    print "# $_\n";
61                    if (/^#\s+1.+Completed: read failure\s+\S+\s+\S+\s+(\d+)/) {
62                            write_sector $1;
63                            smart_test $1;
64                            return;
65                    }
66            }
67    }
68    
69    smart_last_error;
70    
71  while ( smart ) {  while ( smart ) {
72          warn "sleep $delay s", ( $test_started ? " smart test running..." : "idle" ), "\n";          warn "sleep $delay s", ( $test_started ? " smart test running..." : "idle" ), "\n";
73          sleep $delay;          sleep $delay;

Legend:
Removed from v.288  
changed lines
  Added in v.304

  ViewVC Help
Powered by ViewVC 1.1.26