Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for 'include' directives added #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
FYI: Fork which supports ``include`` directive in Nginx config files.

================================================================
``ngxtop`` - **real-time** metrics for nginx server (and others)
================================================================
Expand Down
9 changes: 9 additions & 0 deletions ngxtop/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Nginx config parser and pattern builder.
"""
import os
import glob
import re
import subprocess

Expand All @@ -13,6 +14,7 @@

REGEX_SPECIAL_CHARS = r'([\.\*\+\?\|\(\)\{\}\[\]])'
REGEX_LOG_FORMAT_VARIABLE = r'\$([a-zA-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"'
Expand Down Expand Up @@ -100,6 +102,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)
Expand Down
22 changes: 13 additions & 9 deletions ngxtop/ngxtop.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,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)


# =================================
Expand Down