New script - cleantmp.pl
[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;
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/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       if ($createdFile =~ /$pattern/) {
119         if (-d "$opts{tmp}/$createdFile") {
120           remove_tree ("$opts{tmp}/$createdFile", {error => \my $err});
121
122           if (@$err) {
123             for my $diag (@$err) {
124               my ($file, $message) = %$diag;
125
126               if ($file eq '') {
127                 $log->err("General error: $message");
128               } else {
129                 $log->err("Unable to delete $file: $message");
130               } # if
131             } # for
132           } else {
133             $log->msg(scalar localtime . " $opts{tmp}/$createdFile removed");
134           } # if
135         } else {
136           unless (unlink "$opts{tmp}/$createdFile") {
137             $log->err("Unable to remove $opts{tmp}/$createdFile - $!");
138           } else {
139             $log->msg(scalar localtime . " $opts{tmp}/$createdFile removed");
140           } # if
141         } # if
142
143
144         last;
145       } # if
146     } # for
147   } # while
148
149   return;
150 } # FileCreated
151
152 ## Main
153 GetOptions (
154   \%opts,
155   'usage',
156   'help',
157   'verbose',
158   'debug',
159   'tmp=s',
160   'logpath=s',
161   'conf=s',
162   'sleep=i'
163 ) or pod2usage;
164
165 $log = Logger->new(path => $opts{logpath});
166
167 $log->msg(scalar localtime . ": Starting $FindBin::Script");
168
169 my $monitor = File::Monitor->new;
170
171 $monitor->watch({
172   name     => $opts{tmp},
173   files    => 1,
174   callback => {files_created => \&FileCreated}
175 });
176
177 set_debug if $DB::OUT;
178
179 EnterDaemonMode unless $DB::OUT;
180
181 while () {
182   $monitor->scan;
183
184   sleep $opts{sleep};
185 } # while
186