File Coverage

blib/lib/Daemon/Shutdown/Monitor/smbstatus.pm
Criterion Covered Total %
statement 18 55 32.7
branch 0 12 0.0
condition 0 6 0.0
subroutine 6 8 75.0
pod 2 2 100.0
total 26 83 31.3


line stmt bran cond sub pod time code
1             package Daemon::Shutdown::Monitor::smbstatus;
2              
3 1     1   1318 use warnings;
  1         2  
  1         27  
4 1     1   5 use strict;
  1         1  
  1         23  
5 1     1   4 use Params::Validate qw/:all/;
  1         2  
  1         159  
6 1     1   5 use IPC::Run;
  1         2  
  1         29  
7 1     1   4 use YAML::Any;
  1         2  
  1         5  
8 1     1   1117 use Log::Log4perl;
  1         2  
  1         6  
9              
10             =head1 NAME
11              
12             Daemon::Shutdown::Monitor::smbstatus - check for samba locks
13              
14             =head1 SYNOPSIS
15              
16             Monitor samba file locks
17              
18             =head1 DESCRIPTION
19              
20             Uses C to look for locked files every "loop_sleep". If no files are locked,
21             the flag "trigger_pending" is set. If a further "trigger_time" seconds
22             pass and all disks are still in a spun down state, the trigger is sent back to the parent
23             process (return 1).
24              
25             =head1 METHODS
26              
27             =head2 new
28              
29             =over 2
30              
31             =item loop_sleep
32              
33             How long to sleep between each test
34              
35             Default: 60 (1 minute)
36              
37             =item trigger_time
38              
39             The time to wait after discovering that all disks are spun down before returning (trigger a shutdown).
40              
41             Default: 3600 (1 hour)
42              
43             =back
44              
45             =head3 Example configuration
46            
47             monitor:
48             smbstatus:
49             trigger_time: 1800
50             loop_sleep: 360
51              
52             =cut
53              
54             sub new {
55 0     0 1   my $class = shift;
56 0           my %params = @_;
57              
58             # Validate the config file
59 0           %params = validate_with(
60             params => \%params,
61             spec => {
62             loop_sleep => {
63             regex => qr/^\d*$/,
64             default => 60,
65             },
66             trigger_time => {
67             regex => qr/^\d*$/,
68             default => 3600,
69             },
70             },
71             );
72 0           my $self = {};
73 0           $self->{params} = \%params;
74              
75 0           $self->{trigger_pending} = 0;
76              
77 0           bless $self, $class;
78 0           my $logger = Log::Log4perl->get_logger();
79 0           $self->{logger} = $logger;
80 0           $logger->debug( "Monitor smbstatus params:\n" . Dump( \%params ) );
81              
82 0           return $self;
83             }
84              
85             =head2 run
86              
87             Run the smbstatus lock monitor
88              
89             =cut
90              
91             sub run {
92 0     0 1   my $self = shift;
93              
94 0           my $logger = $self->{logger};
95              
96 0           $logger->info( "Monitor started running: smbstatus" );
97 0           my $conditions_met = 1;
98              
99             # look for locks
100 0           my @cmd = ( qw/smbstatus -L/ );
101 0           $logger->debug( "Monitor smbstatus CMD: " . join( ' ', @cmd ) );
102              
103 0           my ( $in, $out, $err );
104 0 0         if ( not IPC::Run::run( \@cmd, \$in, \$out, \$err, IPC::Run::timeout( 10 ) ) ) {
105 0           $logger->warn( "Could not run '" . join( ' ', @cmd ) . "': $!" );
106             }
107              
108 0 0         if ( $err ) {
109 0           $logger->error( "Monitor smbstatus: $err" );
110 0           $conditions_met = 0;
111             }
112              
113             # If are active locks, the conditions for trigger are not met
114             # XXX other languages?
115 0 0         if ( $out =~ m/Locked files:/ ) {
116 0           $logger->debug( "Monitor smbstatus sees active file locks" );
117 0           $conditions_met = 0;
118             }
119              
120 0 0         if ( $conditions_met ) {
121 0   0       $self->{trigger_pending} = $self->{trigger_pending} || time();
122              
123 0 0 0       if ( $self->{trigger_pending}
124             and ( time() - $self->{trigger_pending} ) >= $self->{params}->{trigger_time} )
125             {
126              
127             # ... and the trigger was set, and time has run out: time to return!
128 0           $logger->info( "Monitor smbstatus trigger time reached after $self->{params}->{trigger_time}" );
129             # Reset the trigger_pending because otherwise if this was a suspend, and the computer comes
130             # up again hours/days later, it will immediately fall asleep again...
131 0           $self->{trigger_pending} = 0;
132 0           return 1;
133             }
134              
135 0           $logger->info( "Monitor smbstatus found no locks: trigger pending." );
136             } else {
137 0 0         if ( $self->{trigger_pending} ) {
138 0           $logger->info( "Monitor smbstatus trigger time being reset because of new file locks" );
139             }
140              
141             # Conditions not met - reset the trigger incase it was previously set.
142 0           $self->{trigger_pending} = 0;
143             }
144              
145 0           return 0;
146             }
147              
148             =head1 AUTHOR
149              
150             Robin Clarke, C<< >>
151              
152             Ioan Rogers, C<< >>
153              
154             =head1 LICENSE AND COPYRIGHT
155              
156             Copyright 2011 Robin Clarke.
157              
158             This program is free software; you can redistribute it and/or modify it
159             under the terms of either: the GNU General Public License as published
160             by the Free Software Foundation; or the Artistic License.
161              
162             See http://dev.perl.org/licenses/ for more information.
163              
164             =cut
165              
166             1; # End of Daemon::Shutdown::Monitor::smbstatus