web_log plugin: change algorithm to incremental
Ilya committed
Feb 12, 2017 at 02:04 UTC
058a43693b47366a1b864debfd2cb9620a6d8e63
1 file changed
+62
-69
python.d/web_log.chart.py
+62
-69
@@ -7,7 +7,7 @@ import re
7
import bisect
8
from os import access, R_OK
9
from os.path import getsize
10
-from collections import defaultdict, namedtuple
10
+from collections import namedtuple
11
from copy import deepcopy
12
try:
13
from itertools import zip_longest
@@ -17,37 +17,38 @@ except ImportError:
17
priority = 60000
18
retries = 60
19
20
-ORDER = ['response_codes', 'bandwidth', 'response_time', 'requests_per_url', 'http_method', 'requests_per_ipproto', 'clients', 'clients_all']
20
+ORDER = ['response_codes', 'bandwidth', 'response_time', 'requests_per_url', 'http_method', 'requests_per_ipproto',
21
+ 'clients', 'clients_all']
22
CHARTS = {
23
'response_codes': {
24
'options': [None, 'Response Codes', 'requests/s', 'responses', 'web_log.response_codes', 'stacked'],
25
'lines': [
25
- ['2xx', '2xx', 'absolute'],
26
- ['5xx', '5xx', 'absolute'],
27
- ['3xx', '3xx', 'absolute'],
28
- ['4xx', '4xx', 'absolute'],
29
- ['1xx', '1xx', 'absolute'],
30
- ['0xx', 'other', 'absolute'],
31
- ['unmatched', 'unmatched', 'absolute']
26
+ ['2xx', '2xx', 'incremental'],
27
+ ['5xx', '5xx', 'incremental'],
28
+ ['3xx', '3xx', 'incremental'],
29
+ ['4xx', '4xx', 'incremental'],
30
+ ['1xx', '1xx', 'incremental'],
31
+ ['0xx', 'other', 'incremental'],
32
+ ['unmatched', 'unmatched', 'incremental']
33
]},
34
'bandwidth': {
35
'options': [None, 'Bandwidth', 'KB/s', 'bandwidth', 'web_log.bandwidth', 'area'],
36
'lines': [
36
- ['resp_length', 'received', 'absolute', 1, 1024],
37
- ['bytes_sent', 'sent', 'absolute', -1, 1024]
37
+ ['resp_length', 'received', 'incremental', 1, 1024],
38
+ ['bytes_sent', 'sent', 'incremental', -1, 1024]
39
]},
40
'response_time': {
41
'options': [None, 'Processing Time', 'milliseconds', 'timings', 'web_log.response_time', 'area'],
42
'lines': [
42
- ['resp_time_min', 'min', 'absolute', 1, 1000],
43
- ['resp_time_max', 'max', 'absolute', 1, 1000],
44
- ['resp_time_avg', 'avg', 'absolute', 1, 1000]
43
+ ['resp_time_min', 'min', 'incremental', 1, 1000],
44
+ ['resp_time_max', 'max', 'incremental', 1, 1000],
45
+ ['resp_time_avg', 'avg', 'incremental', 1, 1000]
46
]},
47
'clients': {
48
'options': [None, 'Current Poll Unique Client IPs', 'unique ips', 'clients', 'web_log.clients', 'stacked'],
49
'lines': [
49
- ['unique_cur_ipv4', 'ipv4', 'absolute', 1, 1],
50
- ['unique_cur_ipv6', 'ipv6', 'absolute', 1, 1]
50
+ ['unique_cur_ipv4', 'ipv4', 'incremental', 1, 1],
51
+ ['unique_cur_ipv6', 'ipv6', 'incremental', 1, 1]
52
]},
53
'clients_all': {
54
'options': [None, 'All Time Unique Client IPs', 'unique ips', 'clients', 'web_log.clients_all', 'stacked'],
@@ -60,10 +61,11 @@ CHARTS = {
61
'lines': [
62
]},
63
'requests_per_ipproto': {
63
- 'options': [None, 'Requests Per IP Protocol', 'requests/s', 'ip protocols', 'web_log.requests_per_ipproto', 'stacked'],
64
+ 'options': [None, 'Requests Per IP Protocol', 'requests/s', 'ip protocols', 'web_log.requests_per_ipproto',
65
+ 'stacked'],
66
'lines': [
65
- ['req_ipv4', 'ipv4', 'absolute', 1, 1],
66
- ['req_ipv6', 'ipv6', 'absolute', 1, 1]
67
+ ['req_ipv4', 'ipv4', 'incremental', 1, 1],
68
+ ['req_ipv6', 'ipv6', 'incremental', 1, 1]
69
]}
70
}
71
@@ -87,13 +89,12 @@ class Service(LogService):
89
self.http_method_chart = None # will be assigned in 'create_*_method' method.
90
# sorted list of unique IPs
91
self.unique_all_time = list()
90
- # dict for values that should not be zeroed every poll
91
- self.storage = {'unique_tot_ipv4': 0, 'unique_tot_ipv6': 0}
92
- # if there is no new logs this dict + self.storage returned to netdata
92
+ # if there is no new logs this dict returned to netdata
93
self.data = {'bytes_sent': 0, 'resp_length': 0, 'resp_time_min': 0,
94
'resp_time_max': 0, 'resp_time_avg': 0, 'unique_cur_ipv4': 0,
95
'unique_cur_ipv6': 0, '2xx': 0, '5xx': 0, '3xx': 0, '4xx': 0,
96
- '1xx': 0, '0xx': 0, 'unmatched': 0, 'req_ipv4': 0, 'req_ipv6': 0}
96
+ '1xx': 0, '0xx': 0, 'unmatched': 0, 'req_ipv4': 0, 'req_ipv6': 0,
97
+ 'unique_tot_ipv4': 0, 'unique_tot_ipv6': 0}
98
99
def check(self):
100
if not self.log_path:
@@ -187,8 +188,7 @@ class Service(LogService):
188
:return:
189
Create additional charts depending on the 'find_regex' result (parsed_line) and configuration file
190
1. 'time_response' chart is removed if there is no 'time_response' in logs.
190
- 2. We need to change divisor for 'response_time' chart for apache (time in microseconds in logs)
191
- 3. Other stuff is just remove/add chart depending on yes/no in conf
191
+ 2. Other stuff is just remove/add chart depending on yes/no in conf
192
"""
193
def find_job_name(override_name, name):
194
"""
@@ -224,17 +224,20 @@ class Service(LogService):
224
if self.detailed_response_codes:
225
self.order.append('detailed_response_codes')
226
self.definitions['detailed_response_codes'] = {'options': [None, 'Detailed Response Codes', 'requests/s',
227
- 'responses', 'web_log.detailed_response_codes', 'stacked'],
227
+ 'responses', 'web_log.detailed_response_codes',
228
+ 'stacked'],
229
'lines': []}
230
231
# Add 'requests_per_url' chart if specified in the configuration
232
if self.url_pattern:
232
- self.url_pattern = [NAMED_URL_PATTERN(description=k, pattern=re.compile(v)) for k, v in self.url_pattern.items()]
233
+ self.url_pattern = [NAMED_URL_PATTERN(description=k, pattern=re.compile(v)) for k, v
234
+ in self.url_pattern.items()]
235
self.definitions['requests_per_url'] = {'options': [None, 'Requests Per Url', 'requests/s',
236
'urls', 'web_log.requests_per_url', 'stacked'],
235
- 'lines': [['other_url', 'other', 'absolute']]}
237
+ 'lines': [['other_url', 'other', 'incremental']]}
238
for elem in self.url_pattern:
237
- self.definitions['requests_per_url']['lines'].append([elem.description, elem.description, 'absolute'])
239
+ self.definitions['requests_per_url']['lines'].append([elem.description, elem.description,
240
+ 'incremental'])
241
self.data.update({elem.description: 0})
242
self.data.update({'other_url': 0})
243
else:
@@ -243,7 +246,7 @@ class Service(LogService):
246
def add_new_dimension(self, dimension, line_list, chart_string, key):
247
"""
248
:param dimension: str: response status code. Ex.: '202', '499'
246
- :param line_list: list: Ex.: ['202', '202', 'Absolute']
249
+ :param line_list: list: Ex.: ['202', '202', 'incremental']
250
:param chart_string: Current string we need to pass to netdata to rebuild the chart
251
:param key: str: CHARTS dict key (chart name). Ex.: 'response_time'
252
:return: str: new chart string = previous + new dimensions
@@ -271,10 +274,7 @@ class Service(LogService):
274
275
request_time, unique_current = list(), list()
276
request_counter = {'count': 0, 'sum': 0}
274
- to_netdata = dict()
275
- to_netdata.update(self.data)
276
- default_dict = defaultdict(lambda: 0)
277
-
277
+ ip_address_counter = {'unique_cur_ip': 0}
278
for line in raw:
279
match = self.regex.search(line)
280
if match:
@@ -282,78 +282,72 @@ class Service(LogService):
282
match.groups()))
283
try:
284
code = ''.join([match_dict['code'][0], 'xx'])
285
- to_netdata[code] += 1
285
+ self.data[code] += 1
286
except KeyError:
287
- to_netdata['0xx'] += 1
287
+ self.data['0xx'] += 1
288
# detailed response code
289
if self.detailed_response_codes:
290
- self._get_data_detailed_response_codes(match_dict['code'], default_dict)
290
+ self._get_data_detailed_response_codes(match_dict['code'])
291
# requests per url
292
if self.url_pattern:
293
- self._get_data_per_url(match_dict['url'], default_dict)
293
+ self._get_data_per_url(match_dict['url'])
294
# requests per http method
295
- self._get_data_http_method(match_dict['method'], default_dict)
295
+ self._get_data_http_method(match_dict['method'])
296
# bandwidth sent
297
- to_netdata['bytes_sent'] += int(match_dict['sent'])
297
+ self.data['bytes_sent'] += int(match_dict['sent'])
298
# request processing time and bandwidth received
299
if match_dict['resp_length'] and match_dict['resp_time']:
300
- to_netdata['resp_length'] += int(match_dict['resp_length'])
300
+ self.data['resp_length'] += int(match_dict['resp_length'])
301
resp_time = self.resp_time_func(float(match_dict['resp_time']))
302
bisect.insort_left(request_time, resp_time)
303
request_counter['count'] += 1
304
request_counter['sum'] += resp_time
305
# requests per ip proto
306
proto = 'ipv4' if '.' in match_dict['address'] else 'ipv6'
307
- to_netdata['req_' + proto] += 1
307
+ self.data['req_' + proto] += 1
308
# unique clients ips
309
if address_not_in_pool(self.unique_all_time, match_dict['address'],
310
- self.storage['unique_tot_ipv4'] + self.storage['unique_tot_ipv6']):
311
- self.storage['unique_tot_' + proto] += 1
312
- if address_not_in_pool(unique_current, match_dict['address'],
313
- to_netdata['unique_cur_ipv4'] + to_netdata['unique_cur_ipv6']):
314
- to_netdata['unique_cur_' + proto] += 1
310
+ self.data['unique_tot_ipv4'] + self.data['unique_tot_ipv6']):
311
+ self.data['unique_tot_' + proto] += 1
312
+ if address_not_in_pool(unique_current, match_dict['address'], ip_address_counter['unique_cur_ip']):
313
+ self.data['unique_cur_' + proto] += 1
314
+ ip_address_counter['unique_cur_ip'] += 1
315
else:
316
- to_netdata['unmatched'] += 1
316
+ self.data['unmatched'] += 1
317
# timings
318
if request_time:
319
- to_netdata['resp_time_min'] = request_time[0]
320
- to_netdata['resp_time_avg'] = round(float(request_counter['sum']) / request_counter['count'])
321
- to_netdata['resp_time_max'] = request_time[-1]
322
-
323
- to_netdata.update(self.storage)
324
- to_netdata.update(default_dict)
325
- return to_netdata
319
+ self.data['resp_time_min'] += int(request_time[0])
320
+ self.data['resp_time_avg'] += int(round(float(request_counter['sum']) / request_counter['count']))
321
+ self.data['resp_time_max'] += int(request_time[-1])
322
+ return self.data
323
327
- def _get_data_detailed_response_codes(self, code, default_dict):
324
+ def _get_data_detailed_response_codes(self, code):
325
"""
326
:param code: str: CODE from parsed line. Ex.: '202, '499'
330
- :param default_dict: defaultdict
327
:return:
328
Calls add_new_dimension method If the value is found for the first time
329
"""
330
if code not in self.data:
331
chart_string_copy = self.detailed_chart
336
- self.detailed_chart = self.add_new_dimension(code, [code, code, 'absolute'],
332
+ self.detailed_chart = self.add_new_dimension(code, [code, code, 'incremental'],
333
chart_string_copy, 'detailed_response_codes')
338
- default_dict[code] += 1
334
+ self.data[code] += 1
335
340
- def _get_data_http_method(self, method, default_dict):
336
+ def _get_data_http_method(self, method):
337
"""
338
:param method: str: METHOD from parsed line. Ex.: 'GET', 'POST'
343
- :param default_dict: defaultdict
339
:return:
340
Calls add_new_dimension method If the value is found for the first time
341
"""
342
if method not in self.data:
343
chart_string_copy = self.http_method_chart
349
- self.http_method_chart = self.add_new_dimension(method, [method, method, 'absolute'],
344
+ self.http_method_chart = self.add_new_dimension(method, [method, method, 'incremental'],
345
chart_string_copy, 'http_method')
351
- default_dict[method] += 1
346
+ self.data[method] += 1
347
353
- def _get_data_per_url(self, url, default_dict):
348
+ def _get_data_per_url(self, url):
349
"""
350
:param url: str: URL from parsed line
356
- :param default_dict: defaultdict
351
:return:
352
Scan through string looking for the first location where patterns produce a match for all user
353
defined patterns
@@ -361,11 +355,11 @@ class Service(LogService):
355
match = None
356
for elem in self.url_pattern:
357
if elem.pattern.search(url):
364
- default_dict[elem.description] += 1
358
+ self.data[elem.description] += 1
359
match = True
360
break
361
if not match:
368
- default_dict['other_url'] += 1
362
+ self.data['other_url'] += 1
363
364
365
def address_not_in_pool(pool, address, pool_size):
@@ -373,8 +367,7 @@ def address_not_in_pool(pool, address, pool_size):
367
:param pool: list of ip addresses
368
:param address: ip address
369
:param pool_size: current size of pool
376
- :return: True if address not pool and False address in pool
377
- If address not in pool function add address to pool.
370
+ :return: True if address not in pool. False if address in pool
371
"""
372
index = bisect.bisect_left(pool, address)
373
if index < pool_size: