Changed announceEmail.pl to strip out things like order numbers
[clearscm.git] / bin / cleantmp.pl
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use v5.22;
5
6 =pod
7
8 =head1 NAME $RCSfile: cleantmp.pl,v $
9
10 Keep /tmp clean based on filesets
11
12 =head1 VERSION
13
14 =over
15
16 =item Author
17
18 Andrew DeFaria <Andrew@DeFaria.com>
19
20 =item Revision
21
22 $Revision: 1.0 $
23
24 =item Created:
25
26 Wed Feb 21 20:43:58 PST 2018
27
28 =item Modified:
29
30 $Date: $
31
32 =back
33
34 =head1 SYNOPSIS
35
36  Usage: cleantmp.pl [-u|sage] [-h|elp] [-v|erbose] [-d|ebug]
37                     [-t|mp <dir>] [-c|onf <file>] [-l|ogpath <path>]
38                     [-s|leep <secs>]
39
40  Where:
41    -u|sage           Print this usage
42    -h|elp            Detailed help
43    -v|erbose:        Verbose mode
44    -d|ebug:          Print debug messages
45    -t|mp <dir>:      Tmp directory to monitor (Default: /tmp)
46    -c|onf <file>:    Config file holding patterns to match (Default: 
47                      .../etc/cleantmp.conf)
48    -l|ogpath <path>: Path to logfile (Default: /var/log)
49    -a|ppend:         Append to logfile (Default: Noappend)
50    -da|emon          Run in daemon mode (Default: -daemon)
51    -s|leep <secs>:   How many seconds to sleep between polls (Default: 60)
52
53 =head1 DESCRIPTION
54
55 This script will run in the background and keep /tmp clean. It will read in a 
56 list of Perl regexs from the config file. When new files are created in tmp they
57 will be compared against the list of regexs and if there's a match the file or
58 directory will be removed. 
59
60 The sleep parameter tells us how long to wait before polling for changes again
61
62 =cut
63
64 use FindBin;
65 use Getopt::Long;
66 use Pod::Usage;
67 use File::Monitor;
68 use File::Spec;
69 use File::Path qw/remove_tree/;
70
71 use lib "$FindBin::Bin/../lib";
72
73 use Display;
74 use Logger;
75 use Utils;
76
77 my ($script) = ($FindBin::Script =~ /^(.*)\.pl/);
78 my $log;
79
80 my %opts = (
81   usage   => sub { pod2usage },
82   help    => sub { pod2usage(-verbose => 2)},
83   verbose => sub { set_verbose },
84   debug   => sub { set_debug },
85   daemon  => 1,
86   tmp     => File::Spec->tmpdir(),
87   conf    => "$FindBin::Bin/../etc/$script.conf",
88   logpath => '/var/local/log',
89   sleep   => 60,
90 );
91
92 sub loadConfig() {
93   my @patterns;
94
95   open my $patterns, '<', $opts{conf}
96     or $log->err("Unable to open $opts{conf} - $!", 1);
97
98   while (<$patterns>) {
99     next if /^\s*#/; # Skip comments
100
101     chomp;
102
103     push @patterns, $_;
104   } # while
105
106   close $patterns;
107
108   return @patterns;
109 } # loadConfig
110
111 sub FileCreated {
112   my ($name, $event, $change) = @_;
113
114   opendir my $dir, $opts{tmp}
115     or $log->err("Unable to open $opts{tmp} - $!", 1);
116
117   while (my $createdFile = readdir $dir) {
118     next if $createdFile =~ /^\./; # Skip all hidden files
119
120     for my $pattern (loadConfig) {
121       debug "Processing pattern $pattern";
122
123       if ($createdFile =~ /$pattern/) {
124         debug "Matched $createdFile to $pattern";
125
126         if (-d "$opts{tmp}/$createdFile") {
127           remove_tree ("$opts{tmp}/$createdFile", {error => \my $err});
128
129           if (@$err) {
130             for my $diag (@$err) {
131               my ($file, $message) = %$diag;
132
133               if ($file eq '') {
134                 $log->err("General error: $message");
135               } else {
136                 $log->err("Unable to delete $file: $message");
137               } # if
138             } # for
139           } else {
140             $log->msg("$opts{tmp}/$createdFile removed");
141           } # if
142         } else {
143           unless (unlink "$opts{tmp}/$createdFile") {
144             $log->err("Unable to remove $opts{tmp}/$createdFile - $!");
145           } else {
146             $log->msg("$opts{tmp}/$createdFile removed");
147           } # if
148         } # if
149
150         last;
151       } # if
152     } # for
153   } # while
154
155   return;
156 } # FileCreated
157
158 $SIG{USR1} = \&FileCreated;
159
160 ## Main
161 GetOptions (
162   \%opts,
163   'usage',
164   'help',
165   'verbose',
166   'debug',
167   'daemon!',
168   'tmp=s',
169   'logpath=s',
170   'conf=s',
171   'sleep=i',
172   'append',
173 ) or pod2usage;
174
175 $log = Logger->new(
176   path        => $opts{logpath},
177   timestamped => 1,
178   append      => $opts{append},
179 );
180
181 $log->msg("Starting $FindBin::Script");
182
183 # First run through whatever junk is in /tmp
184 for (glob "$opts{tmp}/*") {
185   FileCreated($_);
186 } # for
187
188 my $monitor = File::Monitor->new;
189
190 $monitor->watch({
191   name     => $opts{tmp},
192   files    => 1,
193   callback => {files_created => \&FileCreated}
194 });
195
196 set_debug if $DB::OUT;
197
198 if ($opts{daemon}) {
199   # Perl complains if we reference $DB::OUT only once
200   no warnings;
201   EnterDaemonMode unless defined $DB::OUT or get_debug;
202   use warnings;
203 } # if
204
205 while () {
206   $monitor->scan;
207
208   sleep $opts{sleep};
209 } # while
210