|
1 | 1 | #!/usr/bin/perl
|
2 | 2 |
|
3 | 3 | #we do it in perl, since that's available on ubuntu:latest
|
4 |
| -# Results can be picked up by nl.vpro.monitoring.binder.ScriptMeterBinder |
| 4 | + |
| 5 | +=head1 parse_tomcat_access_logs() |
| 6 | +
|
| 7 | +Make an abstract of the tomcat access logs |
| 8 | +
|
| 9 | +Two (optional) positional parameters |
| 10 | +
|
| 11 | +If three parameters: |
| 12 | +- directory |
| 13 | +- max age in minutes |
| 14 | +
|
| 15 | +If one parameter, directory defaults to /data/log |
| 16 | + - max age in minutes |
| 17 | +
|
| 18 | +=cut |
5 | 19 |
|
6 | 20 | use strict;
|
7 | 21 | use warnings;
|
8 | 22 |
|
9 | 23 |
|
10 | 24 | my $dir="/data/logs";
|
11 |
| -my $after = "1 week before now"; |
| 25 | +my $age = "60"; # max age in minutes |
12 | 26 | if (scalar(@ARGV) ge 2) {
|
13 | 27 | $dir = $ARGV[0];
|
14 |
| - $after = $ARGV[1]; |
| 28 | + $age = $ARGV[1]; |
15 | 29 | } elsif (scalar(@ARGV) ge 1) {
|
16 |
| - $after = $ARGV[0]; |
| 30 | + $age = $ARGV[0]; |
17 | 31 | }
|
18 | 32 |
|
19 |
| -my $findcommand="find $dir -name 'tomcat_access.log*'"; |
| 33 | +my $after =`date --iso-8601=minutes --date="\${dataset_date} -$age minute"`; |
| 34 | +my $findcommand="find $dir -cmin -$age -name 'tomcat_access.log*'"; |
| 35 | +my $context=$ENV{CONTEXT}; |
| 36 | +if (! defined($context)) { |
| 37 | + $context="ROOT"; |
| 38 | +} |
| 39 | +my $pathlength=$ENV{ACCESS_LOG_PATH_LENGTH}; |
| 40 | +if (! defined($pathlength)) { |
| 41 | + $pathlength=2; |
| 42 | +} |
20 | 43 | open(FILELIST,"$findcommand |")||die("can't open $findcommand |");
|
21 | 44 | my @filelist=<FILELIST>;
|
22 | 45 | close FILELIST;
|
|
43 | 66 | my $client=(split " ", $full_client)[0];
|
44 | 67 | my $request=$field[7];
|
45 | 68 | my $status=$field[8];
|
| 69 | + $result{"status"}{"status=$status"}++; |
46 | 70 | if ($status ge 300) {
|
47 | 71 | next;
|
48 | 72 | }
|
|
57 | 81 | my @split_full_path=split /\?/, $full_path;
|
58 | 82 | my $path=$split_full_path[0];
|
59 | 83 | my @split_path=split '/', $path;
|
60 |
| - if (scalar(@split_path) < 4) { |
| 84 | + shift @split_path; # path starts with / so first element will be empty string, discard it. |
| 85 | + if ($split_path[0] eq $context) { |
| 86 | + shift @split_path; |
| 87 | + } |
| 88 | + if ($#split_path ge ($pathlength - 1)) { |
| 89 | + $#split_path = $pathlength - 1; |
| 90 | + } |
| 91 | + if (defined($split_path[0]) && $split_path[0] eq 'manage') { |
61 | 92 | next;
|
62 | 93 | }
|
63 |
| - my $api="$split_path[0]/$split_path[1]/$split_path[2]/$split_path[3]"; |
| 94 | + my $short_path=join('/',@split_path); |
64 | 95 |
|
65 | 96 | $result{"clients"}{"client=$client"}++;
|
66 |
| - $result{"methods"}{"method=$method,api=$api"}++; |
67 |
| - $result{"api"}{"api=$api"}++; |
68 |
| - $result{"status"}{"status=$status"}++; |
| 97 | + $result{"paths"}{"method=$method,path=$short_path"}++; |
69 | 98 | }
|
70 | 99 | }
|
71 | 100 |
|
|
75 | 104 | print ("tomcat_access_$name\t$count\t$key\n");
|
76 | 105 | }
|
77 | 106 | }
|
| 107 | + |
0 commit comments