Subversion Repositories sysadmin_scripts

Rev

Rev 64 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
64 rodolico 1
#! /usr/bin/env perl
2
 
3
use warnings;
4
use strict;
5
 
6
use Data::Dumper;
7
 
8
my %allDrives;
9
 
10
# create a hash with the keys having values ada0, da0, etc
11
# comment/uncomment one of the three lines below
12
 
13
# manually enter the drives to look at
14
# %allDrives = map { $_ => 0 } qw/ada0 da0 da1 da2 da3 da4 da5 da6 da7 da8 da9/;
15
# automagically find the drives on a BSD system
16
# %allDrives = map { &trim($_) => 0 } `geom disk list | grep 'Geom name:' | cut -d':' -f 2`;
17
# automagically find the drives on a Linux system
18
#%allDrives = map { &trim($_) => 0 } `lsblk | grep disk | cut -d' ' -f1`;
19
 
20
# if getSmart.cfg exists, read it
21
# note, you can create this with smartctl --scan > getSmart.cfg, then edit
22
# to remove drives you don't want.
23
if -e 'getSmart.cfg' {
24
   %allDrives = read getSmart.cfg
25
} else {
26
   %allDrives = map { &trim($_) => 0 } `smartctl --scan`;
27
}
28
 
29
sub trim {
30
 my $value = shift;
31
 chomp $value;
32
 if ( $value =~ m/^([^#]*)/ ) {
33
   $value = $1;
34
 }
35
 $value =~ s/^\s+|\s+$//g;
36
 return $value;
37
}
38
 
39
sub getSmartReport {
40
   my $drive = shift;
41
   my @report = `smartctl -a /dev/$drive`;
42
   return @report;
43
}
44
 
45
 
46
sub getTotalWrites {
47
 my @report = @_;
48
 return -2 unless grep{ /(Solid State Device)|(SSD)/ } @report;
49
 my @temp = grep{ /^Sector Size:/ } @report;
50
 my $sectors = shift @temp;
51
#   print "The Value is [$sectors]\n"; die;
52
 if ( $sectors =~ m/^Sector Size\:\s+(\d+)\s+bytes/ ) {
53
    $sectors = $1;
54
    # print "Sectors is $sectors\n"; die;
55
    @temp =  grep{ /^241/ } @report;
56
    my $lbas = $temp[0];
57
    if ( $lbas =~ m/(\d+)\s*$/ ) {
58
       $lbas = $1;
59
       return $lbas * $sectors;
60
    } else {
61
       return -3;
62
    }
63
 } else { 
64
    return -4; 
65
 }
66
 return -1; # we could not find something
67
}
68
 
69
sub getInformation {
70
   my @report = @_;
71
   my %info;
72
   my %keys = ( 
73
                  'Model Family:'  => { 
74
                                          'tag' => 'Make',
75
                                          'regex' => '(.*)'
76
                                      },
77
                  'Device Model:'  => { 
78
                                          'tag' => 'Model',
79
                                          'regex' => '(.*)'
80
                                      },
81
                  'Serial Number:' => { 
82
                                          'tag' => 'Serial',
83
                                          'regex' => '(.*)'
84
                                      },
85
                  'User Capacity:' => { 
86
                                          'tag' => 'Capacity',
87
                                          'regex' => '([0-9,]+)'
88
                                      },
89
                  'Sector Size:'   => { 
90
                                          'tag' => 'Sector',
91
                                          'regex' => '(\d+)'
92
                                      },
93
                  'Rotation Rate:' => { 
94
                                          'tag' => 'Rotation',
95
                                          'regex' => '(.*)'
96
                                      },
97
                  'Power_On_Hours' => {
98
                                           'tag' => 'Hours',
99
                                           'regex' => ' (\d+)\s*$'
100
                                      },
101
                  'Error Count:' => {
102
                                           'tag' => 'Errors',
103
                                           'regex' => 'Error Count: (\d+)'
104
                                      },
105
 
106
               );
107
   foreach my $key ( keys %keys ) {
108
      my @temp = grep { /^$key/ } @report;
109
      if ( @temp ) {
110
         $temp[0] =~ m/^$key\s+(.*)$/;
111
         my $value = $1;
112
         $value =~ m/$keys{$key}->{'regex'}/;
113
         $info{$keys{$key}->{'tag'}} = $1;
114
      }
115
   }
116
   # remove comma's from capacity
117
   $info{'Capacity'} =~ s/,//g if $info{'Capacity'};
118
   return \%info;
119
}
120
 
121
# output the drives and information as tab delimited,
122
foreach my $thisDrive ( sort keys %allDrives ) {
123
 print "$thisDrive\n";
124
 my @report = &getSmartReport( $thisDrive );
125
 my $writes = &getTotalWrites( @report );
126
 my $info = &getInformation( @report );
127
 $info->{'writes'} = $writes if $writes > 0;
128
 print Dumper( $info );
129
 #print "$thisDrive\t" . $allDrives{$thisDrive} . "\n" if $allDrives{$thisDrive} > 0;
130
}
131