SMART.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package Disk::SMART;
  2. use warnings;
  3. use strict;
  4. use 5.010;
  5. use Carp;
  6. use Math::Round;
  7. use File::Which;
  8. {
  9. $Disk::SMART::VERSION = '0.13'
  10. }
  11. our $smartctl = which('smartctl');
  12. =head1 NAME
  13. Disk::SMART - Provides an interface to smartctl to return disk stats and to run tests.
  14. =head1 SYNOPSIS
  15. Disk::SMART is an object oriented module that provides an interface to get SMART disk info from a device as well as initiate testing.
  16. use Disk::SMART;
  17. my $smart = Disk::SMART->new('/dev/sda', '/dev/sdb');
  18. my $disk_health = $smart->get_disk_health('/dev/sda');
  19. =cut
  20. =head1 CONSTRUCTOR
  21. =head2 B<new(DEVICE)>
  22. Instantiates the Disk::SMART object
  23. C<DEVICE> - Device identifier of a single SSD / Hard Drive, or a list. If no devices are supplied then it runs get_disk_list() which will return an array of detected sdX and hdX devices.
  24. my $smart = Disk::SMART->new();
  25. my $smart = Disk::SMART->new( '/dev/sda', '/dev/sdb' );
  26. my @disks = $smart->get_disk_list();
  27. Returns C<Disk::SMART> object if smartctl is available and can poll the given device(s).
  28. =cut
  29. sub new {
  30. my ( $class, @devices ) = @_;
  31. my $self = bless {}, $class;
  32. @devices = @devices ? @devices : $self->get_disk_list();
  33. croak "Valid device identifier not supplied to constructor for $class.\n"
  34. if !@devices;
  35. croak "smartctl binary was not found on your system, are you running as root?\n"
  36. if !-f $smartctl;
  37. $self->update_data(@devices);
  38. return $self;
  39. }
  40. =head1 USER METHODS
  41. =head2 B<get_disk_attributes(DEVICE)>
  42. Returns hash of the SMART disk attributes and values
  43. C<DEVICE> - Device identifier of a single SSD / Hard Drive
  44. my %disk_attributes = $smart->get_disk_attributes('/dev/sda');
  45. =cut
  46. sub get_disk_attributes {
  47. my ( $self, $device ) = @_;
  48. $self->_validate_param($device);
  49. return %{ $self->{'devices'}->{$device}->{'attributes'} };
  50. }
  51. =head2 B<get_disk_errors(DEVICE)>
  52. Returns scalar of any listed errors
  53. C<DEVICE> - Device identifier of a single SSD/ Hard Drive
  54. my $disk_errors = $smart->get_disk_errors('/dev/sda');
  55. =cut
  56. sub get_disk_errors {
  57. my ( $self, $device ) = @_;
  58. $self->_validate_param($device);
  59. return $self->{'devices'}->{$device}->{'errors'};
  60. }
  61. =head2 B<get_disk_health(DEVICE)>
  62. Returns the health of the disk. Output is "PASSED", "FAILED", or "N/A". If the device has positive values for the attributes listed below then the status will output that information.
  63. Eg. "FAILED - Reported_Uncorrectable_Errors = 1"
  64. The attributes are:
  65. 5 - Reallocated_Sector_Count
  66. 187 - Reported_Uncorrectable_Errors
  67. 188 - Command_Timeout
  68. 197 - Current_Pending_Sector_Count
  69. 198 - Offline_Uncorrectable
  70. If Reported_Uncorrectable_Errors is greater than 0 then the drive should be replaced immediately. This list is taken from a study shown at https://www.backblaze.com/blog/hard-drive-smart-stats/
  71. C<DEVICE> - Device identifier of a single SSD / Hard Drive
  72. my $disk_health = $smart->get_disk_health('/dev/sda');
  73. =cut
  74. sub get_disk_health {
  75. my ( $self, $device ) = @_;
  76. $self->_validate_param($device);
  77. my $status = $self->{'devices'}->{$device}->{'health'};
  78. my %failure_attribute_hash;
  79. while ( my ($key, $value) = each %{ $self->{'devices'}->{$device}->{'attributes'} } ) {
  80. if ( $key =~ /\A5\Z|\A187\Z|\A188\Z|\A197\Z|\A198\Z/ ) {
  81. $failure_attribute_hash{$key} = $value;
  82. $status .= ": $key - $value->[0] = $value->[1]" if ( $value->[1] > 0 );
  83. }
  84. }
  85. return $status;
  86. }
  87. =head2 B<get_disk_list>
  88. Returns list of detected hda and sda devices. This method can be called manually if unsure what devices are present.
  89. $smart->get_disk_list;
  90. =cut
  91. sub get_disk_list {
  92. open my $fh, '-|', 'parted -l' or confess "Can't run parted binary\n";
  93. local $/ = undef;
  94. my @disks = map { /Disk (\/.*\/[h|s]d[a-z]):/ } split /\n/, <$fh>;
  95. close $fh or confess "Can't close file handle reading parted output\n";
  96. return @disks;
  97. }
  98. =head2 B<get_disk_model(DEVICE)>
  99. Returns the model of the device. eg. "ST3250410AS".
  100. C<DEVICE> - Device identifier of a single SSD / Hard Drive
  101. my $disk_model = $smart->get_disk_model('/dev/sda');
  102. =cut
  103. sub get_disk_model {
  104. my ( $self, $device ) = @_;
  105. $self->_validate_param($device);
  106. return $self->{'devices'}->{$device}->{'model'};
  107. }
  108. =head2 B<get_disk_temp(DEVICE)>
  109. Returns an array with the temperature of the device in Celsius and Farenheit, or N/A.
  110. C<DEVICE> - Device identifier of a single SSD / Hard Drive
  111. my ($temp_c, $temp_f) = $smart->get_disk_temp('/dev/sda');
  112. =cut
  113. sub get_disk_temp {
  114. my ( $self, $device ) = @_;
  115. $self->_validate_param($device);
  116. return @{ $self->{'devices'}->{$device}->{'temp'} };
  117. }
  118. =head2 B<run_short_test(DEVICE)>
  119. Runs the SMART short self test and returns the result.
  120. C<DEVICE> - Device identifier of SSD/ Hard Drive
  121. $smart->run_short_test('/dev/sda');
  122. =cut
  123. sub run_short_test {
  124. my ( $self, $device ) = @_;
  125. $self->_validate_param($device);
  126. my $test_out = get_smart_output( $device, '-t short' );
  127. my ($short_test_time) = $test_out =~ /Please wait (.*) minutes/s;
  128. sleep( $short_test_time * 60 );
  129. my $smart_output = _get_smart_output( $device, '-a' );
  130. ($smart_output) = $smart_output =~ /(SMART Self-test log.*)\nSMART Selective self-test/s;
  131. my @device_tests = split /\n/, $smart_output;
  132. my $short_test_number = $device_tests[2];
  133. my $short_test_status = substr $short_test_number, 25, +30;
  134. $short_test_status = _trim($short_test_status);
  135. return $short_test_status;
  136. }
  137. =head2 B<update_data(DEVICE)>
  138. Updates the SMART output and attributes for each device. Returns undef.
  139. C<DEVICE> - Device identifier of a single SSD / Hard Drive or a list of devices. If none are specified then get_disk_list() is called to detect devices.
  140. $smart->update_data('/dev/sda');
  141. =cut
  142. sub update_data {
  143. my ( $self, @p_devices ) = @_;
  144. my @devices = @p_devices ? @p_devices : $self->get_disk_list();
  145. foreach my $device (@devices) {
  146. my $out;
  147. $out = _get_smart_output( $device, '-a' );
  148. confess "Smartctl couldn't poll device $device\nSmartctl Output:\n$out\n"
  149. if ( !$out || $out !~ /START OF INFORMATION SECTION/ );
  150. chomp($out);
  151. $self->{'devices'}->{$device}->{'SMART_OUTPUT'} = $out;
  152. $self->_process_disk_attributes($device);
  153. $self->_process_disk_errors($device);
  154. $self->_process_disk_health($device);
  155. $self->_process_disk_model($device);
  156. $self->_process_disk_temp($device);
  157. }
  158. return;
  159. }
  160. sub _get_smart_output {
  161. my ( $device, $options ) = @_;
  162. $options = $options // '';
  163. open my $fh, '-|', "$smartctl $device $options" or confess "Can't run smartctl binary\n";
  164. local $/ = undef;
  165. my $smart_output = <$fh>;
  166. #close $fh or confess "Can't close file handle reading smartctl output\n";
  167. if ( $smart_output =~ /Unknown USB bridge/ ) {
  168. open $fh, '-|', "$smartctl $device $options -d sat" or confess "Can't run smartctl binary\n";
  169. $smart_output = <$fh>;
  170. close $fh or confess "Can't close file handle reading smartctl output\n";
  171. }
  172. return $smart_output;
  173. }
  174. sub _process_disk_attributes {
  175. my ( $self, $device ) = @_;
  176. $self->_validate_param($device);
  177. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  178. my ($smart_attributes) = $smart_output =~ /(ID# ATTRIBUTE_NAME.*)\nSMART Error/s;
  179. my @attributes = split /\n/, $smart_attributes;
  180. shift @attributes; #remove table header
  181. foreach my $attribute (@attributes) {
  182. my $id = substr $attribute, 0, +3;
  183. my $name = substr $attribute, 4, +24;
  184. my $value = substr $attribute, 83, +50;
  185. $id = _trim($id);
  186. $name = _trim($name);
  187. $value = _trim($value);
  188. $self->{'devices'}->{$device}->{'attributes'}->{$id} = [ $name, $value ];
  189. }
  190. return;
  191. }
  192. sub _process_disk_errors {
  193. my ( $self, $device ) = @_;
  194. $self->_validate_param($device);
  195. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  196. my ($errors) = $smart_output =~ /SMART Error Log Version: [1-9](.*)SMART Self-test log/s;
  197. $errors = _trim($errors);
  198. $errors = 'N/A' if !$errors;
  199. return $self->{'devices'}->{$device}->{'errors'} = $errors;
  200. }
  201. sub _process_disk_health {
  202. my ( $self, $device ) = @_;
  203. $self->_validate_param($device);
  204. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  205. my ($health) = $smart_output =~ /SMART overall-health self-assessment test result:(.*)\n/;
  206. $health = _trim($health);
  207. $health = 'N/A' if !$health || $health !~ /PASSED|FAILED/x;
  208. return $self->{'devices'}->{$device}->{'health'} = $health;
  209. }
  210. sub _process_disk_model {
  211. my ( $self, $device ) = @_;
  212. $self->_validate_param($device);
  213. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  214. my ($model) = $smart_output =~ /Device\ Model:(.*)\n/;
  215. $model = _trim($model);
  216. $model = 'N/A' if !$model;
  217. return $self->{'devices'}->{$device}->{'model'} = $model;
  218. }
  219. sub _process_disk_temp {
  220. my ( $self, $device ) = @_;
  221. $self->_validate_param($device);
  222. my ( $temp_c, $temp_f );
  223. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  224. ($temp_c) = $smart_output =~ /(Temperature_Celsius.*\n|Airflow_Temperature_Cel.*\n)/;
  225. if ($temp_c) {
  226. $temp_c = substr $temp_c, 83, +3;
  227. $temp_c = _trim($temp_c);
  228. $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
  229. $temp_c = int $temp_c;
  230. $temp_f = int $temp_f;
  231. }
  232. else {
  233. $temp_c = 'N/A';
  234. $temp_f = 'N/A';
  235. }
  236. return $self->{'devices'}->{$device}->{'temp'} = [ ( $temp_c, $temp_f ) ];
  237. }
  238. sub _trim {
  239. my $string = shift;
  240. $string =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  241. return $string;
  242. }
  243. sub _validate_param {
  244. my ( $self, $device ) = @_;
  245. croak "$device not found in object. Verify you specified the right device identifier.\n"
  246. if ( !exists $self->{'devices'}->{$device} );
  247. return;
  248. }
  249. 1;
  250. __END__
  251. =head1 COMPATIBILITY
  252. This module should run on any UNIX like OS with Perl 5.10+ and the smartctl progam installed from the smartmontools package.
  253. =head1 AUTHOR
  254. Paul Trost <ptrost@cpan.org>
  255. =head1 LICENSE AND COPYRIGHT
  256. Copyright 2015 by Paul Trost
  257. This script is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License v2, or at your option any later version.
  258. <http://gnu.org/licenses/gpl.html>
  259. =cut