Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
152 rodolico 1
#!/usr/bin/env perl
2
use warnings;
3
use strict;  
4
 
5
# Description: Use smartctl to get disk information
6
 
7
our $VERSION = '1.0';
8
 
9
# R. W. Rodolico
10
# grabs smart readings from all drives in system
11
 
12
BEGIN {
13
   push @INC, shift;
14
}
15
 
16
use library;
17
 
18
exit 0 unless checkDate( 'w' ); # run this only on Mondays
19
 
20
sub trim {
21
 my $value = shift;
22
 $value =~ s/^\s+|\s+$//g;
23
 return $value;
24
}
25
 
26
sub getSmartReport {
27
   my $drive = shift;
28
   my @report = `smartctl -a /dev/$drive`;
29
   return @report;
30
}
31
 
32
 
33
sub getTotalWrites {
34
 my @report = @_;
35
 return -2 unless grep{ /(Solid State Device)|(SSD)/ } @report;
36
 my @temp = grep{ /^Sector Size:/ } @report;
37
 my $sectors = shift @temp;
38
#   print "The Value is [$sectors]\n"; die;
39
 if ( $sectors =~ m/^Sector Size\:\s+(\d+)\s+bytes/ ) {
40
    $sectors = $1;
41
    # print "Sectors is $sectors\n"; die;
42
    @temp =  grep{ /^241/ } @report;
43
    my $lbas = $temp[0];
44
    if ( $lbas =~ m/(\d+)\s*$/ ) {
45
       $lbas = $1;
46
       return $lbas * $sectors;
47
    } else {
48
       return -3;
49
    }
50
 } else { 
51
    return -4; 
52
 }
53
 return -1; # we could not find something
54
}
55
 
56
sub getInformation {
57
   my @report = @_;
58
   my %info;
59
   my %keys = ( 
60
                  'Model Family:'  => { 
61
                                          'tag' => 'Make',
62
                                          'regex' => '(.*)'
63
                                      },
64
                  'Device Model:'  => { 
65
                                          'tag' => 'Model',
66
                                          'regex' => '(.*)'
67
                                      },
68
                  'Serial Number:' => { 
69
                                          'tag' => 'Serial',
70
                                          'regex' => '(.*)'
71
                                      },
72
                  'User Capacity:' => { 
73
                                          'tag' => 'Capacity',
74
                                          'regex' => '([0-9,]+)'
75
                                      },
76
                  'Sector Size:'   => { 
77
                                          'tag' => 'Sector Size',
78
                                          'regex' => '(\d+)'
79
                                      },
80
                  'Rotation Rate:' => { 
81
                                          'tag' => 'Rotation',
82
                                          'regex' => '(.*)'
83
                                      }
84
               );
85
   foreach my $key ( keys %keys ) {
86
      my @temp = grep { /^$key/ } @report;
87
      if ( @temp ) {
88
         $temp[0] =~ m/^$key\s+(.*)$/;
89
         my $value = $1;
90
         $value =~ m/$keys{$key}->{'regex'}/;
91
         $info{$keys{$key}->{'tag'}} = $1;
92
      }
93
   }
94
   # remove comma's from capacity
95
   $info{'Capacity'} =~ s/,//g if $info{'Capacity'};
96
   return \%info;
97
}
98
 
99
 
100
 
101
# category we will use for all values found
102
# see sysinfo for a list of valid categories
103
my $CATEGORY = 'system';
104
 
105
# run the commands necessary to do whatever you want to do
106
# The library entered above has some helper routines
107
# validCommandOnSystem -- passed a name, returns the fully qualified path or '' if it does not exist
108
# cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
109
#           chomps the string (removes trailing newlines)
110
#           removes all text BEFORE the delimiter, the delimiter, and any whitespace
111
#           thus, the string 'xxI Am x  a weird string' with a newline will become
112
#           'a weird string' with no newline
113
 
114
# now, return the tab delimited output (to STDOUT). $CATEGORY is the first
115
# item, name as recognized by sysinfo is the second and the value is
116
# the last one. For multiple entries, place on separate lines (ie, newline separated)
117
 
118
# check for commands we want to run
119
my %commands = ( 
120
                  'smartctl' => '',
121
                  'geom'     => '',
122
                  'lsblk'    => '',
123
               );
124
 
125
# check the commands for validity
126
foreach my $command ( keys %commands ) {
127
   $commands{$command} = &validCommandOnSystem( $command );
128
}
129
 
130
$commands{'getDrives'} = $commands{ 'lsblk' } ? $commands{'lsblk'} . " | grep disk | cut -d' ' -f1" :
131
                                  ( $commands{'geom'} ? $commands{'geom'} . " disk list | grep 'Geom name:' | cut -d':' -f 2" : '' );
132
 
133
# bail if we don't have the commands we need
134
exit 1 unless $commands{'getDrives'} and $commands{'smartctl'};
135
 
136
# Get all the drives on the system
137
my %allDrives = map { &trim($_) => 0 } `$commands{'getDrives'}`;
138
 
139
 
140
# output the drives and information as tab delimited,
141
foreach my $thisDrive ( sort keys %allDrives ) {
142
 print "$thisDrive\n";
143
 my @report = &getSmartReport( $thisDrive );
144
 my $writes = &getTotalWrites( @report );
145
 my $info = &getInformation( @report );
146
 $info->{'writes'} = $writes if $writes > 0;
147
 #print Dumper( $info );
148
 foreach my $key ( keys %$info ) {
149
    print "$CATEGORY\t$thisDrive $key\t" . $info->{$key} . "\n";
150
 }
151
}
152
 
153
exit 0;