SMART.pm 9.9 KB

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