forked from nickandrew/logproc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogproc.pl
executable file
·205 lines (151 loc) · 3.79 KB
/
logproc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/perl
# @(#) logproc - Process log files
# Copyright (C) 2004,2009 Nick Andrew <[email protected]>
# Released under the terms of the GNU General Public License
#
# Usage: find path -type f -print | logproc [-d] ctlfile
#
use strict;
use Getopt::Std qw(getopts);
use vars qw($opt_d);
select(STDOUT);
$| = 1;
getopts('d');
my $real = 1;
if ($opt_d) {
$real = 0;
}
my $ctlfile = shift @ARGV;
if (! $ctlfile) {
usage();
die "Need to specify control file"
}
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon ++;
$year += 1900;
my $yyyymm = sprintf "%04d-%02d", $year, $mon;
my $yyyymmdd = sprintf "%04d-%02d-%02d", $year, $mon, $mday;
my @paths = parseConfig($ctlfile);
while (<STDIN>) {
chomp;
my $fn = $_;
checkPath($fn);
}
exit(0);
# ---------------------------------------------------------------------------
# Parse the config file
# Return output list
# ---------------------------------------------------------------------------
sub parseConfig {
my ($ctlfile) = @_;
my @paths;
open(C, "<$ctlfile") || die "ctlfile: $ctlfile: $!";
while (<C>) {
chomp;
next if (/^$/);
next if (/^#/);
my ($regex,$crit,$disp,@rest) = split(/\|/);
my $ref = {
regex => $regex,
disp => $disp,
target => undef,
mtime => undef,
cmd => undef,
};
# Parse criteria
my ($count,$type) = ($crit =~ /(\d+)(.)/);
if ($type eq 'y') {
$ref->{target} = sprintf("%04d", $year - $count);
} elsif ($type eq 'm') {
my $y = $year;
my $m = $mon - $count;
while ($m < 1) {
$y--;
$m += 12;
}
$ref->{target} = sprintf("%04d-%02d", $y, $m);
} elsif ($type eq 'd') {
my $then = time - 86400 * $count;
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($then);
$ref->{mtime} = $then;
}
if ($disp eq "discard") {
} elsif ($disp eq "exec") {
$ref->{cmd} = $rest[0];
} else {
print STDERR "Unknown disposition: $regex|$crit|$disp\n";
next;
}
push(@paths, $ref);
}
close(C);
return @paths;
}
# ---------------------------------------------------------------------------
# Check a single pathname ($fn) against our set of regular expressions
# ---------------------------------------------------------------------------
sub checkPath {
my ($fn) = @_;
# print "\nChecking $fn\n";
# File mtime (filled in later)
my $mtime;
foreach my $ref (@paths) {
my $regex = $ref->{regex};
# print "Trying: $regex\n";
if ($fn =~ /^$regex/) {
my $disp = $ref->{disp};
# print "Match: $fn (disp is $disp)\n";
if ($disp eq "ignore") {
last;
}
if (! $mtime) {
my @s = stat($fn);
if (!@s) {
print STDERR "Unable to stat $fn : $!\n";
return;
}
$mtime = $s[9];
}
if (defined $ref->{mtime}) {
# print "Checking mtime: $mtime\n";
if ($mtime >= $ref->{mtime}) {
last;
}
}
if (defined $ref->{target}) {
# Convert mtime to ISO8601 format
my ($ssec,$smin,$shour,$smday,$smon,$syear,@srest) = localtime($mtime);
$smon ++;
$syear += 1900;
my $mstr = sprintf "%04d-%02d-%02d %02d:%02d:%02d", $syear, $smon, $smday, $shour, $smin, $ssec;
# print "Checking mstr: $mstr\n";
if ($mstr ge $ref->{target}) {
next;
}
}
# All tests passed
if ($disp eq "discard") {
print "Unlink $fn\n";
if ($real) {
unlink($fn);
}
} elsif ($disp eq "exec") {
my $run = sprintf($ref->{cmd}, $fn);
print "Execute $run\n";
if ($real) {
system($run);
}
} else {
print "Unknown disposition: $disp\n";
}
# We matched, so we're done checking.
last;
}
}
}
# ---------------------------------------------------------------------------
# Output a usage message
# ---------------------------------------------------------------------------
sub usage {
die "Usage: logproc [-d] ctlfile";
}