From 4137c7da57e74e624a865e357479e7ec0f699b23 Mon Sep 17 00:00:00 2001 From: "Miguel A. Cortizo" Date: Fri, 28 Mar 2014 15:05:17 +0100 Subject: Enable input from stdin, such as pipe output --- README.rst | 16 +++++++++++++--- ngxtop/ngxtop.py | 14 ++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 099d0bf..d1750b4 100644 --- a/README.rst +++ b/README.rst @@ -1,10 +1,13 @@ -=================================================== -``ngxtop`` - **real-time** metrics for nginx server -=================================================== +================================================================ +``ngxtop`` - **real-time** metrics for nginx server (and others) +================================================================ **ngxtop** parses your nginx access log and outputs useful, ``top``-like, metrics of your nginx server. So you can tell what is happening with your server in real-time. +Can be used also with Apache log files (experimental). In this case, if not log format is specified, 'combined' will be +used. If the script doesn't detect redirections properly you can force it by using the '-s' option. + Installation ------------ @@ -24,6 +27,7 @@ Usage ngxtop [options] ngxtop [options] (print|top|avg|sum) ngxtop info + tail -f /var/log/apache2/access.log | ngxtop [-s] Options: -l , --access-log access log file to parse. @@ -44,6 +48,12 @@ Usage -h, --help print this help message. --version print version information. + Advanced / experimental options: + -c , --config allow ngxtop to parse nginx config file for log format and location. + -i , --filter filter in, records satisfied given expression are processed. + -p , --pre-filter in-filter expression to check in pre-parsing phase. + -s, --from-stdin read lines from stdin. + Samples ------- diff --git a/ngxtop/ngxtop.py b/ngxtop/ngxtop.py index 17af878..12a84c3 100755 --- a/ngxtop/ngxtop.py +++ b/ngxtop/ngxtop.py @@ -29,6 +29,7 @@ Options: -c , --config allow ngxtop to parse nginx config file for log format and location. -i , --filter filter in, records satisfied given expression are processed. -p , --pre-filter in-filter expression to check in pre-parsing phase. + -s, --from-stdin read lines from stdin. Examples: All examples read nginx config file for access log location and format. @@ -377,6 +378,8 @@ def build_source(access_log, arguments): # constructing log source if arguments['--no-follow']: lines = open(access_log) + elif (arguments['--from-stdin'] or not sys.stdin.isatty()): + lines = sys.stdin else: lines = follow(access_log) return lines @@ -403,10 +406,13 @@ def process(arguments): access_log = arguments['--access-log'] log_format = arguments['--log-format'] if access_log is None or log_format is None: - config = arguments['--config'] - if config is None: - config = get_nginx_conf_path() - access_log, log_format = extract_nginx_conf(config, access_log) + if not (arguments['--from-stdin'] or sys.stdin.isatty()): + config = arguments['--config'] + if config is None: + config = get_nginx_conf_path() + access_log, log_format = extract_nginx_conf(config, access_log) + else: + log_format = 'combined' else: config = None logging.info('access_log: %s', access_log) -- cgit v1.2.3