From c8d8be522da780e60b34f81dc85401e7c3b6893f Mon Sep 17 00:00:00 2001 From: takeshixx Date: Tue, 1 Apr 2014 16:10:53 +0200 Subject: [PATCH 1/3] support for 'include' directives added --- ngxtop/config_parser.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ngxtop/config_parser.py b/ngxtop/config_parser.py index 82a2d2a..a4d1bdc 100644 --- a/ngxtop/config_parser.py +++ b/ngxtop/config_parser.py @@ -2,6 +2,7 @@ Nginx config parser and pattern builder. """ import os +import glob import re import subprocess @@ -13,6 +14,7 @@ REGEX_SPECIAL_CHARS = r'([\.\*\+\?\|\(\)\{\}\[\]])' REGEX_LOG_FORMAT_VARIABLE = r'\$([a-z0-9\_]+)' +REGEX_CONFIG_INCLUDES = r'include (.+);' LOG_FORMAT_COMBINED = '$remote_addr - $remote_user [$time_local] ' \ '"$request" $status $body_bytes_sent ' \ '"$http_referer" "$http_user_agent"' @@ -97,6 +99,13 @@ def detect_log_config(arguments): with open(config) as f: config_str = f.read() + + if 'include' in config_str: + inre = re.compile(REGEX_CONFIG_INCLUDES) + for include in inre.findall(config_str): + for filename in glob.glob(include): + config_str += open(filename).read() + access_logs = dict(get_access_logs(config_str)) if not access_logs: error_exit('Access log file is not provided and ngxtop cannot detect it from your config file (%s).' % config) From bd7db7fd3b5bc38feed42a179ed902237667a313 Mon Sep 17 00:00:00 2001 From: takeshixx Date: Wed, 2 Apr 2014 12:19:29 +0200 Subject: [PATCH 2/3] catch exception on missing log format field --- ngxtop/ngxtop.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/ngxtop/ngxtop.py b/ngxtop/ngxtop.py index f1487a5..e185ec7 100755 --- a/ngxtop/ngxtop.py +++ b/ngxtop/ngxtop.py @@ -180,15 +180,19 @@ def to_float(value): def parse_log(lines, pattern): - matches = (pattern.match(l) for l in lines) - records = (m.groupdict() for m in matches if m is not None) - records = map_field('status', to_int, records) - records = add_field('status_type', parse_status_type, records) - records = add_field('bytes_sent', lambda r: r['body_bytes_sent'], records) - records = map_field('bytes_sent', to_int, records) - records = map_field('request_time', to_float, records) - records = add_field('request_path', parse_request_path, records) - return records + try: + matches = (pattern.match(l) for l in lines) + records = (m.groupdict() for m in matches if m is not None) + records = map_field('status', to_int, records) + records = add_field('status_type', parse_status_type, records) + records = add_field('bytes_sent', lambda r: r['body_bytes_sent'], records) + records = map_field('bytes_sent', to_int, records) + records = map_field('request_time', to_float, records) + records = add_field('request_path', parse_request_path, records) + return records + except: + print("Parsing log format failed. Verify that all mandatory fields are in place (e.g. bytes_sent)") + sys.exit(0) # ================================= From 13f86fee0d23b38bf9598f8034402841ffd91749 Mon Sep 17 00:00:00 2001 From: takeshix Date: Fri, 24 Oct 2014 12:21:27 +0200 Subject: [PATCH 3/3] Hint added --- README.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.rst b/README.rst index 2680597..dde6cf1 100644 --- a/README.rst +++ b/README.rst @@ -1,3 +1,5 @@ +FYI: Fork which supports ``include`` directive in Nginx config files. + ================================================================ ``ngxtop`` - **real-time** metrics for nginx server (and others) ================================================================