-
Notifications
You must be signed in to change notification settings - Fork 20
/
analyze-filters.pl
executable file
·98 lines (84 loc) · 2.82 KB
/
analyze-filters.pl
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
#!/usr/bin/perl
# Program: Analyze filters in OpenLDAP logs <analyze-filters.pl>
#
# Source code home: https://github.com/ltb-project/ldap-scripts/analyze-filters.pl
#
# Author: LDAP Tool Box project
# Author: David Coutadeur <[email protected]>
#
# Current Version: 1
#
# Purpose:
# Display the number of occurrences for each type of filter in OpenLDAP logs
# Mainly used for index tuning
#
# License:
#
# 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>.
#
# Installation:
# 1. Enable a minimum of 'loglevel 256' in OpenLDAP configuration
# 2. Copy the perl script to a suitable location.
# 3. Refer to the usage section for options and examples.
#
# Usage:
# ./analyze-filters.pl slapd.log
#
use strict;
use warnings;
use Data::Dumper;
# Function replacing static values by the tag <value>
# don't replace * in the values
sub format_value
{
my $value = shift;
$value =~ s/[^*]+/<value>/g;
return $value;
}
# Get file from arguments passed to script
unless( @ARGV)
{
print "missing file to analyze\n";
exit 1;
}
my $full_filters; # { "full_filter" => occurrence }
my $comp_filters; # { "component_filter" => occurrence }
foreach my $file (@ARGV)
{
print "Analyze file $file\n";
open(my $fh, "<", "$file") or die "Can't open < $file: $!";
while(my $line = <$fh>)
{
if( $line =~ /filter="([^"]+)"/ )
{
my $full_filter = "$1";
my $comp_filter = "$1";
# Compute full filter
$full_filter =~ s/\(([^=(]+)=([^)]+)\)/"($1=" . &format_value("$2") . ")"/eg;
$full_filters->{$full_filter}++;
# Compute components of filter
while ($comp_filter =~ /\(([^=(]+)=([^)]+)\)/g) {
$comp_filters->{"($1=" . &format_value("$2") . ")"}++;
}
}
}
}
# Print table of full_filters, ordered by occurrences
print "| Occurrences | Full filters |\n";
print "+-------------+----------------------------------------------------------------+\n";
foreach my $filter (sort {$full_filters->{$b} <=> $full_filters->{$a}} keys %$full_filters) {
print sprintf "|%12s | %62s |\n", $full_filters->{$filter}, $filter;
}
# Print table of filter components, ordered by occurrences
print "\n";
print "| Occurrences | Filter components |\n";
print "+-------------+----------------------------------------------------------------+\n";
foreach my $filter (sort {$comp_filters->{$b} <=> $comp_filters->{$a}} keys %$comp_filters) {
print sprintf "|%12s | %62s |\n", $comp_filters->{$filter}, $filter;
}