| 1 | package Daemon::Shutdown::Monitor::hdparm; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Params::Validate qw/:all/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use IPC::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use YAML::Any; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use User; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Daemon::Shutdown::Monitor::hdparm - a hdparm specific monitor | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Version 0.07 | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Monitor hard disk spindown state using hdparm | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Tests the spin state of all the disks listed in "disks" every "loop_sleep". When all disks | ||||||||||||||||||||||||||||
| 30 | are in spun down state, the flag "trigger_pending" is set. If a further "trigger_time" seconds | ||||||||||||||||||||||||||||
| 31 | pass and all disks are still in a spun down state, the trigger is sent back to the parent | ||||||||||||||||||||||||||||
| 32 | process (return 1). | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 METHODS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head2 new | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over 2 | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item loop_sleep <Int> | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | How long to sleep between each test | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Default: 60 (1 minute) | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =item disks <ArrayRef> | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | An array of disks to be tested. e.g. /dev/sda | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Default: [ '/dev/sda' ] | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item trigger_time <Int> | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | The time to wait after discovering that all disks are spun down before returning (trigger a shutdown). | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Default: 3600 (1 hour) | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =item use_sudo 1|0 | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Use sudo for hdparm | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sudo hdparm -C /dev/sda | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Default: 0 | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =back | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head3 Example configuration | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | monitor: | ||||||||||||||||||||||||||||
| 71 | hdparm: | ||||||||||||||||||||||||||||
| 72 | trigger_time: 1800 | ||||||||||||||||||||||||||||
| 73 | loop_sleep: 1 | ||||||||||||||||||||||||||||
| 74 | use_sudo: 0 | ||||||||||||||||||||||||||||
| 75 | disks: | ||||||||||||||||||||||||||||
| 76 | - /dev/sdb | ||||||||||||||||||||||||||||
| 77 | - /dev/sdc | ||||||||||||||||||||||||||||
| 78 | - /dev/sdd | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub new { | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Validate the config file | ||||||||||||||||||||||||||||
| 87 | %params = validate_with( | ||||||||||||||||||||||||||||
| 88 | params => \%params, | ||||||||||||||||||||||||||||
| 89 | spec => { | ||||||||||||||||||||||||||||
| 90 | loop_sleep => { | ||||||||||||||||||||||||||||
| 91 | regex => qr/^\d*$/, | ||||||||||||||||||||||||||||
| 92 | default => 60, | ||||||||||||||||||||||||||||
| 93 | }, | ||||||||||||||||||||||||||||
| 94 | trigger_time => { | ||||||||||||||||||||||||||||
| 95 | regex => qr/^\d*$/, | ||||||||||||||||||||||||||||
| 96 | default => 3600, | ||||||||||||||||||||||||||||
| 97 | }, | ||||||||||||||||||||||||||||
| 98 | disks => { | ||||||||||||||||||||||||||||
| 99 | type => ARRAYREF, | ||||||||||||||||||||||||||||
| 100 | default => ['/dev/sda'], | ||||||||||||||||||||||||||||
| 101 | callbacks => { | ||||||||||||||||||||||||||||
| 102 | 'Disks exist' => sub { | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my $disks_ref = shift(); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 104 | 0 | foreach my $disk ( @{$disks_ref} ) { | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | return 0 if !-e $disk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 0 | return 1; | |||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||||||||||
| 110 | }, | ||||||||||||||||||||||||||||
| 111 | 0 | use_sudo => { | |||||||||||||||||||||||||||
| 112 | default => 0, | ||||||||||||||||||||||||||||
| 113 | regex => qr/^[1|0]$/, | ||||||||||||||||||||||||||||
| 114 | }, | ||||||||||||||||||||||||||||
| 115 | }, | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 118 | 0 | $self->{params} = \%params; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | $self->{trigger_pending} = 0; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 123 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 124 | 0 | $self->{logger} = $logger; | |||||||||||||||||||||||||||
| 125 | 0 | $logger->debug( "Monitor hdparm params:\n" . Dump( \%params ) ); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | return $self; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head2 run | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Run the hdparm spindown Monitor | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub run { | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | my $logger = $self->{logger}; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | $logger->info( "Monitor started running: hdparm" ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $conditions_met = 1; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Test each disk | ||||||||||||||||||||||||||||
| 146 | 0 | foreach my $disk ( @{ $self->{params}->{disks} } ) { | |||||||||||||||||||||||||||
| 147 | 0 | $logger->debug( "Monitor hdparm testing $disk" ); | |||||||||||||||||||||||||||
| 148 | 0 | my @cmd = ( qw/hdparm -C/, $disk ); | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | if ( $self->{params}->{use_sudo} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | unshift( @cmd, 'sudo' ); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 0 | $logger->debug( "Monitor hdparm CMD: " . join( ' ', @cmd ) ); | |||||||||||||||||||||||||||
| 153 | 0 | my ( $in, $out, $err ); | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ( not IPC::Run::run( \@cmd, \$in, \$out, \$err, IPC::Run::timeout( 10 ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | $logger->warn( "Could not run '" . join( ' ', @cmd ) . "': $!" ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if ( $err ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | $logger->error( "Monitor hdparm: $err" ); | |||||||||||||||||||||||||||
| 159 | 0 | $conditions_met = 0; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # If any of the disks are active, the conditions for trigger are not met | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ( $out =~ m/drive state is: active/s ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | $logger->debug( "Monitor hdparm sees disk is active: $disk" ); | |||||||||||||||||||||||||||
| 165 | 0 | $conditions_met = 0; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ( $conditions_met ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # All disks are spun down! Set the trigger_pending time. | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $self->{trigger_pending} = $self->{trigger_pending} || time(); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'trigger_pending'} || time
MC/DC0%0/2
$self->{'trigger_pending'} || time
$self->{'trigger_pending'}
time
Truth table0%0/3
A || B
A= $self->{'trigger_pending'}B= time
| |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ( $self->{trigger_pending} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"trigger_pending"} and time - $self->{"trigger_pending"} >= $self->{"params"}{"trigger_time"}
MC/DC0%0/2
$self->{"trigger_pending"} and time - $self->{"trigger_pending"} >= $self->{"params"}{"trigger_time"}
$self->{"trigger_pending"}
time - $self->{"trigger_pending"} >= $self->{"params"}{"trigger_time"}
Truth table0%0/3
A and B
A= $self->{"trigger_pending"}B= time - $self->{"trigger_pending"} >= $self->{"params"}{"trigger_time"}
| |||||||||||||||||||||||||||||
| 174 | and ( time() - $self->{trigger_pending} ) >= $self->{params}->{trigger_time} ) | ||||||||||||||||||||||||||||
| 175 | { | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # ... and the trigger was set, and time has run out: time to return! | ||||||||||||||||||||||||||||
| 178 | 0 | $logger->info( "Monitor hdparm trigger time reached after $self->{params}->{trigger_time}" ); | |||||||||||||||||||||||||||
| 179 | 0 | return 1; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | $logger->info( "Monitor hdparm found all disks spun down: trigger pending." ); | |||||||||||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | if ( $self->{trigger_pending} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | $logger->info( "Monitor hdparm trigger time being reset because of disk activity" ); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # Conditions not met - reset the trigger incase it was previously set. | ||||||||||||||||||||||||||||
| 189 | 0 | $self->{trigger_pending} = 0; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 0 | return 0; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Robin Clarke, C<< <perl at robinclarke.net> >> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Copyright 2011 Robin Clarke. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 203 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 204 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1 | 1; # End of Daemon::Shutdown::Monitor::hdparm | |||||||||||||||||||||||||||