forked from mirekys/collectd-apachelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apachelog.py
executable file
·313 lines (253 loc) · 10.2 KB
/
apachelog.py
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
#!/usr/bin/env python
""" A collectd-python plugin for retrieving
metrics from Apache access log file. """
import sys
import subprocess
from os import access, R_OK
from threading import Thread
from datetime import datetime
from Queue import Queue, Empty
from apache_log_parser import make_parser, LineDoesntMatchException
# Dictionary of functions to be mapped to each key's value
FLDMAP = {
'response_bytes_clf': (lambda x: str(x).replace('-', '0'))
}
def remap(key, value):
""" Returns value transformed by accorging mapping table entry """
try:
return FLDMAP[key](value)
except KeyError:
return value
class CollectdPlugin(object):
""" Main plugin class """
def __init__(self, debug=False):
self.debug_mode = debug
self.plugin_name = 'python'
def configure(self, conf):
""" Receive and process configuration block from collectd """
raise NotImplementedError
def read(self):
""" Read plugin values and call submit """
raise NotImplementedError
def submit(self, type, instance, value):
""" Send collected values to collectd """
if self.debug_mode:
print('[%s] %s/%s-%s=%s' % (
str(datetime.now()), self.plugin_name, type, instance, value))
else:
cval = collectd.Values()
cval.plugin = self.plugin_name
cval.type = type
cval.type_instance = instance
cval.values = [value, ]
try:
cval.dispatch()
except TypeError:
pass
def debug(self, message):
""" Log a debug message to console """
if self.debug_mode:
print('%s:DBG %s' % (self.plugin_name, message))
def warn(self, message):
""" Log a warning message """
fmsg = '%s:WRN %s' % (self.plugin_name, message)
if not self.debug_mode:
collectd.warning(fmsg)
else:
print(fmsg)
def err(self, message):
""" Log an error message """
fmsg = '%s:ERR %s' % (self.plugin_name, message)
if not self.debug_mode:
collectd.error(fmsg)
else:
print(fmsg)
class LogWatch(Thread):
""" Thread watching a log file for appended lines """
def __init__(self, logfile, line_queue):
Thread.__init__(self)
self.killed = False
self.logfile = logfile
self.line_queue = line_queue
def run(self):
""" Main thread entrypoint """
self.tail()
def tail(self):
""" Watch for, and enqueue lines appended to a file """
tail = subprocess.Popen(
["tail", "-f", self.logfile], stdout=subprocess.PIPE)
while not self.killed:
line = tail.stdout.readline()
self.line_queue.put(line)
if not line:
break
class ApacheLog(CollectdPlugin):
""" Main plugin class """
def __init__(self, debug=False):
""" Initialize itself with sane defaults """
super(ApacheLog, self).__init__(debug)
self.parser = None
self.values = {}
self.logwatch = None
self.interval = 1
self.access_log = '/var/log/ssl_access.log'
self.access_log_format = '%h %l %u %t \"%r\" %>s %b'
self.line_buffer = Queue()
def configure(self, conf):
""" Receive and process configuration block from collectd """
for node in conf.children:
key = node.key.lower()
val = node.values[0]
if key == 'accesslog':
self.access_log = val
if not access(self.access_log, R_OK):
self.err('AccessLog %s is not readable!' % self.access_log)
elif key == 'accesslogformat':
self.access_log_format = val
try:
self.parser = make_parser(self.access_log_format)
except LineDoesntMatchException:
self.err('Couldn\'t parse AccessLogFormat: %s' % (
self.access_log_format))
return
elif key == 'name':
self.plugin_name = val
elif key == 'interval':
self.interval = val
else:
self.warn('Unknown config key: %s.' % key)
def init(self):
""" Prepare itself for reading in new values """
if not self.logwatch:
self.logwatch = LogWatch(self.access_log, self.line_buffer)
self.logwatch.start()
for key, val in self.values.iteritems():
for subkey in val.keys():
self.values[key][subkey] = 0 if subkey != 'time_us' else []
self.values['response_time'] = { 'avg':[], 'min':sys.maxint, 'max':0 }
def gather_metrics(self):
""" Gather metrics data from lines queued by self.logwatch """
read_start = datetime.now()
while (self.logwatch.isAlive() and
((datetime.now()-read_start).seconds) < self.interval):
try:
line = self.line_buffer.get_nowait()
except Empty:
break
try:
request = self.parser(line)
method = request['request_method']
status = 'status_%sxx' % request['status'][:len(request['status'])-2]
self.debug(datetime.now())
self.debug(line)
# Update request count by method and HTTP status
try:
self.values[method]['count'] += 1
except KeyError:
# Initialize values dict for method with request fields
self.values[method] = {
k:0 for k in request.keys() if not k in ['time_us', 'status']
}
if 'time_us' in request.keys():
self.values[method]['time_us'] = []
self.values[method]['count'] = 1
try:
self.values[method][status] += 1
except KeyError:
self.values[method][status] = 1
# Read and save values from request
for key, val in request.iteritems():
if key == 'status':
continue # Status has been already processed
val = remap(key, val)
try:
if key == 'time_us':
self.update_response_time(method, int(val)/1000)
else:
self.values[method][key] += int(val)
except TypeError:
pass
except ValueError:
pass # Ignore values that cannot be converted to int
# Continue to the next line if the current one cannot be parsed.
except LineDoesntMatchException as e:
self.err(e)
def update_response_time(self, method, val):
cmax = self.values['response_time']['max']
cmin = self.values['response_time']['min']
self.values['response_time']['max'] = val if val >= cmax else cmax
self.values['response_time']['min'] = val if val <= cmin else cmin
self.values['response_time']['avg'].append(val)
self.values[method]['time_us'].append(val)
def get_avg_response_time(self, times_list):
avg = 0
try:
avg = sum(times_list)/float(len(times_list))
except ZeroDivisionError:
pass
return avg
def read(self):
""" Collectd read callback to gather metrics
data from the access log and submit them """
self.init()
self.gather_metrics()
self.debug('=========DONE READING. WRITING METRICS...==============')
# Submit response times
rt = self.values['response_time']
self.submit('response_time', 'avg', self.get_avg_response_time(rt['avg']))
self.submit('response_time', 'min', rt['min'] if rt['min'] != sys.maxint else 0)
self.submit('response_time', 'max', rt['max'])
# Submit hit counts
hits = sum([val['count'] if 'count' in val.keys() else 0 for val in self.values.values()])
self.submit('count', 'hits', hits)
# Submit all remaining values
for method, data in self.values.iteritems():
for key, val in data.iteritems():
if 'count' in key:
self.submit('count', method, data['count'])
elif 'bytes' in key:
self.submit('bytes', '%s-%s' % (method, key), val)
elif 'num' in key or 'status' in key:
self.submit('count', '%s-%s' % (method, key), val)
elif 'time_us' in key:
self.submit('response_time', '%s-avg' % method, self.get_avg_response_time(val))
def shutdown(self):
""" Collectd plugin shutdown callback """
self.logwatch.killed = True
self.logwatch.join(1)
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
print('<Debugging Mode ON>')
class NodeMock(object):
""" Immitates single configuration item """
def __init__(self, key, value):
self.key = key
self.values = [value]
class ConfigMock(object):
""" Immitates class passed in by collectd """
def __init__(self, name, intvl, acclog, acclog_fmt):
self.children = []
self.children.append(NodeMock('name', name))
self.children.append(NodeMock('interval', intvl))
self.children.append(NodeMock('accesslog', acclog))
self.children.append(NodeMock('accesslogformat', acclog_fmt))
from time import sleep
sleep_time = 1
cfg = ConfigMock(
'serverX_requests', sleep_time,
'/etc/httpd/logs/ssl_access.log',
'%h %l %u %t \"%r\" %>s %b \"%{Referer}i\"'\
' \"%{User-Agent}i\" %k %I %O %D')
alog = ApacheLog(debug=True)
alog.configure(cfg)
try:
while True:
alog.read()
sleep(sleep_time)
except KeyboardInterrupt:
alog.shutdown()
else:
import collectd
alog = ApacheLog()
collectd.register_config(alog.configure)
collectd.register_read(alog.read)
collectd.register_shutdown(alog.shutdown)