source: people/joerg.steffens/technical/ldap-stats/ldap-stats.mutli-date-formats.diff@ 1268

Last change on this file since 1268 was 1145, checked in by joergs, on Apr 17, 2013 at 1:03:03 PM

initial, multi date formats

File size: 5.5 KB
RevLine 
[1145]1--- ldap-stats.pl.orig 2007-04-28 05:25:34.000000000 +0200
2+++ ldap-stats.pl 2013-04-17 12:53:18.000000000 +0200
3@@ -10,6 +10,8 @@
4 #
5 # Revision History:
6 #
7+# - add support for rsyslog log format -- Joerg Steffens, dass II GmbH
8+#
9 # Version 5.2
10 # Perl::Tidy and Perl::Critic -- Gavin Henry, Suretec Systems Ltd.
11 #
12@@ -315,10 +317,18 @@
13 FIELD => '%4s',
14 };
15
16+# slapd log file can use different date formats:
17+# rsyslog: 2013-04-17T10:45:15.576386+02:00
18+# syslog: Mar 23 09:54:34
19+# We check the first lone of the log file
20+# and use the first date regex that matches.
21+my @date_formats=('\d\d\d\d-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.\d\d\d\d\d\d\+\d\d:\d\d', '(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)');
22+my $regex_date;
23+
24 ###################################################
25 ### Open the logfile and process all of the entries
26 ###################################################
27-for my $file (@ARGV) {
28+FILE: for my $file (@ARGV) {
29 $logfile = $file;
30 my $lines = 0;
31
32@@ -359,11 +369,25 @@
33
34 while ( my $line = <LOGFILE> ) {
35
36- ### check start and end dates
37- if ( $line =~ /^(\w+\s+\d+\s+\d+:\d+:\d+)/mx ) {
38- if ( !$logarray{$logfile}{SDATE} ) {
39- $logarray{$logfile}{SDATE} = $1;
40+ # first line
41+ if ( !$logarray{$logfile}{SDATE} ) {
42+ # check date format in logfile
43+ for my $regex (@date_formats) {
44+ print "regex: $regex\n";
45+ if ( $line =~ /^($regex)/mx ) {
46+ $logarray{$logfile}{SDATE} = $1;
47+ $regex_date = $regex;
48+ last;
49+ }
50 }
51+ if( ! $regex_date ) {
52+ print "ERROR: unable to determine date format in '$logfile'\n";
53+ next FILE;
54+ }
55+ }
56+
57+ ### check start and end dates
58+ if ( $line =~ /^($regex_date)/mx ) {
59 $logarray{$logfile}{EDATE} = $1;
60 }
61
62@@ -375,7 +399,7 @@
63
64 ### Check for a new connection
65 if ( $line =~
66-/^(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+).*conn=(\d+) [ ] fd=\d+ [ ] (?:ACCEPT|connection) [ ] from/mx
67+/^$regex_date .*conn=(\d+) [ ] fd=\d+ [ ] (?:ACCEPT|connection) [ ] from/mx
68 )
69 {
70 my $month = $1;
71@@ -483,13 +507,13 @@
72 ### Check for anonymous binds
73 }
74 elsif ( $line =~
75-/^(\w+)\s+(\d+)\s+(\d+):\d+:\d+.*conn=(\d+) [ ] op=\d+ [ ] BIND [ ] dn="" [ ] method=128/mx
76+/^$regex_date .*conn=(\d+) [ ] op=\d+ [ ] BIND [ ] dn="" [ ] method=128/mx
77 )
78 {
79 my $month = $1;
80 my $day = $2;
81 my $hour = $3;
82- my $conn = $4;
83+ my $conn = $6;
84
85 ### Increment the counters
86 if ( defined $conns{$conn}
87@@ -508,14 +532,14 @@
88 ### Check for non-anonymous binds
89 }
90 elsif ( $line =~
91-/^(\w+)\s+(\d+)\s+(\d+):\d+:\d+.*conn=(\d+) [ ] op=\d+ [ ] BIND [ ] dn="([^"]+)" [ ] mech=/mx
92+/^$regex_date .*conn=(\d+) [ ] op=\d+ [ ] BIND [ ] dn="([^"]+)" [ ] mech=/mx
93 )
94 {
95 my $month = $1;
96 my $day = $2;
97 my $hour = $3;
98- my $conn = $4;
99- my $binddn = lc $5;
100+ my $conn = $6;
101+ my $binddn = lc $7;
102
103 ### Increment the counters
104 if ( defined $conns{$conn}
105@@ -566,13 +590,13 @@
106 ### Check for SEARCHES
107 }
108 elsif ( $line =~
109-/^(\w+)\s+(\d+)\s+(\d+):\d+:\d+.*conn=(\d+) [ ] op=\d+ [ ] SEARCH [ ] RESULT/mx
110+/^$regex_date .*conn=(\d+) [ ] op=\d+ [ ] SEARCH [ ] RESULT/mx
111 )
112 {
113 my $month = $1;
114 my $day = $2;
115 my $hour = $3;
116- my $conn = $4;
117+ my $conn = $6;
118
119 ### Increment the counters
120 if ( defined $conns{$conn}
121@@ -588,13 +612,13 @@
122 ### Check for unbinds
123 }
124 elsif ( $line =~
125- /^(\w+)\s+(\d+)\s+(\d+):\d+:\d+.*conn=(\d+) [ ] op=\d+ [ ] UNBIND/mx
126+ /^$regex_date .*conn=(\d+) [ ] op=\d+ [ ] UNBIND/mx
127 )
128 {
129 my $month = $1;
130 my $day = $2;
131 my $hour = $3;
132- my $conn = $4;
133+ my $conn = $6;
134
135 ### Increment the counters
136 if ( defined $conns{$conn}
137@@ -611,13 +635,13 @@
138 ### TODO: Add other err=X values from contrib/ldapc++/src/LDAPResult.h
139 }
140 elsif ( $line =~
141-/^(\w+)\s+(\d+)\s+(\d+):\d+:\d+.*conn=(\d+) [ ] op=\d+(?: SEARCH)? [ ] RESULT [ ]/mx
142+/^$regex_date .*conn=(\d+) [ ] op=\d+(?: SEARCH)? [ ] RESULT [ ]/mx
143 )
144 {
145 my $month = $1;
146 my $day = $2;
147 my $hour = $3;
148- my $conn = $4;
149+ my $conn = $6;
150
151 if ( $line =~ /\berr=49\b/mx ) {
152 ### Increment the counters
153@@ -635,14 +659,14 @@
154 ### Check for entry changes: add, modify modrdn, delete
155 }
156 elsif ( $line =~
157-/^(\w+)\s+(\d+)\s+(\d+):\d+:\d+.*conn=(\d+) [ ] op=\d+ [ ] (ADD|CMP|MOD|MODRDN|DEL) [ ] dn=/mx
158+/^$regex_date .*conn=(\d+) [ ] op=\d+ [ ] (ADD|CMP|MOD|MODRDN|DEL) [ ] dn=/mx
159 )
160 {
161 my $month = $1;
162 my $day = $2;
163 my $hour = $3;
164- my $conn = $4;
165- my $type = $5;
166+ my $conn = $6;
167+ my $type = $7;
168
169 ### Increment the counters
170 if ( defined $conns{$conn}
Note: See TracBrowser for help on using the repository browser.