dbce3ca7bef17bad3d789999f380307bfedbd062
[log-merge] / mysql-slow-apache.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5 use HTTP::Date;
6 use POSIX qw(strftime);
7 use Data::Dump qw(dump);
8
9 $|=1;
10
11 my $t;
12
13 sub t_iso {
14         return strftime("%Y-%m-%d %H:%M:%S", localtime($_[0]));
15 }
16
17 open(my $slow_fh, '<', '/var/log/mysql/mysql-slow.log');
18 while(<$slow_fh>) {
19         print $_;
20         if ( m/Time:\s+(\S+)\s+(\S+)/ ) {
21                 $t = str2time("20$1 $2");
22         } elsif ( m/Query_time: (\S+)/ ) {
23                 my $d = $1;
24                 my $t_start = $t - $d;
25                 print "XXX ",t_iso($t), " -> ", t_iso($t_start), $/;
26                 my $test;
27                 foreach ( $t , $t_start, $t_start - 0.5 ) {
28                         $test->{ strftime("%d/%b/%Y:%H:%M:%S", localtime($_)) }++; # apache
29                 }
30                 print "XXX test=",dump($test),$/;
31                 my $patt = join('|', sort keys %$test);
32         
33                 my $cmd = "grep -E '($patt)' /var/log/apache2/other_vhosts_access.log";
34                 print "XXX $cmd\n";
35                 system $cmd;
36 =for xxx
37                 my $date_log = "/var/log/apache2/other_vhosts_access.log-20$1";
38                 if ( -e $date_log ) {
39                         system "grep $2 $date_log";
40                 } else {
41                         system "grep $2 /var/log/apache2/other_vhosts_access.log";
42                 }
43 =cut
44         }
45 }
46
47 __END__
48 #!/bin/sh -xe
49
50 stdbuf -o L cat /var/log/mysql/mysql-slow.log \
51 | stdbuf -e L tee /dev/stderr \
52 | stdbuf -o L grep Time: \
53 | stdbuf -o L awk '{ print "grep --line-buffered " $4 " /var/log/apache2/other_vhosts_access.log-20"$3 ; fflush(stdout); }' | sh
54
55 exit 0
56
57 zgrep Query_time /var/log/mysql/mysql-slow.log*
58
59 exit 0
60
61 grep -E '(Query_time|Time)' /var/log/mysql/mysql-slow.log