-
Notifications
You must be signed in to change notification settings - Fork 1
/
apachelog-showstartend
executable file
·234 lines (215 loc) · 6.3 KB
/
apachelog-showstartend
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/perl -w
# Die Jun 22 14:01:09 MEST 2004 - Tue, 22 Jun 2004 16:45:49 +0200 (inkl xreadline_backwards schreiben in ner alphaversion)
(my $email='christian.jaeger%ethlife,ethz,ch')=~ tr/%,/@./;
use strict;
my $allowed_hole=30;
my $allowed_overlap=15;
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname file(s)
show start date and, unless it's compressed, the end date of the file(s).
options:
-a|--all read/uncompress whole file if compressed to also get end time.
-m|--mtime use the mtime of compressed files as endtime.
-s|--sort sort on start time.
-l list files only, no time output; only works if also sorting.
-c|--check check if there are holes or overlaps between the logfiles.
accept a hole of $allowed_hole or overlap of $allowed_overlap seconds.
(Christian Jaeger <$email>)
";
exit @_ ? 1 : 0;
}
my @args;
my $DEBUG=0;
my $opt_readall;
my $opt_sort;
my $opt_l;
my $opt_check;
my $opt_mtime;
for (my $i=0; $i<=$#ARGV; $i++) {
local $_=$ARGV[$i];
if (/^--?h(elp)?$/) {
usage
} elsif ($_ eq '--') {
push @args, @ARGV[$i+1..$#ARGV];
last;
} elsif (/^--?d(ebug)?$/) {
$DEBUG=1;
} elsif (/^--?a(ll)?$/) {
$opt_readall=1;
} elsif (/^--?s(ort)?$/) {
$opt_sort=1;
} elsif (/^--?l(ist-only)?$/) {# orwhatever it is called in grep
$opt_l=1;
} elsif (/^--?c(heck)?$/) {
$opt_check=1;
} elsif (/^--?m(time)?$/) {
$opt_mtime=1;
# } elsif (/^--?X(?:XXX(?:=(.*))?)?$/) {
# if (defined $1) {
# $XXX=$1
# } else {
# $XXX=$ARGV[++$i] or usage "missing argument for '$_' option";
# }
} elsif (/^-./) {
usage("Unknown option '$_'\n");
} else {
push @args, $_
}
}
usage unless @args;
usage "it does not make to give both -m and -a" if $opt_readall and $opt_mtime;
my @all;# [unix,filepath,str]
{
#argh, +- copy of code at end of program
my $previous_end;#undef=no previous file; 0= previous file had no end time
sub myprint {
my ($unixtime,$endunixtime,$filepath,@rest)=@_;
if ($opt_sort) {
push @all,[$unixtime,$filepath,(join "",@rest),$endunixtime];
} else {
if ($opt_check) {
my $start= $unixtime;
if (defined $previous_end) {
if ($previous_end) {
if ($start > $previous_end) {
if (($start - $previous_end)> $allowed_hole) {
print " *** hole of ".($start - $previous_end)." seconds ***\n";
}
} else {
if (($previous_end - $start)> $allowed_overlap) {
print " *** overlap of ".($previous_end - $start)." seconds ***\n";
}
}
} else {
print " (cannot check hole/overlap as endtime of previous file is missing)\n";
}
}
my $end= $endunixtime;
$previous_end=$end||0;
}
print @rest;
}
}
}
#use Chj::IO::Command;
use Chj::Parse::Apache::Accesslog -importprefix=>"AL_";
use Chj::xperlfunc;
use Compress::Zlib ;
use Chj::xopen qw(xopen_read);
use Fcntl ':seek';
my $parser=new Chj::Parse::Apache::Accesslog;
sub show_start_end_of {
my($path)=@_;
if ($path=~ /gz$/) {
# zipped, only show start.
# hmmm wildes pipen aufbauen, oder doch einfach nur selber in mem?
my $gz = gzopen($path, "r")
or die "$myname: gzopen('$path'): $!";
unless ($opt_readall) {
my $buf;
$gz->gzread($buf,8192)
or die "$myname: gzread gave error";
$buf=~ /^([^\n]+)\n/
or die "$myname: no lineending seen in the first 8192 chars of file '$path'\n";
$parser->parseline_time($1)
or die "$myname: error parsing first line of file '$path': ".$parser->errmsg."\n";
my $unixstart= $parser->[AL_Unixtime];
my $unixend;
if ($opt_mtime) {
if (my @s= stat $path) {
$unixend=$s[9];
} else {
warn "could not stat '$path': $!";
}
}
my $end= $unixend? "".localtime($unixend) : "";
myprint $unixstart,$unixend,$path,"$path:\t".localtime($unixstart)."\t-\t$end\n";
} else {
my $line;
my ($first,$last);#last needed since line will be clobbered on eof
while(1) {
my $rv=$gz->gzreadline($line) ;
if ($rv<0){
die "$myname: gzreadline gave error ";##
}
if ($rv==0) {
last;
}
if (!defined $first) {
$first=$line;
} else {
$last=$line;
}
}
if (!defined $first) {
die "$myname: no line in file '$path'";
}
$parser->parseline_time($first)
or die "$myname: error parsing first line of file '$path': ".$parser->errmsg.": $first";
my $start= localtime($parser->[AL_Unixtime]);
my $unixstart=$parser->[AL_Unixtime];
my $end;
my $unixend;
if (defined $last) {
$parser->parseline_time($last)
or die "$myname: error parsing last line of file '$path': ".$parser->errmsg.": $last";
$unixend=$parser->[AL_Unixtime];
$end=localtime($parser->[AL_Unixtime]);
} else {
$end="(same, only 1 line in file)";
}
myprint $unixstart,$unixend,$path, "$path:\t$start\t-\t$end\n";
}
} else {
my $f=xopen_read $path;
$parser->parseline_time(scalar $f->xreadline)
or die "$myname: error parsing first line of file '$path': ".$parser->errmsg."\n";
my $start= $parser->[AL_Unixtime];
$f->xseek(0,SEEK_END);
$parser->parseline_time(scalar $f->xreadline_backwards)
or die "$myname: error parsing last line of file '$path': ".$parser->errmsg."\n";
my $end=$parser->[AL_Unixtime];
myprint $start,$end,$path,"$path:\t".localtime($start)."\t-\t".localtime($end)."\n";
}
}
for (@args) {
eval {
show_start_end_of $_
};
if ($@) {
print STDERR $@;
}
}
if ($opt_sort) {
my $previous_end;#undef=no previous file; 0= previous file had no end time
for (sort { $a->[0] cmp $b->[0] } @all) {
if ($opt_check) {
my $start= $_->[0];
if (defined $previous_end) {
if ($previous_end) {
if ($start > $previous_end) {
if (($start - $previous_end)> $allowed_hole) {
print " *** hole of ".($start - $previous_end)." seconds ***\n";
}
} else {
if (($previous_end - $start)> $allowed_overlap) {
print " *** overlap of ".($previous_end - $start)." seconds ***\n";
}
}
} else {
print " (cannot check hole/overlap as endtime of previous file is missing)\n";
}
}
my $end= $_->[3];
$previous_end=$end||0;
}
if ($opt_l) {
print $_->[1],"\n";
} else {
print $_->[2]
}
}
}