2 # statslog - Rearrange and output selected parts of slapd's statslog output.
5 # Copyright 2004 Hallvard B. Furuseth.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted only as authorized by the OpenLDAP
12 # A copy of this license is available in the file LICENSE in the
13 # top-level directory of the distribution or, alternatively, at
14 # <http://www.OpenLDAP.org/license.html>.
17 die join("", @_, <<'EOM');
18 Usage: statslog [options] [logfiles; may be .gz or .bz2 files]
20 Output selected parts of slapd's statslog output (LDAP request/response
21 log to syslog or stderr; loglevel 256), grouping log lines by LDAP
22 connection. Lines with no connection are excluded by default.
25 --brief -b Brief output (omit time, host/process name/ID).
26 --exclude=RE -e RE Exclude connections whose output matches REgexp.
27 --include=RE -i RE Only include connections matching REgexp.
28 --EXCLUDE=RE -E RE Case-sensitive '--exclude'.
29 --INCLUDE=RE -I RE Case-sensitive '--include'.
30 --loose -l Include "loose" lines (lines with no connection).
31 --no-loose -L RE Only exclude the "loose" lines that match RE.
32 --join -j Join the inputs as if they were one big log file.
33 Each file must start where the previous left off.
34 --no-join -J Do not --join. (Can be useful with --sort.)
35 --sort -s Sort input files by age. Implies --join.
36 --trace -t Print file names when read. Implies --no-join.
37 All --exclude/include options are applied. Note: --exclude/include are
38 unreliable without --join/sort for connections spanning several log files.
42 ########################################################################
49 my %conns; # Hash (connection number -> output)
50 my @loose; # Collected output with no connection number
52 # Command line options
53 my($brief, @filters, @conditions, $no_loose);
54 my($join_files, $sort_files, $trace, $getopt_ok);
56 # Handle --include/INCLUDE/exclude/EXCLUDE options
58 my($opt, $regexp) = @_;
59 push(@conditions, sprintf('$lines %s /$filters[%d]/om%s',
60 (lc($opt) eq 'include' ? "=~" : "!~"),
62 ($opt eq lc($opt) ? "i" : "")));
63 push(@filters, $regexp);
66 # Parse options at compile time so some can become constants to optimize away
68 &Getopt::Long::Configure(qw(bundling no_ignore_case));
69 $getopt_ok = GetOptions("brief|b" => \$brief,
70 "include|i=s" => \&filter_opt,
71 "exclude|e=s" => \&filter_opt,
72 "INCLUDE|I=s" => \&filter_opt,
73 "EXCLUDE|E=s" => \&filter_opt,
74 "join|j" => \$join_files,
75 "no-join|J" => sub { $join_files = 0; },
76 "sort|s" => \$sort_files,
77 "loose|l" => sub { $no_loose = ".^"; },
78 "no-loose|L=s" => \$no_loose,
79 "trace|t" => \$trace);
81 usage() unless $getopt_ok;
82 usage("--trace is incompatible with --join.\n") if $trace && $join_files;
84 $join_files = 1 if !defined($join_files) && $sort_files && !$trace;
85 use constant BRIEF => !!$brief;
86 use constant LOOSE => defined($no_loose) && ($no_loose eq ".^" ? 2 : 1);
88 # Build sub out(header, connection number) to output one connection's data
90 ? ' if (@loose) { print "\n", @loose; @loose = (); } '
92 $out_body .= ' print "\n", $_[0], $lines; ';
93 $out_body = " if (" . join("\n && ", @conditions) . ") {\n$out_body\n}"
97 my \$lines = delete(\$conns{\$_[1]});
104 # Read and output log lines from one file
107 my($conn, $line, $act);
110 ? (($conn, $line, $act) = /\bconn=(\d+) (\S+ (\S+).*\n)/)
111 : (($conn, $act) = /\bconn=(\d+) \S+ (\S+)/ )) {
112 $conns{$conn} .= (BRIEF ? $line : $_);
113 out("", $conn) if $act eq 'closed';
114 } elsif (LOOSE && (LOOSE > 1 || !/$no_loose/omi)) {
115 s/^\w{3} [ \d]+:\d\d:\d\d [^:]*: // if BRIEF;
119 final() unless $join_files;
122 # Output log lines for unfinished connections
125 for my $conn (sort keys %conns) {
126 out("UNFINISHED:\n", $conn);
130 if (LOOSE && @loose) { print "\n", @loose; @loose = (); }
138 if ($sort_files && @ARGV > 1) {
139 # Sort files by last modified time; oldest first
141 for my $file (@ARGV) {
144 push(@fileinfo, [$age, $file]);
146 print STDERR "File not found: $file\n";
149 exit(1) unless @fileinfo;
150 @ARGV = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @fileinfo;
153 # Prepare to pipe .gz, .bz2 and .bz files through gunzip or bunzip2
154 my %type2prog = ("gz" => "gunzip", "bz2" => "bunzip2", "bz" => "bunzip2");
156 if (/\.(gz|bz2?)$/) {
158 die "Bad filename: $_\n" if /^[+-]|[^\w\/.,:%=+-]|^$/;
159 $_ = "$type2prog{$type} -c $_ |";
164 for my $file (@ARGV) {
165 print "\n$file:\n" if $trace;