SMART.pm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package Disk::SMART;
  2. use warnings;
  3. use strict;
  4. use Carp;
  5. use Math::Round;
  6. {
  7. $Disk::SMART::VERSION = '0.06'
  8. }
  9. our $smartctl = '/usr/sbin/smartctl';
  10. =head1 NAME
  11. Disk::SMART - Provides an interface to smartctl
  12. =head1 SYNOPSIS
  13. Disk::SMART is an object ooriented 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. my $test_data;
  28. if ( $ENV{'TEST_MOCK_DATA'} ) {
  29. $test_data = pop @devices;
  30. }
  31. croak "Valid device identifier not supplied to constructor for $class.\n"
  32. if !@devices && !$ENV{'TEST_MOCK_DATA'};
  33. croak "smartctl binary was not found on your system, are you running as root?\n"
  34. if !-f $smartctl && !$ENV{'TEST_MOCK_DATA'};
  35. foreach my $device (@devices) {
  36. ($test_data) ? $self->update_data($device, $test_data) : $self->update_data($device);
  37. }
  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 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 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".
  63. C<DEVICE> - Device identifier of SSD / Hard Drive
  64. my $disk_health = $smart->get_disk_health('/dev/sda');
  65. =cut
  66. sub get_disk_health {
  67. my ( $self, $device ) = @_;
  68. $self->_validate_param($device);
  69. return $self->{'devices'}->{$device}->{'health'};
  70. }
  71. =head2 B<get_disk_model(DEVICE)>
  72. Returns the model of the device. eg. "ST3250410AS".
  73. C<DEVICE> - Device identifier of SSD / Hard Drive
  74. my $disk_model = $smart->get_disk_model('/dev/sda');
  75. =cut
  76. sub get_disk_model {
  77. my ( $self, $device ) = @_;
  78. $self->_validate_param($device);
  79. return $self->{'devices'}->{$device}->{'model'};
  80. }
  81. =head2 B<get_disk_temp(DEVICE)>
  82. Returns an array with the temperature of the device in Celsius and Farenheit, or N/A.
  83. C<DEVICE> - Device identifier of SSD / Hard Drive
  84. my ($temp_c, $temp_f) = $smart->get_disk_temp('/dev/sda');
  85. =cut
  86. sub get_disk_temp {
  87. my ( $self, $device ) = @_;
  88. $self->_validate_param($device);
  89. return @{ $self->{'devices'}->{$device}->{'temp'} };
  90. }
  91. =head2 B<update_data>
  92. Updates the SMART output and attributes of a device. Returns undef.
  93. C<DEVICE> - Device identifier of SSD/ Hard Drive
  94. $smart->update_data('/dev/sda');
  95. =cut
  96. sub update_data {
  97. my ( $self, $device, $test_data ) = @_;
  98. my $out = $test_data // undef;
  99. $out = qx($smartctl -a $device) if !defined $test_data;
  100. my $retval = $?;
  101. if ( !$test_data ) {
  102. croak "Smartctl couldn't poll device $device\n"
  103. if ( $out !~ /START OF INFORMATION SECTION/ );
  104. }
  105. chomp($out);
  106. $self->{'devices'}->{$device}->{'SMART_OUTPUT'} = $out;
  107. # update_data() can be called at any time with a device name. Let's check
  108. # the device name given to make sure it matches what was given during
  109. # object construction.
  110. $self->_validate_param($device);
  111. $self->_process_disk_attributes($device);
  112. $self->_process_disk_errors($device);
  113. $self->_process_disk_health($device);
  114. $self->_process_disk_model($device);
  115. $self->_process_disk_temp($device);
  116. return 1;
  117. }
  118. sub _process_disk_attributes {
  119. my ( $self, $device ) = @_;
  120. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  121. my ($smart_attributes) = $smart_output =~ /(ID# ATTRIBUTE_NAME.*)\nSMART Error/s;
  122. my @attributes = split /\n/, $smart_attributes;
  123. shift @attributes;
  124. foreach my $attribute (@attributes) {
  125. my $name = substr $attribute, 4, +24;
  126. my $value = substr $attribute, 83, +50;
  127. $name =~ s/\s+$//g; # trim ending whitespace
  128. $value =~ s/^\s+//g; # trim beginning and ending whitepace
  129. $self->{'devices'}->{$device}->{'attributes'}->{$name} = $value;
  130. }
  131. return;
  132. }
  133. sub _process_disk_errors {
  134. my ( $self, $device ) = @_;
  135. $self->_validate_param($device);
  136. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  137. my ($errors) = $smart_output =~ /SMART Error Log Version: [1-9](.*)SMART Self-test log/s;
  138. $errors =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  139. $errors = 'N/A' if !$errors;
  140. return $self->{'devices'}->{$device}->{'errors'} = $errors;
  141. }
  142. sub _process_disk_health {
  143. my ( $self, $device ) = @_;
  144. $self->_validate_param($device);
  145. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  146. my ($health) = $smart_output =~ /SMART overall-health self-assessment test result:(.*)\n/;
  147. $health =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  148. $health = 'N/A' if !$health or $health !~ /PASSED|FAILED/x;
  149. return $self->{'devices'}->{$device}->{'health'} = $health;
  150. }
  151. sub _process_disk_model {
  152. my ( $self, $device ) = @_;
  153. $self->_validate_param($device);
  154. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  155. my ($model) = $smart_output =~ /Device\ Model:(.*)\n/;
  156. $model =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  157. $model = 'N/A' if !$model;
  158. return $self->{'devices'}->{$device}->{'model'} = $model;
  159. }
  160. sub _process_disk_temp {
  161. my ( $self, $device ) = @_;
  162. $self->_validate_param($device);
  163. my ( $temp_c, $temp_f );
  164. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  165. ($temp_c) = $smart_output =~ /(Temperature_Celsius.*\n)/;
  166. if ($temp_c) {
  167. $temp_c = substr $temp_c, 83, +3;
  168. $temp_c =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  169. $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
  170. $temp_c = int $temp_c;
  171. $temp_f = int $temp_f;
  172. }
  173. else {
  174. $temp_c = 'N/A';
  175. $temp_f = 'N/A';
  176. }
  177. return $self->{'devices'}->{$device}->{'temp'} = [ ( $temp_c, $temp_f ) ];
  178. }
  179. sub _validate_param {
  180. my ( $self, $device ) = @_;
  181. croak "$device not found in object, you probably didn't enter it right" if ( !exists $self->{'devices'}->{$device} );
  182. return;
  183. }
  184. 1;
  185. __END__
  186. =head1 AUTHOR
  187. Paul Trost <ptrost@cpan.org>
  188. =head1 LICENSE AND COPYRIGHT
  189. Copyright 2014 by Paul Trost
  190. 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.
  191. <http://gnu.org/licenses/gpl.html>
  192. =cut