-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlograte
executable file
·346 lines (312 loc) · 8.45 KB
/
lograte
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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
# encoding: utf-8
"""
lograte.py
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License"). You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at
# http://forgerock.org/license/CDDLv1.0.html.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at
# trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
# add the following below this CDDL HEADER, with the fields enclosed
# by brackets "[]" replaced with your own identifying information:
# Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2018 ForgeRock AS.
Created by Ludovic Poitou on 2018-06-01.
This program reads OpenDJ access logs and output statistics about operation throughput.
"""
import sys
import getopt
import re
import string
import datetime
help_message = '''
Usage: lograte [options] file [file ...]
options:
\t --stats / -s operation(s) : specifies which operations to compute stat for
\t --output / -o output : specifies the output file, otherwise stdout is used
\t --graph / -g : generate csv output to import in spreadsheet and graph
\t -r : include replicated operations
\t -c : access log in combined mode (single line per operation)
\t -v : verbose mode
'''
class OpStat():
def __init__(self, type):
self.type = type
self.totalcount = long(0)
self.currentcount = long(0)
self.max = long(0)
def inc(self):
self.currentcount += 1
def summarise(self):
self.totalcount += self.currentcount
if self.max < self.currentcount:
self.max = self.currentcount
self.currentcount = long(0)
def printCurrentStats(self, outfile):
if self.currentcount != 0:
outfile.write(self.type + ":\t" + str(self.currentcount) + "\n")
def printStats(self, outfile, duration):
if duration > 0:
outfile.write(self.type + "\tAvg: " + str(self.totalcount / duration) + "\tMax: " + str(self.max) + "\n")
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
output = ""
ops= ""
includeReplOps = False
doSearch = True
doAdd = True
doBind = True
doCompare = True
doDelete = True
doExtended = True
doModify = True
doModDN = True
doAbandon = True
isCombined = False
isCSV = False
IDs = {}
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "cgho:rs:v", ["help", "output=", "combined", "stats"])
except getopt.error, msg:
raise Usage(msg)
# option processing
for option, value in opts:
if option == "-v":
verbose = True
if option == "-r":
includeReplOps = True
if option in ("-c", "--combined"):
isCombined = True
if option in ("-g", "--graph"):
isCSV = True
if option in ("-h", "--help"):
raise Usage(help_message)
if option in ("-o", "--output"):
output = value
if option in ("-s", "--stats"):
ops = value
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, "\t for help use --help"
return 2
if output != "":
try:
outfile = open(output, "w")
except Usage, err:
print >> sys.stderr, "Can't open output file: " + str(err.msg)
else:
outfile = sys.stdout
if ops != "":
doSearch = False
doAdd = False
doBind = False
doCompare = False
doDelete = False
doExtended = False
doModify = False
doModDN = False
doAbandon = False
opers = ops.split(',')
for op in opers:
if op == "Search":
doSearch = True
continue;
if op == "Add":
doAdd = True
continue
if op == "Bind":
doBind = True
continue
if op == "Compare":
doCompare = True
continue
if op == "Delete":
doDelete = True
continue
if op == "Extended":
doExtended = True
continue
if op == "Modify":
doModify = True
continue
if op == "ModDN":
doModDN = True
continue
if op == "Abandon":
doAbandon = true
continue
print >> sys.stderr, "Invalid op name in stats: " + op +", ignored"
searches = OpStat("Search")
adds = OpStat("Add")
binds = OpStat("Bind")
compares = OpStat("Compare")
deletes = OpStat("Delete")
extops = OpStat("Extend")
modifies = OpStat("Modify")
moddns = OpStat("ModDN")
abandons = OpStat("Abandon")
searchTag = "SEARCH RES"
addTag = "ADD RES"
bindTag = "BIND RES"
compareTag = "COMPARE RES"
deleteTag = "DELETE RES"
extopTag = "EXTENDED RES"
modifyTag = "MODIFY RES"
moddnTag = "MODDN RES"
abandonTag = "ABANDON RES"
if isCombined:
searchTag = "SEARCH "
addTag = "ADD "
bindTag = "BIND "
compareTag = "COMPARE "
deleteTag = "DELETE "
extopTag = "EXTENDED "
modifyTag = "MODIFY "
moddnTag = "MODDN "
abandonTag = "ABANDON "
lastdate = ""
firstdate = ""
date = ""
countseconds = long(0)
if isCSV:
# Write the first header line:
outfile.write("Time")
if doAdd:
outfile.write(",Add")
if doBind:
outfile.write(",Bind")
if doCompare:
outfile.write(",Compare")
if doDelete:
outfile.write(",Delete")
if doExtended:
outfile.write(",Extended")
if doModify:
outfile.write(",Modify")
if doModDN:
outfile.write(",ModDN")
if doSearch:
outfile.write(",Search")
if doAbandon:
outfile.write(",Abandon")
outfile.write("\n")
for logfile in args:
try:
infile = open(logfile, "r")
except err:
print >> sys.stderr, "Can't open file: " + str(err.msg)
if not isCSV:
outfile.write("processing file: "+ logfile + "\n")
for i in infile:
m = re.match("^\[(.*) \+\d\d\d\d\] ", i)
if m:
date = m.group(1)
else:
print >> sys.stderr, "Date parsing error on record \"" + i + "\n"
if firstdate == "":
firstdate = date
else:
if date != lastdate:
countseconds += 1
if isCSV:
outfile.write(date)
if doAdd:
outfile.write("," + str(adds.currentcount))
if doBind:
outfile.write("," + str(binds.currentcount))
if doCompare:
outfile.write("," + str(compares.currentcount))
if doDelete:
outfile.write("," + str(deletes.currentcount))
if doExtended:
outfile.write("," + str(extops.currentcount))
if doModify:
outfile.write("," + str(modifies.currentcount))
if doModDN:
outfile.write("," + str(moddns.currentcount))
if doSearch:
outfile.write("," + str(searches.currentcount))
if doAbandon:
outfile.write("," + str(abandons.currentcount))
outfile.write("\n")
adds.summarise()
binds.summarise()
compares.summarise()
deletes.summarise()
extops.summarise()
modifies.summarise()
moddns.summarise()
abandons.summarise()
searches.summarise()
lastdate = date
if re.search(" conn=-1 ", i) and not includeReplOps:
continue
if doSearch and re.search(searchTag, i):
searches.inc()
if doAdd and re.search(addTag, i):
adds.inc()
if doBind and re.search(bindTag, i):
binds.inc()
if doCompare and re.search(compareTag, i):
compares.inc()
if doDelete and re.search(deleteTag, i):
deletes.inc()
if doExtended and re.search(extopTag, i):
extops.inc()
if doModify and re.search(modifyTag, i):
modifies.inc()
if doModDN and re.search(moddnTag, i):
moddns.inc()
if doAbandon and re.search(abandonTag, i):
abandons.inc()
# Done processing that file, lets move to next one
infile.close()
# We're done with all files. Proceed with displaying stats
myformat = "%d/%b/%Y:%H:%M:%S"
startdt = datetime.datetime.strptime(firstdate, myformat)
enddt = datetime.datetime.strptime(date, myformat)
delta = enddt - startdt
duration = delta.total_seconds()
if not isCSV:
outfile.write("Total duration " + str(duration) + ", counted intervals: " + str(countseconds) + "\n")
if doAdd:
adds.printStats(outfile, duration)
if doBind:
binds.printStats(outfile, duration)
if doCompare:
compares.printStats(outfile, duration)
if doDelete:
deletes.printStats(outfile, duration)
if doExtended:
extops.printStats(outfile, duration)
if doModify:
modifies.printStats(outfile, duration)
if doModDN:
moddns.printStats(outfile, duration)
if doSearch:
searches.printStats(outfile, duration)
if doAbandon:
abandons.printStats(outfile, duration)
outfile.write("Done\n")
outfile.close()
if __name__ == "__main__":
sys.exit(main())