SMART.pm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package Disk::SMART;
  2. use warnings;
  3. use strict;
  4. use Carp;
  5. use Math::Round;
  6. {
  7. $Disk::SMART::VERSION = '0.08'
  8. }
  9. our $smartctl = '/usr/sbin/smartctl';
  10. =head1 NAME
  11. Disk::SMART - Provides an interface to smartctl to return disk stats and to run tests.
  12. =head1 SYNOPSIS
  13. Disk::SMART is an object oriented module that provides an interface to get SMART disk info from a device as well as initiate testing.
  14. use Disk::SMART;
  15. my $smart = Disk::SMART->new('/dev/sda');
  16. =cut
  17. =head1 CONSTRUCTOR
  18. =head2 B<new(DEVICE)>
  19. Instantiates the Disk::SMART object
  20. C<DEVICE> - Device identifier of SSD / Hard Drive. The constructor takes either a single device name, or an array of device names.
  21. my $smart = Disk::SMART->new( 'dev/sda', '/dev/sdb' );
  22. Returns C<Disk::SMART> object if smartctl is available and can poll the given device(s).
  23. =cut
  24. sub new {
  25. my ( $class, @devices ) = @_;
  26. my $self = bless {}, $class;
  27. croak "Valid device identifier not supplied to constructor for $class.\n"
  28. if !@devices && !defined $ENV{'MOCK_TEST_DATA'};
  29. croak "smartctl binary was not found on your system, are you running as root?\n"
  30. if !-f $smartctl && !defined $ENV{'MOCK_TEST_DATA'};
  31. $self->update_data($_) foreach @devices;
  32. return $self;
  33. }
  34. =head1 USER METHODS
  35. =head2 B<get_disk_attributes(DEVICE)>
  36. Returns hash of the SMART disk attributes and values
  37. C<DEVICE> - Device identifier of SSD/ Hard Drive
  38. my %disk_attributes = $smart->get_disk_attributes('/dev/sda');
  39. =cut
  40. sub get_disk_attributes {
  41. my ( $self, $device ) = @_;
  42. $self->_validate_param($device);
  43. return $self->{'devices'}->{$device}->{'attributes'};
  44. }
  45. =head2 B<get_disk_errors(DEVICE)>
  46. Returns scalar of any listed errors
  47. C<DEVICE> - Device identifier of SSD/ Hard Drive
  48. my $disk_errors = $smart->get_disk_errors('/dev/sda');
  49. =cut
  50. sub get_disk_errors {
  51. my ( $self, $device ) = @_;
  52. $self->_validate_param($device);
  53. return $self->{'devices'}->{$device}->{'errors'};
  54. }
  55. =head2 B<get_disk_health(DEVICE)>
  56. Returns the health of the disk. Output is "PASSED", "FAILED", or "N/A".
  57. C<DEVICE> - Device identifier of SSD / Hard Drive
  58. my $disk_health = $smart->get_disk_health('/dev/sda');
  59. =cut
  60. sub get_disk_health {
  61. my ( $self, $device ) = @_;
  62. $self->_validate_param($device);
  63. return $self->{'devices'}->{$device}->{'health'};
  64. }
  65. =head2 B<get_disk_model(DEVICE)>
  66. Returns the model of the device. eg. "ST3250410AS".
  67. C<DEVICE> - Device identifier of SSD / Hard Drive
  68. my $disk_model = $smart->get_disk_model('/dev/sda');
  69. =cut
  70. sub get_disk_model {
  71. my ( $self, $device ) = @_;
  72. $self->_validate_param($device);
  73. return $self->{'devices'}->{$device}->{'model'};
  74. }
  75. =head2 B<get_disk_temp(DEVICE)>
  76. Returns an array with the temperature of the device in Celsius and Farenheit, or N/A.
  77. C<DEVICE> - Device identifier of SSD / Hard Drive
  78. my ($temp_c, $temp_f) = $smart->get_disk_temp('/dev/sda');
  79. =cut
  80. sub get_disk_temp {
  81. my ( $self, $device ) = @_;
  82. $self->_validate_param($device);
  83. return @{ $self->{'devices'}->{$device}->{'temp'} };
  84. }
  85. =head2 B<update_data>
  86. Updates the SMART output and attributes of a device. Returns undef.
  87. C<DEVICE> - Device identifier of SSD/ Hard Drive
  88. $smart->update_data('/dev/sda');
  89. =cut
  90. sub update_data {
  91. my ( $self, $device ) = @_;
  92. my $out;
  93. $out = $ENV{'MOCK_TEST_DATA'} if ( defined $ENV{'MOCK_TEST_DATA'} );
  94. $out = qx($smartctl -a $device) if -f $smartctl;
  95. croak "Smartctl couldn't poll device $device\n"
  96. if ( !$out || $out !~ /START OF INFORMATION SECTION/ );
  97. chomp($out);
  98. $self->{'devices'}->{$device}->{'SMART_OUTPUT'} = $out;
  99. $self->_process_disk_attributes($device);
  100. $self->_process_disk_errors($device);
  101. $self->_process_disk_health($device);
  102. $self->_process_disk_model($device);
  103. $self->_process_disk_temp($device);
  104. return 1;
  105. }
  106. =head2 B<run_short_test>
  107. Runs the SMART short self test and returns the result.
  108. C<DEVICE> - Device identifier of SSD/ Hard Drive
  109. $smart->run_short_test('/dev/sda');
  110. =cut
  111. sub run_short_test {
  112. my ( $self, $device ) = @_;
  113. $self->_validate_param($device);
  114. if ( !defined $ENV{'MOCK_TEST_DATA'} ) {
  115. my $out = qx( $smartctl -t short $device );
  116. my ($short_test_time) = $out =~ /Please wait (.*) minutes/s;
  117. sleep( $short_test_time * 60 );
  118. }
  119. my $smart_output = ( defined $ENV{'MOCK_TEST_DATA'} ) ? $ENV{'MOCK_TEST_DATA'} : qx($smartctl -a $device);
  120. ($smart_output) = $smart_output =~ /(SMART Self-test log.*)\nSMART Selective self-test/s;
  121. my @device_tests = split /\n/, $smart_output;
  122. my $short_test_number = $device_tests[2];
  123. my $short_test_status = substr $short_test_number, 25, +30;
  124. $short_test_status =~ s/\s+$//g; #trim beginning and ending whitepace
  125. return $short_test_status;
  126. }
  127. ##INTERNAL FUNCTIONS BELOW. THESE SHOULD NEVER BE CALLED BY A SCRIPT##
  128. sub _process_disk_attributes {
  129. my ( $self, $device ) = @_;
  130. $self->_validate_param($device);
  131. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  132. my ($smart_attributes) = $smart_output =~ /(ID# ATTRIBUTE_NAME.*)\nSMART Error/s;
  133. my @attributes = split /\n/, $smart_attributes;
  134. shift @attributes;
  135. foreach my $attribute (@attributes) {
  136. my $name = substr $attribute, 4, +24;
  137. my $value = substr $attribute, 83, +50;
  138. $name =~ s/\s+$//g; # trim ending whitespace
  139. $value =~ s/^\s+//g; # trim beginning and ending whitepace
  140. $self->{'devices'}->{$device}->{'attributes'}->{$name} = $value;
  141. }
  142. return;
  143. }
  144. sub _process_disk_errors {
  145. my ( $self, $device ) = @_;
  146. $self->_validate_param($device);
  147. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  148. my ($errors) = $smart_output =~ /SMART Error Log Version: [1-9](.*)SMART Self-test log/s;
  149. $errors =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  150. $errors = 'N/A' if !$errors;
  151. return $self->{'devices'}->{$device}->{'errors'} = $errors;
  152. }
  153. sub _process_disk_health {
  154. my ( $self, $device ) = @_;
  155. $self->_validate_param($device);
  156. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  157. my ($health) = $smart_output =~ /SMART overall-health self-assessment test result:(.*)\n/;
  158. $health =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  159. $health = 'N/A' if !$health || $health !~ /PASSED|FAILED/x;
  160. return $self->{'devices'}->{$device}->{'health'} = $health;
  161. }
  162. sub _process_disk_model {
  163. my ( $self, $device ) = @_;
  164. $self->_validate_param($device);
  165. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  166. my ($model) = $smart_output =~ /Device\ Model:(.*)\n/;
  167. $model =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  168. $model = 'N/A' if !$model;
  169. return $self->{'devices'}->{$device}->{'model'} = $model;
  170. }
  171. sub _process_disk_temp {
  172. my ( $self, $device ) = @_;
  173. $self->_validate_param($device);
  174. my ( $temp_c, $temp_f );
  175. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  176. ($temp_c) = $smart_output =~ /(Temperature_Celsius.*\n)/;
  177. if ($temp_c) {
  178. $temp_c = substr $temp_c, 83, +3;
  179. $temp_c =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  180. $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
  181. $temp_c = int $temp_c;
  182. $temp_f = int $temp_f;
  183. }
  184. else {
  185. $temp_c = 'N/A';
  186. $temp_f = 'N/A';
  187. }
  188. return $self->{'devices'}->{$device}->{'temp'} = [ ( $temp_c, $temp_f ) ];
  189. }
  190. sub _validate_param {
  191. my ( $self, $device ) = @_;
  192. croak "$device not found in object. Verify you specified the right device identifier.\n" if ( !exists $self->{'devices'}->{$device} );
  193. return;
  194. }
  195. 1;
  196. __END__
  197. =head1 AUTHOR
  198. Paul Trost <ptrost@cpan.org>
  199. =head1 LICENSE AND COPYRIGHT
  200. Copyright 2014 by Paul Trost
  201. 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.
  202. <http://gnu.org/licenses/gpl.html>
  203. =cut