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;
165 rodolico 3
use strict;
4
use Data::Dumper;
152 rodolico 5
 
169 rodolico 6
# Description: Use smartctl, lsblk and geom to get disk information
7
# will decode lsblk first. Then use geom to get information on BSD systems
8
# finally, smartctl will be used to gather additional information.
152 rodolico 9
 
169 rodolico 10
# 20200224 RWR v2.1
11
# large re-organization of code and inclusion to get basic information from lsblk if it is on the system
152 rodolico 12
 
169 rodolico 13
our $VERSION = '2.1';
14
 
152 rodolico 15
# R. W. Rodolico
16
# grabs smart readings from all drives in system
157 rodolico 17
# note that in the case of some hardware RAID controller, will list the components of a "drive" and the drive itself,
18
# so you may have up to twice the number of entries than you have physical drives, for example, if you have a megaraid
19
# controller where each physical drive is also a single logical drive.
152 rodolico 20
 
165 rodolico 21
# find our location and use it for searching for libraries
152 rodolico 22
BEGIN {
165 rodolico 23
   use FindBin;
24
   use File::Spec;
25
   use lib File::Spec->catdir($FindBin::Bin);
26
   use library;
152 rodolico 27
}
28
 
165 rodolico 29
exit 0 unless checkDate( 'm' ); # run this only on first of month
152 rodolico 30
 
169 rodolico 31
my %driveDefinitions; # this will be a global that everything will put info into
32
 
165 rodolico 33
my %ignoreDriveTypes = ( # a list of drive "types" used by virtuals that we just ignore.
34
         'VBOX HARDDISK' => 1,
35
         );
152 rodolico 36
 
37
 
169 rodolico 38
# acquires information using lsblk, if it is available
39
# uses global %driveDefinitions to store the results
40
 
41
sub lsblk {
42
   eval ( 'use JSON qw( decode_json );' );
43
   if ( $@ ) {
44
      warn "Could not load JSON library\n";    
45
      return;
46
   }
47
 
48
   my $output = qx'lsblk -bdJO 2>/dev/null';
49
   # older versions do not have the O option, so we'll run it without
50
   $output = qx'lsblk -bdJ 2>/dev/null' if $?;
51
   my $drives = decode_json( join( '', $output ) );
52
   $drives = $drives->{'blockdevices'};
53
   while ( my $thisDrive = shift @{$drives} ) {
54
      if ( $thisDrive->{'type'} eq 'disk' ) {
55
         my $key = '/dev/' . $thisDrive->{'name'};
56
         $driveDefinitions{$key}{'Capacity'} = $thisDrive->{'size'};
57
         $driveDefinitions{$key}{'Model'} = $thisDrive->{'model'} if defined $thisDrive->{'model'};
58
         $driveDefinitions{$key}{'Serial'} = $thisDrive->{'serial'} if defined $thisDrive->{'serial'};
59
      }
60
   }
61
}
62
 
63
 
165 rodolico 64
sub getSmartInformationReport {
157 rodolico 65
   my ($drive, $type) = @_;
165 rodolico 66
   $type = '' if ( $type =~ m/scsi/ );
67
   my @report = `smartctl -i $drive $type`;
157 rodolico 68
   chomp @report;
165 rodolico 69
   my %reportHash;
70
   for ( my $i = 0; $i < @report; $i++ ) {
71
      if ( $report[$i] =~ m/^(.*):(.*)$/ ) {
72
         $reportHash{$1} = trim($2);
73
      }
74
   }
75
   return \%reportHash;
76
} # getSmartInformationReport
77
 
78
sub getSmartAttributeReport {
79
   my ($drive, $type) = @_;
80
   $type = '' if ( $type =~ m/scsi/ );
81
   my @report = `smartctl -A $drive $type`;
82
   chomp @report;
83
   my %reportHash;
84
   my %headers;
85
   # bypass all the header information
86
   my $i;
87
   for ( $i = 0; $i < @report && $report[$i] !~ m/^ID#/; $i++ ) {}
88
   if ( $i < @report ) { # did we get an actual report? some drives will not give us one
89
      my $char = 0;
90
      while ( $char < length($report[$i]) ) {
91
         substr( $report[$i],$char ) =~ m/^([^ ]+\s*)/;
92
         my $header = $1;
93
         my $start = $char;
94
         my $length = length($header);
95
         if ( $header = &trim( $header ) ) {
96
            $headers{$header}{'start'} = $start;
97
            $headers{$header}{'length'} = $length-1;
98
         }
99
         $char += $length;
100
      }
101
      while ( ++$i < @report ) {
102
         last unless $report[$i];
103
         my $id = &trim(substr( $report[$i], $headers{'ID#'}{'start'}, $headers{'ID#'}{'length'} ));
104
         my $name = &trim(substr( $report[$i], $headers{'ATTRIBUTE_NAME'}{'start'}, $headers{'ATTRIBUTE_NAME'}{'length'} ));
105
         my $value = &trim(substr( $report[$i], $headers{'RAW_VALUE'}{'start'} ));
106
         $reportHash{$id}{'value'} = $value;
107
         $reportHash{$id}{'name'} = $name;
108
      }
109
   }
110
   #print Dumper( \%reportHash ); die;
111
   return \%reportHash;
152 rodolico 112
}
113
 
114
 
165 rodolico 115
sub getAttributes {
169 rodolico 116
   my ($drive,$type,$sectorSize) = @_;
117
 
118
   my $report = &getSmartAttributeReport( $drive, $type );
119
 
165 rodolico 120
   # first let's get total disk writes
121
   if ( defined( $report->{'241'} ) ) {
122
      $sectorSize =~ m/^(\d+)/;
169 rodolico 123
      $driveDefinitions{$drive}{'writes'} = $report->{'241'} * $sectorSize;
165 rodolico 124
   }
125
   # find total uptime
126
   if ( defined( $report->{'9'} ) ) {
127
      $report->{'9'}->{'value'} =~ m/^(\d+)/;
169 rodolico 128
      $driveDefinitions{$drive}{'uptime'} = $1;
165 rodolico 129
   }
152 rodolico 130
}
131
 
132
sub getInformation {
169 rodolico 133
   my ($drive, $type) = @_;
134
 
135
   my $report = &getSmartInformationReport( $drive, $type );
136
 
152 rodolico 137
   my %info;
138
   my %keys = ( 
165 rodolico 139
                  'Model Family'  => { 
152 rodolico 140
                                          'tag' => 'Make',
141
                                          'regex' => '(.*)'
142
                                      },
165 rodolico 143
                  'Device Model'  => { 
152 rodolico 144
                                          'tag' => 'Model',
145
                                          'regex' => '(.*)'
146
                                      },
165 rodolico 147
                  'Serial number' => { 
157 rodolico 148
                                          'tag' => 'Serial',
149
                                          'regex' => '(.*)'
150
                                      },
165 rodolico 151
                  'Serial Number' => { 
152 rodolico 152
                                          'tag' => 'Serial',
153
                                          'regex' => '(.*)'
154
                                      },
165 rodolico 155
                  'User Capacity' => { 
152 rodolico 156
                                          'tag' => 'Capacity',
157
                                          'regex' => '([0-9,]+)'
158
                                      },
165 rodolico 159
                  'Logical block size' =>{ 
157 rodolico 160
                                          'tag' => 'Sector Size',
161
                                          'regex' => '(\d+)'
162
                                      },
165 rodolico 163
                  'Sector Size'   => { 
152 rodolico 164
                                          'tag' => 'Sector Size',
165
                                          'regex' => '(\d+)'
166
                                      },
165 rodolico 167
                  'Rotation Rate' => { 
152 rodolico 168
                                          'tag' => 'Rotation',
169
                                          'regex' => '(.*)'
165 rodolico 170
                                      },
152 rodolico 171
               );
172
   foreach my $key ( keys %keys ) {
165 rodolico 173
      if ( defined( $report->{$key} ) && $report->{$key} =~ m/$keys{$key}->{'regex'}/ ) {
169 rodolico 174
         $driveDefinitions{$drive}{$keys{$key}->{'tag'}} = $1;
152 rodolico 175
      }
176
   }
177
}
178
 
169 rodolico 179
sub smartctl {
180
   # Get all the drives on the system
181
   my %allDrives = map { $_ =~ '(^[a-z0-9/]+)\s+(.*)\#'; ($1,$2) } `smartctl --scan`;
152 rodolico 182
 
169 rodolico 183
   # output the drives and information as tab delimited,
184
   foreach my $thisDrive ( sort keys %allDrives ) {
185
      $driveDefinitions{$thisDrive}{'type'} = $allDrives{$thisDrive};
186
      &getInformation( $thisDrive, $allDrives{$thisDrive} );
187
      #print Dumper( $info ); die;
188
      my $attributes = &getAttributes( $thisDrive, $allDrives{$thisDrive}, $driveDefinitions{$thisDrive}{'Sector Size'} );
189
      #print Dumper( $attributes ); die;
190
      #print Dumper( $info ); die;
191
   }
192
 
193
}
152 rodolico 194
 
169 rodolico 195
 
196
 
152 rodolico 197
# category we will use for all values found
198
# see sysinfo for a list of valid categories
157 rodolico 199
my $CATEGORY = 'attributes';
152 rodolico 200
 
201
# run the commands necessary to do whatever you want to do
202
# The library entered above has some helper routines
203
# validCommandOnSystem -- passed a name, returns the fully qualified path or '' if it does not exist
204
# cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
205
#           chomps the string (removes trailing newlines)
206
#           removes all text BEFORE the delimiter, the delimiter, and any whitespace
207
#           thus, the string 'xxI Am x  a weird string' with a newline will become
208
#           'a weird string' with no newline
209
 
210
# now, return the tab delimited output (to STDOUT). $CATEGORY is the first
211
# item, name as recognized by sysinfo is the second and the value is
212
# the last one. For multiple entries, place on separate lines (ie, newline separated)
213
 
214
# check for commands we want to run
215
my %commands = ( 
216
                  'smartctl' => '',
169 rodolico 217
                  'lsblk' => '',
218
                  'geom' => '',
152 rodolico 219
               );
220
 
221
# check the commands for validity
222
foreach my $command ( keys %commands ) {
223
   $commands{$command} = &validCommandOnSystem( $command );
224
}
225
 
226
# bail if we don't have the commands we need
169 rodolico 227
exit 1 unless $commands{'smartctl'} || $commands{'lsblk'} || $commands{'geom'};
152 rodolico 228
 
169 rodolico 229
# first, get basic information using lsblk for linux systems
230
&lsblk() if $commands{'lsblk'};
231
# now, try geom for bsd systems
232
#&geom() if $commands{'geom'};
233
# finally, populate whatever using smartctl if it is on system.
234
&smartctl() if $commands{'smartctl'};
152 rodolico 235
 
169 rodolico 236
for my $drive ( sort keys %driveDefinitions ) {
237
   # don't print iSCSI definitions
238
   next if defined( $driveDefinitions{$drive}{'Transport protocol'} ) && $driveDefinitions{$drive}{'Transport protocol'} eq 'ISCSI';
239
   #also, blow off our ignored types
240
   next if ( defined( $driveDefinitions{$drive}{'Model'} ) && defined( $ignoreDriveTypes{ $driveDefinitions{$drive}{'Model'} }  ) );
241
 
242
   # remove comma's from capacity
243
   $driveDefinitions{$drive}{'Capacity'} =~ s/,//g if $driveDefinitions{$drive}{'Capacity'};
244
   foreach my $key ( sort keys %{$driveDefinitions{$drive}} ) {
245
      print "$CATEGORY\t$drive $key\t$driveDefinitions{$drive}{$key}\n";
246
   }
152 rodolico 247
}
248
 
169 rodolico 249
 
152 rodolico 250
exit 0;