statslog 5.36 KB
Newer Older
1
2
3
#!/usr/bin/perl -w
# statslog - Rearrange and output selected parts of slapd's statslog output.
# $OpenLDAP$
4
# This work is part of OpenLDAP Software <http://www.openldap.org/>.
5
#
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
6
# Copyright 1998-2020 The OpenLDAP Foundation.
7
# Portions Copyright 2004 Hallvard B. Furuseth.
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
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted only as authorized by the OpenLDAP
# Public License.
#
# A copy of this license is available in the file LICENSE in the
# top-level directory of the distribution or, alternatively, at
# <http://www.OpenLDAP.org/license.html>.

sub usage {
    die join("", @_, <<'EOM');
Usage: statslog [options] [logfiles; may be .gz or .bz2 files]

  Output selected parts of slapd's statslog output (LDAP request/response
  log to syslog or stderr; loglevel 256), grouping log lines by LDAP
  connection.  Lines with no connection are excluded by default.

Options:
  --brief       -b      Brief output (omit time, host/process name/ID).
  --exclude=RE  -e RE   Exclude connections whose output matches REgexp.
  --include=RE  -i RE   Only include connections matching REgexp.
  --EXCLUDE=RE  -E RE   Case-sensitive '--exclude'.
  --INCLUDE=RE  -I RE   Case-sensitive '--include'.
  --loose       -l      Include "loose" lines (lines with no connection).
  --no-loose    -L RE   Only exclude the "loose" lines that match RE.
  --join        -j      Join the inputs as if they were one big log file.
                        Each file must start where the previous left off.
  --no-join     -J      Do not --join.  (Can be useful with --sort.)
  --sort        -s      Sort input files by age.     Implies --join.
  --trace       -t      Print file names when read.  Implies --no-join.
All --exclude/include options are applied.  Note: --exclude/include are
unreliable without --join/sort for connections spanning several log files.
EOM
}

########################################################################

use bytes;
use strict;
use Getopt::Long;

# Globals
my %conns;			# Hash (connection number -> output)
my @loose;			# Collected output with no connection number

# Command line options
my($brief, @filters, @conditions, $no_loose);
my($join_files, $sort_files, $trace, $getopt_ok);

# Handle --include/INCLUDE/exclude/EXCLUDE options
sub filter_opt {
    my($opt, $regexp) = @_;
    push(@conditions, sprintf('$lines %s /$filters[%d]/om%s',
			      (lc($opt) eq 'include' ? "=~" : "!~"),
			      scalar(@filters),
			      ($opt eq lc($opt) ? "i" : "")));
    push(@filters, $regexp);
}

# Parse options at compile time so some can become constants to optimize away
BEGIN {
    &Getopt::Long::Configure(qw(bundling no_ignore_case));
    $getopt_ok = GetOptions("brief|b"		=> \$brief,
			    "include|i=s"	=> \&filter_opt,
			    "exclude|e=s"	=> \&filter_opt,
			    "INCLUDE|I=s"	=> \&filter_opt,
			    "EXCLUDE|E=s"	=> \&filter_opt,
			    "join|j"		=> \$join_files,
			    "no-join|J"		=> sub { $join_files = 0; },
			    "sort|s"		=> \$sort_files,
			    "loose|l"		=> sub { $no_loose = ".^"; },
			    "no-loose|L=s"	=> \$no_loose,
			    "trace|t"		=> \$trace);
}
usage() unless $getopt_ok;
usage("--trace is incompatible with --join.\n") if $trace && $join_files;

86
$join_files = 1 if !defined($join_files) && $sort_files && !$trace;
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
use constant BRIEF => !!$brief;
use constant LOOSE => defined($no_loose) && ($no_loose eq ".^" ? 2 : 1);

# Build sub out(header, connection number) to output one connection's data
my $out_body = (LOOSE
		? ' if (@loose) { print "\n", @loose; @loose = (); } '
		: '');
$out_body .= ' print "\n", $_[0], $lines; ';
$out_body = " if (" . join("\n && ", @conditions) . ") {\n$out_body\n}"
    if @conditions;
eval <<EOM;
sub out {
    my \$lines = delete(\$conns{\$_[1]});
    $out_body
}
1;
EOM
die $@ if $@;

# Read and output log lines from one file
sub do_file {
    local(@ARGV) = @_;
    my($conn, $line, $act);
    while (<>) {
	if (BRIEF
	    ? (($conn, $line, $act) = /\bconn=(\d+) (\S+ (\S+).*\n)/)
	    : (($conn,        $act) = /\bconn=(\d+) \S+ (\S+)/      )) {
	    $conns{$conn} .= (BRIEF ? $line : $_);
	    out("", $conn) if $act eq 'closed';
	} elsif (LOOSE && (LOOSE > 1 || !/$no_loose/omi)) {
	    s/^\w{3} [ \d]+:\d\d:\d\d [^:]*: // if BRIEF;
	    push(@loose, $_);
	}
    }
    final() unless $join_files;
}

# Output log lines for unfinished connections
sub final {
    if (%conns) {
	for my $conn (sort keys %conns) {
	    out("UNFINISHED:\n", $conn);
	}
	die if %conns;
    }
    if (LOOSE && @loose) { print "\n", @loose; @loose = (); }
}

# Main program
if (!@ARGV) {
    # Read from stdin
    do_file();
} else {
    if ($sort_files && @ARGV > 1) {
	# Sort files by last modified time; oldest first
	my @fileinfo;
	for my $file (@ARGV) {
	    my $age = -M $file;
	    if (defined($age)) {
		push(@fileinfo, [$age, $file]);
	    } else {
		print STDERR "File not found: $file\n";
	    }
	}
	exit(1) unless @fileinfo;
	@ARGV = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @fileinfo;
    }

    # Prepare to pipe .gz, .bz2 and .bz files through gunzip or bunzip2
    my %type2prog = ("gz" => "gunzip", "bz2" => "bunzip2", "bz" => "bunzip2");
    for (@ARGV) {
	if (/\.(gz|bz2?)$/) {
	    my $type = $1;
	    die "Bad filename: $_\n" if /^[+-]|[^\w\/.,:%=+-]|^$/;
	    $_ = "$type2prog{$type} -c $_ |";
	}
    }

    # Process the files
    for my $file (@ARGV) {
	print "\n$file:\n" if $trace;
	do_file($file);
    }
}
final();