-
Notifications
You must be signed in to change notification settings - Fork 13
/
multi-syphon
executable file
·121 lines (96 loc) · 2.93 KB
/
multi-syphon
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
#!/usr/bin/perl
# Wrapper around syphon -i
use strict;
use warnings;
use Date::Manip qw(ParseDate UnixDate DateCalc Date_Cmp);
use Getopt::Long;
Getopt::Long::Configure('bundling');
my %opts = ( verbosity => 1 );
GetOptions(
\%opts,
'help|h',
'dry-run|d',
) or usage();
sub usage {
warn @_, "\n" if @_;
(my $ME = $0) =~ s,.*/,,;
die <<EOUSAGE;
Usage: $ME IN-FILE FORMAT-TYPE FIRST-PERIOD LAST-PERIOD PERIOD-DURATION
Options:
-h, --help Show this help
-d, --dry-run
e.g. $ME error.log apache2 'July 2010' 'Oct 2010' '1 month'
First period should be the first period contained in the input file,
otherwise the first bucket file generated will contain entries from
multiple periods. TODO: auto-detect this.
Last period is included, so a file for it will be created.
EOUSAGE
}
usage() if @ARGV != 5 or $opts{help};
my %formats = (
# Typical Apache error log
# [Wed Jul 01 13:18:53 2009] ....
'apache1' => '^\[%a %b %d %T %Y\]',
# Typical Apache access log
# 178.154.163.29 - - [16/Apr/2011:13:40:44 +0000] "GET ...
'apache2' => '^\d[\d.]+ - - \[%d/%b/%Y:%T ',
# /var/www/wordpress/wp-content/debug.log
# [02-Feb-2011 09:53:13] ...
'wp-debug' => '^\[%d-%b-%Y %T\] ',
# date output
# Fri Apr 1 00:59:01 UTC 2011
'date' => '^%a %b %e %T ... %Y',
);
my ($in_file, $format_type, $first_period_start, $end, $duration) = @ARGV;
my $format = $formats{$format_type};
$format or die "Unrecognised format '$format_type'\n";
my $pattern_template = $format;
$pattern_template =~ s/%T/[ \\d]\\d:\\d\\d:\\d\\d/g;
if ($duration =~ /month/) {
$pattern_template =~ s/%d/\\d\\d/g;
$pattern_template =~ s/%e/[ \\d]\\d/g;
}
$pattern_template =~ s/%a/\\w{3}/g;
my $start_date = ParseDate($first_period_start);
my $end_date = ParseDate($end);
while (1) {
# print " " . UnixDate($start_date, "%c") . "\n";
# print " " . UnixDate($end_date, "%c") . "\n";
last if Date_Cmp($start_date, $end_date) > 0;
syphon_period($in_file, $format, $start_date, $duration);
$start_date = DateCalc($start_date, "+$duration");
}
sub output_cmd {
my @cmd = ();
for my $a (@_) {
my $arg = $a; # no in-place modification evilness
if ($arg =~ /["' ]/) {
$arg =~ s/"/\\"/g;
$arg = qq!"$arg"!;
}
push @cmd, $arg;
}
print join(" ", @cmd), "\n";
}
sub syphon_period {
my ($in_file, $format, $period_start, $duration) = @_;
my $period_end = DateCalc($period_start, "+$duration");
# print "Syphoning period from:\n";
# print " " . UnixDate($period_start, "%c") . "\n";
# print "to:\n";
# print " " . UnixDate($period_end, "%c") . "\n";
my $pattern = UnixDate($period_end, $pattern_template);
my $suffix = $duration =~ /month/ ? "-%Y-%m" : "-%Y-%m-%d";
my $out_file = $in_file . UnixDate($period_start, $suffix);
my @cmd = (
'syphon', '-i',
$pattern,
$in_file,
$out_file,
);
output_cmd(@cmd);
unless ($opts{'dry-run'}) {
system(@cmd);
die "Command failed\n" if $? > 0;
}
}