Shutdown/Monitor/smbstatus.pm

filter: Statementstmt 35.7% 20 / 56 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 6 / 8 Podpod 100% 2 / 2 totaltotal 31.8% 28 / 88 CC 9 SCAR 39.3
CC
9
Cov
27%
run
90.0
new
2.0
BEGIN
1.0
CRAP 51.1
« Shutdown.pm ↑ Summary
1 package Daemon::Shutdown::Monitor::smbstatus;
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 Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Daemon::Shutdown::Monitor::smbstatus - check for samba locks
13  
14 =head1 VERSION
15  
16 Version 0.02
17  
18 =cut
19  
20 1 our $VERSION = '0.02';
21  
22 =head1 SYNOPSIS
23  
24 Monitor samba file locks
25  
26 =head1 DESCRIPTION
27  
28 Uses C<smbstatus> to look for locked files every "loop_sleep". If no files are locked,
29 the flag "trigger_pending" is set. If a further "trigger_time" seconds
30 pass and all disks are still in a spun down state, the trigger is sent back to the parent
31 process (return 1).
32  
33 =head1 METHODS
34  
35 =head2 new
36  
37 =over 2
38  
39 =item loop_sleep <Int>
40  
41 How long to sleep between each test
42  
43 Default: 60 (1 minute)
44  
45 =item trigger_time <Int>
46  
47 The time to wait after discovering that all disks are spun down before returning (trigger a shutdown).
48  
49 Default: 3600 (1 hour)
50  
51 =back
52  
53 =head3 Example configuration
54
55 monitor:
56 smbstatus:
57 trigger_time: 1800
58 loop_sleep: 360
59  
60 =cut
61  
62 sub new {
63 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
64 0 my %params = @_;
65  
66 # Validate the config file
67 0 %params = validate_with(
68 params => \%params,
69 spec => {
70 loop_sleep => {
71 regex => qr/^\d*$/,
72 default => 60,
73 },
74 trigger_time => {
75 regex => qr/^\d*$/,
76 default => 3600,
77 },
78 },
79 );
80 0 my $self = {};
81 0 $self->{params} = \%params;
82  
83 0 $self->{trigger_pending} = 0;
84  
85 0 bless $self, $class;
86 0 my $logger = Log::Log4perl->get_logger();
87 0 $self->{logger} = $logger;
88 0 $logger->debug( "Monitor smbstatus params:\n" . Dump( \%params ) );
89  
90 0 return $self;
91 }
92  
93 =head2 run
94  
95 Run the smbstatus lock monitor
96  
97 =cut
98  
99 sub run {
100 0 ▶ my $self = shift;
Subroutine run: not called CC=9 SCAR=45.0
Pod documented
101  
102 0 my $logger = $self->{logger};
103  
104 0 $logger->info( "Monitor started running: smbstatus" );
105 0 my $conditions_met = 1;
106  
107 # look for locks
108 0 my @cmd = ( qw/smbstatus -L/ );
109 0 $logger->debug( "Monitor smbstatus CMD: " . join( ' ', @cmd ) );
110  
111 0 my ( $in, $out, $err );
112 0 ▶ if ( not IPC::Run::run( \@cmd, \$in, \$out, \$err, IPC::Run::timeout( 10 ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (IPC::Run::run(\@cmd, \$in, \$out, \$err, IPC::Run::timeout(10))) 0 0 0
113 0 $logger->warn( "Could not run '" . join( ' ', @cmd ) . "': $!" );
114 }
115  
116 0 ▶ if ( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
117 0 $logger->error( "Monitor smbstatus: $err" );
118 0 $conditions_met = 0;
119 }
120  
121 # If are active locks, the conditions for trigger are not met
122 # XXX other languages?
123 0 ▶ if ( $out =~ m/Locked files:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /Locked files:/) 0 0 0
124 0 $logger->debug( "Monitor smbstatus sees active file locks" );
125 0 $conditions_met = 0;
126 }
127  
128 0 ▶ if ( $conditions_met ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conditions_met) { } 0 0 0
129 0 ▶ $self->{trigger_pending} = $self->{trigger_pending} || time();
Condition0%0/3
$self->{'trigger_pending'} || time
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'trigger_pending'} || time
$self->{'trigger_pending'} time
Truth table0%0/3
A || B
A= $self->{'trigger_pending'}B= time
ABresult
1X1
011
000
130  
131 0 ▶ if ( $self->{trigger_pending}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trigger_pending"} and time - $self->{"trigger_pending"} >= $self->{"params"}{"trigger_time"}) 0 0 0
Condition0%0/3
$self->{"trigger_pending"} and time - $self->{"trigger_pending"} >= $self->{"params"}{"trigger_time"}
!ll&&!rl&&r
000
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"}
ABresult
0X0
100
111
132 and ( time() - $self->{trigger_pending} ) >= $self->{params}->{trigger_time} )
133 {
134  
135 # ... and the trigger was set, and time has run out: time to return!
136 0 $logger->info( "Monitor smbstatus trigger time reached after $self->{params}->{trigger_time}" );
137 0 return 1;
138 }
139  
140 0 $logger->info( "Monitor smbstatus found no locks: trigger pending." );
141 } else {
142 0 ▶ if ( $self->{trigger_pending} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trigger_pending"}) 0 0 0
143 0 $logger->info( "Monitor smbstatus trigger time being reset because of new file locks" );
144 }
145  
146 # Conditions not met - reset the trigger incase it was previously set.
147 0 $self->{trigger_pending} = 0;
148 }
149  
150 0 return 0;
151 }
152  
153 =head1 AUTHOR
154  
155 Robin Clarke, C<< <perl at robinclarke.net> >>
156  
157 Ioan Rogers, C<< <ioan.rogers at gmail.com> >>
158  
159 =head1 LICENSE AND COPYRIGHT
160  
161 Copyright 2011 Robin Clarke.
162  
163 This program is free software; you can redistribute it and/or modify it
164 under the terms of either: the GNU General Public License as published
165 by the Free Software Foundation; or the Artistic License.
166  
167 See http://dev.perl.org/licenses/ for more information.
168  
169 =cut
170  
171 1 1; # End of Daemon::Shutdown::Monitor::smbstatus
« Shutdown.pm ↑ Summary