web_log plugin: custom log format bugfix + some optimization
Ilya committed
Feb 16, 2017 at 22:34 UTC
8952afde042730a30af390f3df43350a1ba80fe9
1 file changed
+27
-33
python.d/web_log.chart.py
+27
-33
@@ -233,44 +233,38 @@ class Service(LogService):
233
find_regex_return(msg='Custom log: search OK but contains no named subgroups'
234
' (you need to use ?P<subgroup_name>)')
235
else:
236
- basic_values = {'address', 'method', 'url', 'code', 'bytes_sent'} - set(match_dict)
237
-
238
- if basic_values:
236
+ mandatory_dict = {'address': r'[\da-f.:]+',
237
+ 'code': r'[1-9]\d{2}',
238
+ 'method': r'[A-Z]+',
239
+ 'bytes_sent': r'\d+|-'}
240
+ optional_dict = {'resp_length': r'\d+',
241
+ 'resp_time': r'[\d.]+'}
242
+
243
+ mandatory_values = set(mandatory_dict) - set(match_dict)
244
+ if mandatory_values:
245
return find_regex_return(msg='Custom log: search OK but some mandatory keys (%s) are missing'
240
- % list(basic_values))
246
+ % list(mandatory_values))
247
+ else:
248
+ for key in mandatory_dict:
249
+ if not re.search(mandatory_dict[key], match_dict[key]):
250
+ return find_regex_return(msg='Custom log: can\'t parse "%s": %s'
251
+ % (key, match_dict[key]))
252
+
253
+ optional_values = set(optional_dict) & set(match_dict)
254
+ for key in optional_values:
255
+ if not re.search(optional_dict[key], match_dict[key]):
256
+ return find_regex_return(msg='Custom log: can\'t parse "%s": %s'
257
+ % (key, match_dict[key]))
258
+
259
+ dot_in_time = '.' in match_dict.get('resp_time', '')
260
+ if dot_in_time:
261
+ self.resp_time_func = lambda time: time * (resp_time_func or 1000000)
262
else:
242
- if not re.search(r'[\da-f.:]+', match_dict['address']):
243
- return find_regex_return(msg='Custom log: can\'t parse "address": %s'
244
- % match_dict['address'])
245
- if not re.search(r'[1-9]\d{2}', match_dict['code']):
246
- return find_regex_return(msg='Custom log: can\'t parse "code": %s'
247
- % match_dict['code'])
248
- if not re.search(r'[A-Z]+', match_dict['method']):
249
- return find_regex_return(msg='Custom log: can\'t parse "method": %s'
250
- % match_dict['method'])
251
- if not re.search(r'\d+|-', match_dict['bytes_sent']):
252
- return find_regex_return(msg='Custom log: can\'t parse "bytes_sent": %s'
253
- % match_dict['bytes_sent'])
254
-
255
- if 'resp_length' in match_dict:
256
- if not re.search(r'\d+', match_dict['resp_length']):
257
- return find_regex_return(msg='Custom log: can\'t parse "resp_length": %s'
258
- % match_dict['resp_length'])
259
-
260
- if 'resp_time' in match_dict:
261
- if not re.search(r'[\d.]+', match_dict['resp_length']):
262
- return find_regex_return(msg='Custom log: can\'t parse "resp_time": %s'
263
- % match_dict['resp_time'])
264
- else:
265
- if '.' in match_dict['resp_time']:
266
- self.resp_time_func = lambda time: time * (resp_time_func or 1000000)
267
- else:
268
- self.resp_time_func = lambda time: time * (resp_time_func or 1)
263
+ self.resp_time_func = lambda time: time * (resp_time_func or 1)
264
265
self.regex = regex
266
return find_regex_return(match_dict=match_dict,
272
- log_name='web_access',
273
- msg='We are fine')
267
+ log_name='web_access')
268
269
def find_regex(self, last_line):
270
"""