SMART.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.01
  10. =cut
  11. our $VERSION = '0.01';
  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. confess "Valid device identifier not supplied to constructor.\n" if ( !@devices );
  26. my $smartctl = '/usr/sbin/smartctl';
  27. if ( !-f $smartctl ) {
  28. confess "smartctl binary was not found on your system, are you running as root?\n";
  29. }
  30. my $self = bless {}, $class;
  31. foreach my $device (@devices) {
  32. my $out = qx($smartctl -a $device);
  33. if ( $out =~ /No such device/i ) {
  34. confess "Device $device could not be found\n";
  35. next;
  36. }
  37. $self->{'devices'}->{$device}->{'SMART_OUTPUT'} = $out;
  38. }
  39. return $self;
  40. }
  41. =head1 Getting information from smartctl
  42. =cut
  43. =head2 B<get_disk_temp (DEVICE)>
  44. Returns an array with the temperature of the device in Celsius and Farenheit, or N/A.
  45. C<DEVICE> - Device identifier of SSD / Hard Drive
  46. my ($temp_c, $temp_f) = $smart->get_disk_temp('/dev/sda');
  47. =cut
  48. sub get_disk_temp {
  49. my ( $self, $device ) = @_;
  50. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  51. my ($temp_c) = $smart_output =~ /(Temperature_Celsius.*\n)/;
  52. if ( defined $temp_c ) {
  53. chomp $temp_c;
  54. $temp_c =~ s/ //g;
  55. $temp_c =~ s/.*-//;
  56. $temp_c =~ s/\(.*\)//;
  57. }
  58. if ( !$temp_c || $smart_output =~ qr/S.M.A.R.T. not available/x ) {
  59. return 'N/A';
  60. }
  61. else {
  62. my $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
  63. return ( $temp_c, $temp_f );
  64. }
  65. return undef;
  66. }
  67. =head2 B<get_disk_health (DEVICE)>
  68. Returns the health of the disk. Output is "PASSED", "FAILED", or "N/A".
  69. C<DEVICE> - Device identifier of SSD / Hard Drive
  70. my $disk_health = $smart->get_disk_health('/dev/sda');
  71. =cut
  72. sub get_disk_health {
  73. my ( $self, $device ) = @_;
  74. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  75. my ($health) = $smart_output =~ /(SMART overall-health self-assessment.*\n)/;
  76. if ( defined $health and $health =~ /PASSED|FAILED/x ) {
  77. $health =~ s/.*: //;
  78. chomp $health;
  79. return $health;
  80. }
  81. else {
  82. return 'N/A';
  83. }
  84. }
  85. =head2 B<get_disk_model (DEVICE)>
  86. Returns the model of the device. Output is "<device>: <model>" or "N/A". eg. "/dev/sda: ST3250410AS"
  87. C<DEVICE> - Device identifier of SSD / Hard Drive
  88. my $disk_model = $smart->get_disk_model('/dev/sda');
  89. =cut
  90. sub get_disk_model {
  91. my ( $self, $device ) = @_;
  92. my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
  93. my ($model) = $smart_output =~ /(Device\ Model.*\n)/;
  94. if ( defined $model ) {
  95. $model =~ s/.*:\ //;
  96. $model =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
  97. }
  98. return ($model) ? "$device: $model" : "$device: N/A";
  99. }
  100. 1;
  101. __END__
  102. =head1 AUTHOR
  103. Paul Trost <paul.trost@trostfamily.org>
  104. =head1 LICENSE AND COPYRIGHT
  105. Copyright 2014.
  106. 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.
  107. <http://gnu.org/licenses/gpl.html>
  108. =cut