Moved user log files to /var/local/log
[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    -s|leep <secs>:   How many seconds to sleep between polls (Default: 60)
50
51 =head1 DESCRIPTION
52
53 This script will run in the background and keep /tmp clean. It will read in a 
54 list of Perl regexs from the config file. When new files are created in tmp they
55 will be compared against the list of regexs and if there's a match the file or
56 directory will be removed. 
57
58 The sleep parameter tells us how long to wait before polling for changes again
59
60 =cut
61
62 use FindBin;
63 use Getopt::Long;
64 use Pod::Usage;
65 use File::Monitor;
66 use File::Spec;
67 use File::Path qw/remove_tree/;
68
69 use lib "$FindBin::Bin/../lib";
70
71 use Display;
72 use Logger;
73 use Utils;
74
75 my ($script) = ($FindBin::Script =~ /^(.*)\.pl/);
76 my $log;
77
78 my %opts = (
79   usage   => sub { pod2usage },
80   help    => sub { pod2usage(-verbose => 2)},
81   verbose => sub { set_verbose },
82   debug   => sub { set_debug },
83   tmp     => File::Spec->tmpdir(),
84   conf    => "$FindBin::Bin/../etc/$script.conf",
85   logpath => '/var/local/log',
86   sleep   => 60,
87 );
88
89 sub loadConfig() {
90   my @patterns;
91
92   open my $patterns, '<', $opts{conf}
93     or $log->err("Unable to open $opts{conf} - $!", 1);
94
95   while (<$patterns>) {
96     next if /^\s*#/; # Skip comments
97
98     chomp;
99
100     push @patterns, $_;
101   } # while
102
103   close $patterns;
104
105   return @patterns;
106 } # loadConfig
107
108 sub FileCreated {
109   my ($name, $event, $change) = @_;
110
111   opendir my $dir, $opts{tmp}
112     or $log->err("Unable to open $opts{tmp} - $!", 1);
113
114   while (my $createdFile = readdir $dir) {
115     next if $createdFile =~ /^\./; # Skip all hidden files
116
117     for my $pattern (loadConfig) {
118       debug "Processing pattern $pattern";
119
120       if ($createdFile =~ /$pattern/) {
121         debug "Matched $createdFile to $pattern";
122
123         if (-d "$opts{tmp}/$createdFile") {
124           remove_tree ("$opts{tmp}/$createdFile", {error => \my $err});
125
126           if (@$err) {
127             for my $diag (@$err) {
128               my ($file, $message) = %$diag;
129
130               if ($file eq '') {
131                 $log->err("General error: $message");
132               } else {
133                 $log->err("Unable to delete $file: $message");
134               } # if
135             } # for
136           } else {
137             $log->msg("$opts{tmp}/$createdFile removed");
138           } # if
139         } else {
140           unless (unlink "$opts{tmp}/$createdFile") {
141             $log->err("Unable to remove $opts{tmp}/$createdFile - $!");
142           } else {
143             $log->msg("$opts{tmp}/$createdFile removed");
144           } # if
145         } # if
146
147         last;
148       } # if
149     } # for
150   } # while
151
152   return;
153 } # FileCreated
154
155 $SIG{USR1} = \&FileCreated;
156
157 ## Main
158 GetOptions (
159   \%opts,
160   'usage',
161   'help',
162   'verbose',
163   'debug',
164   'tmp=s',
165   'logpath=s',
166   'conf=s',
167   'sleep=i'
168 ) or pod2usage;
169
170 $log = Logger->new(path => $opts{logpath}, timestamped => 1);
171
172 $log->msg("Starting $FindBin::Script");
173
174 # First run through whatever junk is in /tmp
175 for (glob "$opts{tmp}/*") {
176   FileCreated($_);
177 } # for
178
179 my $monitor = File::Monitor->new;
180
181 $monitor->watch({
182   name     => $opts{tmp},
183   files    => 1,
184   callback => {files_created => \&FileCreated}
185 });
186
187 set_debug if $DB::OUT;
188
189 EnterDaemonMode unless $DB::OUT or get_debug;
190
191 while () {
192   $monitor->scan;
193
194   sleep $opts{sleep};
195 } # while
196