« blib/lib/Monitoring/Sneck/Checks/procs.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/wireguard.pm »
| 1 | package Monitoring::Sneck::Checks::smart; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 30 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 30 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | Monitoring::Sneck::Checks::smart - checks disk health through smartctl | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | Version 0.0.1 | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 30 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # Where smartctl is. Set by tests. SMARTCTL undef means the first of | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # SMARTCTL_PATHS that is there. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 30 | our $SMARTCTL; | |||||||||||||||||||||||||||||||||||||||||
| 23 | our @SMARTCTL_PATHS | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 30 | = ( '/usr/sbin/smartctl', '/usr/local/sbin/smartctl', '/usr/bin/smartctl', '/usr/local/bin/smartctl' ); | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | # ATA attributes counted as bad sectors, by ID, to what each is called. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 30 | our %ATA_BAD = ( 5 => 'reallocated', 197 => 'pending', 198 => 'uncorrectable' ); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # The bits of the NVMe critical warning, what each is called, and 1 if it | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # is only a WARNING, as running hot can pass. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 30 | our @NVME_WARNINGS = ( | |||||||||||||||||||||||||||||||||||||||||
| 32 | [ 0x01, 'spare below threshold', 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 33 | [ 0x02, 'over temperature', 1 ], | ||||||||||||||||||||||||||||||||||||||||||
| 34 | [ 0x04, 'reliability degraded', 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 35 | [ 0x08, 'read only', 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 36 | [ 0x10, 'volatile memory backup failed', 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 37 | [ 0x20, 'persistent memory unreliable', 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | smart|perl:smart | ||||||||||||||||||||||||||||||||||||||||||
| 43 | smart_no_usb|perl:smart -e '^/dev/sd[f-z]' | ||||||||||||||||||||||||||||||||||||||||||
| 44 | smart_raid|perl:smart -d /dev/bus/0,megaraid,0 -d /dev/bus/0,megaraid,1 | ||||||||||||||||||||||||||||||||||||||||||
| 45 | smart_hot|perl:smart --temp-warning 50 --temp-critical 60 | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | Checks disks through smartctl -j -a -n standby, which needs to be | ||||||||||||||||||||||||||||||||||||||||||
| 50 | smartmontools 7.0 or newer for its JSON output. Without --device, the | ||||||||||||||||||||||||||||||||||||||||||
| 51 | disks are those smartctl --scan-open finds. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | - CRITICAL :: The disk failing its SMART health check. A ATA attribute | ||||||||||||||||||||||||||||||||||||||||||
| 54 | failing now. A NVMe critical warning other than temperature. Bad | ||||||||||||||||||||||||||||||||||||||||||
| 55 | sectors or media errors over --critical. Wear or temperature over | ||||||||||||||||||||||||||||||||||||||||||
| 56 | their critical. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | - WARNING :: Bad sectors or media errors over --warning. The last ATA | ||||||||||||||||||||||||||||||||||||||||||
| 59 | self test failing. The NVMe temperature warning. Wear or temperature | ||||||||||||||||||||||||||||||||||||||||||
| 60 | over their warning. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | - UNKNOWN :: smartctl not being there, a disk that can not be opened, or | ||||||||||||||||||||||||||||||||||||||||||
| 63 | smartctl not returning JSON. | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | - OK :: Everything else, including disks in standby, which are left | ||||||||||||||||||||||||||||||||||||||||||
| 66 | asleep, disks without SMART, and finding no disks, so the same check | ||||||||||||||||||||||||||||||||||||||||||
| 67 | can go everywhere. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | Bad sectors and media errors are added up for --warning and | ||||||||||||||||||||||||||||||||||||||||||
| 70 | --critical, from... | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | - ATA :: Reallocated_Sector_Ct (5), Current_Pending_Sector (197), and | ||||||||||||||||||||||||||||||||||||||||||
| 73 | Offline_Uncorrectable (198). | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | - NVMe :: Media errors. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | - SCSI :: The grown defect list and uncorrected read, write, and | ||||||||||||||||||||||||||||||||||||||||||
| 78 | verify errors. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | Wear is the NVMe percentage used, or the SCSI percentage used | ||||||||||||||||||||||||||||||||||||||||||
| 81 | endurance indicator. ATA disks have none that is not vendor specific. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | A line for each disk follows the status line, with its model, serial, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | temperature, wear, and counts. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | This needs root, to open the disks. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | - -d, --device :: A disk to check instead of scanning, such as /dev/sda. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | A smartctl device type may follow after a comma, such as | ||||||||||||||||||||||||||||||||||||||||||
| 92 | /dev/bus/0,megaraid,0 or /dev/sda,sat. May be given more than once. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | - -e, --exclude :: A regex for scanned disks to skip, matched against | ||||||||||||||||||||||||||||||||||||||||||
| 95 | the name, such as ^/dev/sdf. May be given more than once. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | - -w, --warning :: Range for bad sectors and media errors. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | Default :: 0 | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | - -c, --critical :: Range for bad sectors and media errors. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | - --wear-warning :: Range for wear, in percent. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | Default :: 80 | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | - --wear-critical :: Range for wear, in percent. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | Default :: 90 | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | - --temp-warning :: Range for temperature, in Celsius. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | - --temp-critical :: Range for temperature, in Celsius. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 299 | ▶ | sub option_spec { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 299 | 'device|d=s@', 'exclude|e=s@', 'warning|w=s', 'critical|c=s', | |||||||||||||||||||||||||||||||||||||||||
| 117 | 'wear-warning=s', 'wear-critical=s', 'temp-warning=s', 'temp-critical=s', | ||||||||||||||||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 299 | ▶ | sub defaults { return { warning => '0', 'wear-warning' => '80', 'wear-critical' => '90' } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 299 | ▶ | sub ranges { return ( 'warning', 'critical', 'wear-warning', 'wear-critical', 'temp-warning', 'temp-critical' ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 299 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 299 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 299 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 299 | ▶ | foreach my $regex ( @{ $options->{exclude} // [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"exclude"} // []
MC/DC100%1/1
$options->{"exclude"} // []
$options->{"exclude"}
Truth table100%2/2
A
A= $options->{"exclude"}
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 26 | ▶ | push( @problems, '--exclude "' . $regex . '" is not a valid regex' ) if !eval { qr/$regex/; 1 }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 299 | return @problems; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } ## end sub validate | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | =head2 check | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | Does the check. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 298 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=19 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 298 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | my $smartctl = $SMARTCTL // ( grep { -x $_ } @SMARTCTL_PATHS )[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$Monitoring::Sneck::Checks::smart::SMARTCTL // (grep {-x $_;} @Monitoring::Sneck::Checks::smart::SMARTCTL_PATHS)[0]
MC/DC0%0/2
$Monitoring::Sneck::Checks::smart::SMARTCTL // (grep {-x $_;} @Monitoring::Sneck::Checks::smart::SMARTCTL_PATHS)[0]
$Monitoring::Sneck::Checks::smart::SMARTCTL
(grep {-x $_;} @Monitoring::Sneck::Checks::smart::SMARTCTL_PATHS)[0]
Truth table33%1/3
A // B
A= $Monitoring::Sneck::Checks::smart::SMARTCTLB= (grep {-x $_;} @Monitoring::Sneck::Checks::smart::SMARTCTL_PATHS)[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 298 | ▶ | return $self->finish( $self->UNKNOWN, 'smartctl is not there' ) if !defined($smartctl); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 298 | ▶ | return $self->finish( $self->UNKNOWN, $smartctl . ' is not there' ) if !-x $smartctl; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 297 | require JSON; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 297 | my @devices; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 297 | ▶ | foreach my $given ( @{ $self->opt('device') // [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("device") // []
MC/DC100%1/1
$self->opt("device") // []
$self->opt("device")
Truth table100%2/2
A
A= $self->opt("device")
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 212 | my ( $name, $type ) = split( /,/, $given, 2 ); | |||||||||||||||||||||||||||||||||||||||||
| 155 | 212 | push( @devices, { name => $name, type => $type } ); | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 297 | ▶ | if ( !defined( $devices[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 97 | my ( $exit, $output ) = $self->run_command( $smartctl, '--scan-open', '-j' ); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 93 | my $scan = eval { JSON->new->decode($output) }; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 93 | ▶ | return $self->finish( $self->UNKNOWN, 'smartctl --scan-open exited ' . $exit . ', without JSON' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | if ref($scan) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 93 | ▶ | my @excludes = map { qr/$_/ } @{ $self->opt('exclude') // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("exclude") // []
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 93 | ▶ | foreach my $found ( @{ $scan->{devices} // [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$scan->{"devices"} // []
MC/DC0%0/1
$scan->{"devices"} // []
$scan->{"devices"}
Truth table50%1/2
A
A= $scan->{"devices"}
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 146 | ▶ | next if grep { $found->{name} =~ $_ } @excludes; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 122 | push( @devices, { name => $found->{name}, type => $found->{type}, open_error => $found->{open_error} } ); | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 93 | ▶ | return $self->finish( $self->OK, 'no disks found' ) if !defined( $devices[0] ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | } ## end if ( !defined( $devices[0] ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 273 | my @codes; | |||||||||||||||||||||||||||||||||||||||||
| 171 | my @parts; | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 273 | my @details; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 273 | my $ok_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 273 | foreach my $device (@devices) { | |||||||||||||||||||||||||||||||||||||||||
| 175 | 329 | my ( $code, $says, $detail ) = $self->device_status( $smartctl, $device ); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 306 | push( @codes, $code ); | |||||||||||||||||||||||||||||||||||||||||
| 177 | 306 | ▶ | defined($says) ? push( @parts, $says ) : $ok_count++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 306 | ▶ | push( @details, $detail ) if defined($detail); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 250 | ▶ | if ($ok_count) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 107 | ▶ | my $noun = defined( $parts[0] ) ? 'other' : 'disk'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 107 | ▶ | $noun = $noun . 's' if $ok_count > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 107 | push( @parts, $ok_count . ' ' . $noun . ' ok' ); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 250 | return $self->finish( $self->worst(@codes), join( ', ', @parts ), @details ); | |||||||||||||||||||||||||||||||||||||||||
| 187 | } ## end sub check | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =head2 device_status | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | Runs smartctl -j -a -n standby on a disk and works out how it is. | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | - smartctl :: The path to smartctl. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | - device :: Hash ref of name, such as /dev/sda, type, a smartctl device | ||||||||||||||||||||||||||||||||||||||||||
| 196 | type or undef, and open_error, what smartctl --scan-open said when it | ||||||||||||||||||||||||||||||||||||||||||
| 197 | could not open it, if it could not. | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | Returns the code, what to say about the disk in the status line, undef | ||||||||||||||||||||||||||||||||||||||||||
| 200 | if it is fine, and the line about it for after the status line, undef | ||||||||||||||||||||||||||||||||||||||||||
| 201 | if smartctl said nothing useful. | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | my ( $code, $says, $detail ) = $check->device_status( '/usr/sbin/smartctl', { name => '/dev/sdb' } ); | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # ( 1, 'sdb 8 pending, 2 reallocated', 'sdb WDC WD40EFRX-68N32N0, S/N WD-WCC7K0000001, 36 C, ...' ) | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 329 | ▶ | sub device_status { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
device_status: called CC=47 SCAR=38.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 329 | my ( $self, $smartctl, $device ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | # what it is called in the output, the type after when it says which disk behind a controller | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 329 | my $short = $device->{name} =~ s{\A/dev/}{}r; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 329 | ▶ | $short = $short . ':' . $device->{type} if defined( $device->{type} ) && $device->{type} =~ /,/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $device->{"type"} and $device->{"type"} =~ /,/u
MC/DC100%2/2
defined $device->{"type"} and $device->{"type"} =~ /,/u
defined $device->{"type"}
$device->{"type"} =~ /,/u
Truth table100%3/3
A and B
A= defined $device->{"type"}B= $device->{"type"} =~ /,/u
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | return ( $self->UNKNOWN, $short . ' can not be opened, ' . $device->{open_error}, undef ) | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 329 | ▶ | if defined( $device->{open_error} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 308 | my @command = ( $smartctl, '-j', '-a', '-n', 'standby' ); | |||||||||||||||||||||||||||||||||||||||||
| 218 | 308 | ▶ | push( @command, '-d', $device->{type} ) if defined( $device->{type} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 308 | my ( $exit, $output ) = $self->run_command( @command, $device->{name} ); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 285 | my $info = eval { JSON->new->decode($output) }; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 285 | ▶ | return ( $self->UNKNOWN, $short . ' smartctl exited ' . $exit . ', without JSON', undef ) if ref($info) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | # bit 0 is a bad command line and bit 1 the disk not opening, or being asleep with -n standby | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 284 | ▶ | my @messages = map { $_->{string} // '' } @{ $info->{smartctl}{messages} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$_->{"string"} // ""
$info->{"smartctl"}{"messages"} // []
MC/DC0%0/1
$_->{"string"} // ""
$_->{"string"}
MC/DC100%1/1
$info->{"smartctl"}{"messages"} // []
$info->{"smartctl"}{"messages"}
Truth table50%1/2
A
A= $_->{"string"}
Truth table100%2/2
A
A= $info->{"smartctl"}{"messages"}
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 284 | ▶ | if ( $exit & 3 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | return ( $self->OK, $short . ' in standby', undef ) | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 13 | ▶ | if grep { /\bin (?:STANDBY|SLEEP|IDLE\S*) mode\b/i } @messages; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 2 | ▶ | return ( $self->UNKNOWN, $short . ' smartctl exited ' . $exit . ', ' . ( $messages[0] // 'saying nothing' ), | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$messages[0] // "saying nothing"
MC/DC0%0/1
$messages[0] // "saying nothing"
$messages[0]
Truth table50%1/2
A
A= $messages[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | undef ); | ||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 271 | ▶ | if ( defined( $info->{smart_support} ) && !$info->{smart_support}{available} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info->{"smart_support"} and not $info->{"smart_support"}{"available"}
MC/DC50%1/2
defined $info->{"smart_support"} and not $info->{"smart_support"}{"available"}
defined $info->{"smart_support"}
not $info->{"smart_support"}{"available"}
Truth table66%2/3
A and not(B)
A= defined $info->{"smart_support"}B= not $info->{"smart_support"}{"available"}
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 7 | return ( $self->OK, $short . ' has no SMART', undef ); | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 264 | my @codes = ( $self->OK ); | |||||||||||||||||||||||||||||||||||||||||
| 236 | 264 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 264 | ▶ | if ( defined( $info->{smart_status} ) && !$info->{smart_status}{passed} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info->{"smart_status"} and not $info->{"smart_status"}{"passed"}
MC/DC50%1/2
defined $info->{"smart_status"} and not $info->{"smart_status"}{"passed"}
defined $info->{"smart_status"}
not $info->{"smart_status"}{"passed"}
Truth table66%2/3
A and not(B)
A= defined $info->{"smart_status"}B= not $info->{"smart_status"}{"passed"}
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 29 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||||||||||||||||
| 239 | 29 | push( @problems, 'failing SMART health check' ); | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | # bad sectors and media errors, by what they are called | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 264 | my %bad; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 264 | ▶ | foreach my $attribute ( @{ $info->{ata_smart_attributes}{table} // [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$info->{"ata_smart_attributes"}{"table"} // []
MC/DC100%1/1
$info->{"ata_smart_attributes"}{"table"} // []
$info->{"ata_smart_attributes"}{"table"}
Truth table100%2/2
A
A= $info->{"ata_smart_attributes"}{"table"}
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 792 | ▶ | if ( ( $attribute->{when_failed} // '' ) eq 'now' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$attribute->{"when_failed"} // ""
MC/DC0%0/1
$attribute->{"when_failed"} // ""
$attribute->{"when_failed"}
Truth table50%1/2
A
A= $attribute->{"when_failed"}
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 15 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||||||||||||||||
| 247 | 15 | push( @problems, $attribute->{name} . ' failing' ); | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 792 | ▶ | $bad{ $ATA_BAD{ $attribute->{id} } } = $attribute->{raw}{value} // 0 if exists( $ATA_BAD{ $attribute->{id} } ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$attribute->{"raw"}{"value"} // 0
MC/DC0%0/1
$attribute->{"raw"}{"value"} // 0
$attribute->{"raw"}{"value"}
Truth table50%1/2
A
A= $attribute->{"raw"}{"value"}
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 264 | my $nvme = $info->{nvme_smart_health_information_log}; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 264 | ▶ | if ( defined($nvme) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 62 | ▶ | $bad{'media errors'} = $nvme->{media_errors} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$nvme->{"media_errors"} // 0
MC/DC0%0/1
$nvme->{"media_errors"} // 0
$nvme->{"media_errors"}
Truth table50%1/2
A
A= $nvme->{"media_errors"}
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 62 | foreach my $warning (@NVME_WARNINGS) { | |||||||||||||||||||||||||||||||||||||||||
| 255 | 372 | my ( $bit, $name, $warning_only ) = @{$warning}; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 372 | ▶ | next if !( ( $nvme->{critical_warning} // 0 ) & $bit ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$nvme->{"critical_warning"} // 0
MC/DC0%0/1
$nvme->{"critical_warning"} // 0
$nvme->{"critical_warning"}
Truth table50%1/2
A
A= $nvme->{"critical_warning"}
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 14 | ▶ | push( @codes, $warning_only ? $self->WARNING : $self->CRITICAL ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 14 | push( @problems, $name ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 264 | ▶ | $bad{'grown defects'} = $info->{scsi_grown_defect_list} if defined( $info->{scsi_grown_defect_list} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 264 | ▶ | if ( defined( $info->{scsi_error_counter_log} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 16 | my $uncorrected = 0; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 16 | foreach my $direction ( 'read', 'write', 'verify' ) { | |||||||||||||||||||||||||||||||||||||||||
| 265 | $uncorrected | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 48 | ▶ | = $uncorrected + ( $info->{scsi_error_counter_log}{$direction}{total_uncorrected_errors} // 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"scsi_error_counter_log"}{$direction}{"total_uncorrected_errors"} // 0
MC/DC0%0/1
$info->{"scsi_error_counter_log"}{$direction}{"total_uncorrected_errors"} // 0
$info->{"scsi_error_counter_log"}{$direction}{"total_uncorrected_errors"}
Truth table50%1/2
A
A= $info->{"scsi_error_counter_log"}{$direction}{"total_uncorrected_errors"}
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 16 | $bad{'uncorrected errors'} = $uncorrected; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 264 | my @bad_names = sort( keys(%bad) ); | |||||||||||||||||||||||||||||||||||||||||
| 271 | 264 | ▶ | if ( defined( $bad_names[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 264 | my $bad_total = 0; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 264 | $bad_total = $bad_total + $_ foreach values(%bad); | |||||||||||||||||||||||||||||||||||||||||
| 274 | 264 | my $bad_code = $self->threshold_status( $bad_total, $self->opt('warning'), $self->opt('critical') ); | |||||||||||||||||||||||||||||||||||||||||
| 275 | 264 | push( @codes, $bad_code ); | |||||||||||||||||||||||||||||||||||||||||
| 276 | 264 | ▶ | push( @problems, map { $bad{$_} . ' ' . $_ } grep { $bad{$_} } @bad_names ) if $bad_code != $self->OK; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 264 | $self->perfdata( | |||||||||||||||||||||||||||||||||||||||||
| 278 | label => $short . '_bad', | ||||||||||||||||||||||||||||||||||||||||||
| 279 | value => $bad_total, | ||||||||||||||||||||||||||||||||||||||||||
| 280 | warning => $self->opt('warning'), | ||||||||||||||||||||||||||||||||||||||||||
| 281 | critical => $self->opt('critical'), | ||||||||||||||||||||||||||||||||||||||||||
| 282 | min => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 283 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 284 | } ## end if ( defined( $bad_names[0] ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 264 | ▶ | my $wear = defined($nvme) ? $nvme->{percentage_used} : $info->{scsi_percentage_used_endurance_indicator}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 264 | ▶ | if ( defined($wear) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 62 | my $wear_code = $self->threshold_status( $wear, $self->opt('wear-warning'), $self->opt('wear-critical') ); | |||||||||||||||||||||||||||||||||||||||||
| 289 | 62 | push( @codes, $wear_code ); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 62 | ▶ | push( @problems, $wear . '% worn' ) if $wear_code != $self->OK; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 62 | $self->perfdata( | |||||||||||||||||||||||||||||||||||||||||
| 292 | label => $short . '_wear', | ||||||||||||||||||||||||||||||||||||||||||
| 293 | value => $wear, | ||||||||||||||||||||||||||||||||||||||||||
| 294 | uom => '%', | ||||||||||||||||||||||||||||||||||||||||||
| 295 | warning => $self->opt('wear-warning'), | ||||||||||||||||||||||||||||||||||||||||||
| 296 | critical => $self->opt('wear-critical'), | ||||||||||||||||||||||||||||||||||||||||||
| 297 | min => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 298 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 299 | } ## end if ( defined($wear) ) | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 264 | my $temperature = $info->{temperature}{current}; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 264 | ▶ | if ( defined($temperature) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 264 | my $temperature_code | |||||||||||||||||||||||||||||||||||||||||
| 304 | = $self->threshold_status( $temperature, $self->opt('temp-warning'), $self->opt('temp-critical') ); | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 264 | push( @codes, $temperature_code ); | |||||||||||||||||||||||||||||||||||||||||
| 306 | 264 | ▶ | push( @problems, $temperature . ' C' ) if $temperature_code != $self->OK; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 264 | $self->perfdata( | |||||||||||||||||||||||||||||||||||||||||
| 308 | label => $short . '_temp', | ||||||||||||||||||||||||||||||||||||||||||
| 309 | value => $temperature, | ||||||||||||||||||||||||||||||||||||||||||
| 310 | warning => $self->opt('temp-warning'), | ||||||||||||||||||||||||||||||||||||||||||
| 311 | critical => $self->opt('temp-critical'), | ||||||||||||||||||||||||||||||||||||||||||
| 312 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 313 | } ## end if ( defined($temperature) ) | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | # only the newest, as a old failure stays in the log after the disk has passed since | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 264 | my $last_test = $info->{ata_smart_self_test_log}{standard}{table}[0]; | |||||||||||||||||||||||||||||||||||||||||
| 317 | 264 | ▶ | if ( defined($last_test) && defined( $last_test->{status}{passed} ) && !$last_test->{status}{passed} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $last_test and defined $last_test->{"status"}{"passed"} and not $last_test->{"status"}{"passed"}
defined $last_test and defined $last_test->{"status"}{"passed"}
MC/DC66%2/3
defined $last_test and defined $last_test->{"status"}{"passed"} and not $last_test->{"status"}{"passed"}
defined $last_test
defined $last_test->{"status"}{"passed"}
not $last_test->{"status"}{"passed"}
Truth table75%3/4
A and B and not(C)
A= defined $last_testB= defined $last_test->{"status"}{"passed"}C= not $last_test->{"status"}{"passed"}
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 13 | push( @codes, $self->WARNING ); | |||||||||||||||||||||||||||||||||||||||||
| 319 | 13 | push( @problems, 'last self test failed' ); | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 264 | ▶ | my @detail = ( $short . ' ' . ( $info->{model_name} // 'unknown model' ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"model_name"} // "unknown model"
MC/DC0%0/1
$info->{"model_name"} // "unknown model"
$info->{"model_name"}
Truth table50%1/2
A
A= $info->{"model_name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 264 | ▶ | push( @detail, 'S/N ' . $info->{serial_number} ) if defined( $info->{serial_number} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | 264 | ▶ | push( @detail, $temperature . ' C' ) if defined($temperature); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 264 | ▶ | push( @detail, $wear . '% worn' ) if defined($wear); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 264 | push( @detail, map { $bad{$_} . ' ' . $_ } @bad_names ); | |||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 264 | ▶ | $self->worst(@codes), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | defined( $problems[0] ) ? $short . ' ' . join( ', ', @problems ) : undef, | ||||||||||||||||||||||||||||||||||||||||||
| 331 | join( ', ', @detail ) | ||||||||||||||||||||||||||||||||||||||||||
| 332 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 333 | } ## end sub device_status | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | 30 | 1; | |||||||||||||||||||||||||||||||||||||||||