SMART.pm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package Disk::SMART;
  2. use warnings;
  3. use strict;
  4. use Carp;
  5. use Math::Round;
  6. =head1 NAME
  7. Disk::SMART - Provides an interface to smartctl
  8. =head1 VERSION
  9. Version 0.03
  10. =cut
  11. our $VERSION = '0.03';
  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 METHODS
  18. =head2 B<new (DEVICE)>
  19. Instantiates the Disk::SMART object
  20. C<DEVICE> - Device identifier of SSD / Hard Drive
  21. my $smart = Disk::SMART->new( 'dev/sda', '/dev/sdb' );
  22. =cut
  23. sub new {
  24. my ( $class, @devices ) = @_;
  25. my $smartctl = '/usr/sbin/smartctl';
  26. my $self = bless {}, $class;
  27. croak "Valid device identifier not supplied to constructor for $class.\n" if ( !@devices );
  28. croak "smartctl binary was not found on your system, are you running as root?\n" if !-f $smartctl;
  29. foreach my $device (@devices) {
  30. my $out = qx($smartctl -a $device);
  31. if ( $out =~ /No such device/i ) {
  32. croak "Smartctl couldn't poll device $device\n";
  33. }
  34. $self->{'devices'}->{$device}->{'SMART_OUTPUT'} = $out;
  35. }
  36. return $self;
  37. }
  38. =head1 Getting information from smartctl
  39. =cut
  40. =head2 B<get_disk_temp (DEVICE)>
  41. Returns an array with the temperature of the device in Celsius and Farenheit, or N/A.
  42. C<DEVICE> - Device identifier of SSD / Hard Drive
  43. my ($temp_c, $temp_f) = $smart->get_disk_temp('/dev/sda');
  44. =cut
  45. sub get_disk_temp {
  46. my ( $self, $device ) = @_;
  47. $self->_validate_param($device);
  48. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  49. my ($temp_c) = $smart_output =~ /(Temperature_Celsius.*\n)/;
  50. if ( !defined $temp_c || $smart_output =~ qr/S.M.A.R.T. not available/x ) {
  51. return 'N/A';
  52. }
  53. chomp $temp_c;
  54. $temp_c =~ s/ //g;
  55. $temp_c =~ s/.*-//;
  56. $temp_c =~ s/\(.*\)//;
  57. my $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
  58. return ( $temp_c, $temp_f );
  59. }
  60. =head2 B<get_disk_health (DEVICE)>
  61. Returns the health of the disk. Output is "PASSED", "FAILED", or "N/A".
  62. C<DEVICE> - Device identifier of SSD / Hard Drive
  63. my $disk_health = $smart->get_disk_health('/dev/sda');
  64. =cut
  65. sub get_disk_health {
  66. my ( $self, $device ) = @_;
  67. $self->_validate_param($device);
  68. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  69. my ($health) = $smart_output =~ /(SMART overall-health self-assessment.*\n)/;
  70. if ( (!defined $health) or $health !~ /PASSED|FAILED/x ) {
  71. return 'N/A';
  72. }
  73. $health =~ s/.*: //;
  74. chomp $health;
  75. return $health;
  76. }
  77. =head2 B<get_disk_model (DEVICE)>
  78. Returns the model of the device. eg. "ST3250410AS".
  79. C<DEVICE> - Device identifier of SSD / Hard Drive
  80. my $disk_model = $smart->get_disk_model('/dev/sda');
  81. =cut
  82. sub get_disk_model {
  83. my ( $self, $device ) = @_;
  84. $self->_validate_param($device);
  85. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  86. my ($model) = $smart_output =~ /(Device\ Model.*\n)/;
  87. if ( !defined $model ) {
  88. return 'N/A';
  89. }
  90. $model =~ s/.*:\ //;
  91. $model =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  92. return $model;
  93. }
  94. =head2 B<get_disk_errors (DEVICE)>
  95. Returns any listed errors
  96. C<DEVICE> - DEvice identifier of SSD/ Hard Drive
  97. my $disk_errors = $smart->get_disk_errors('/dev/sda');
  98. =cut
  99. sub get_disk_errors {
  100. my ( $self, $device ) = @_;
  101. $self->_validate_param($device);
  102. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  103. my ($errors) = $smart_output =~ /SMART Error Log Version: [1-9](.*)SMART Self-test log/s;
  104. if ( !defined $errors ) {
  105. return 'N/A';
  106. }
  107. $errors =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  108. return $errors;
  109. }
  110. sub _validate_param {
  111. my ( $self, $device ) = @_;
  112. croak "$device not found in object, You probably didn't enter it right" if ( !exists $self->{'devices'}->{$device} );
  113. }
  114. 1;
  115. __END__
  116. =head1 AUTHOR
  117. Paul Trost <paul.trost@trostfamily.org>
  118. =head1 LICENSE AND COPYRIGHT
  119. Copyright 2014.
  120. 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.
  121. <http://gnu.org/licenses/gpl.html>
  122. =cut