首页 > 代码库 > 原创!ngxtop-监控nginx的利器!!!

原创!ngxtop-监控nginx的利器!!!

原创!ngxtop-监控nginx的利器!!!


无论名称还是界面,ngxtop的灵感均源自大名鼎鼎的top命令.ngxtop的功能就是,分析Nginx访问日志文件(以及其他日志文件,比如Apache2日志),

并通过类似top的界面,实时显示分析后所得的结果.你可能吹嘘自己的综合监控工具拥有各种各样的所需功能,但说到简洁性,肯定比不过ngxtop,而简洁性是不可忽视的一大优点.



1. 先安装pip

网址: https://pypi.python.org/packages/source/p/pip/

例如: wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz --no-check-certificate # 由于下载pip是基于https协议的,故需要在wget url后面加上--no-check-certificate,否则不能下载

tar zxvf pip-1.5.6.tar.gz

cd pip-1.5.6

python setup.py install



2. 安装ngxtop

pip install ngxtop



3.使用方法:

cd ${nginx的安装目录}

ngxtop -c conf/nginx.conf #它会显示由Nginx处理的10个请求,按请求数量排序.想显示前20个最频繁的请求,只要运行这个命令:$ ngxtop -n 20



4. 技巧

#如果你愿意,也可以定制所要显示的变量.为此,只要列出你感兴趣的变量."print"命令会显示一个个请求: ngxtop print request http_user_agent remote_addr

#想显示客户机的最常出现的IP地址,只要运行这个命令:ngxtop top remote_addr

#想显示状态代码是404的请求,只要运行这个命令:ngxtop -i ‘status == 404‘ print request status

#除了Nginx外,ngxtop还能够处理其他日志文件,比如Apache访问日志.想监控Apache Web服务器,只要使用这个命令: tail -f /var/log/apache2/access.log | ngxtop -f common





5.附录:

Usage:

ngxtop [options]

ngxtop [options] (print|top|avg|sum) <var>

ngxtop info



Options:

-l <file>, --access-log <file> access log file to parse.

-f <format>, --log-format <format> log format as specify in log_format directive.

--no-follow ngxtop default behavior is to ignore current lines in log

and only watch for new lines as they are written to the access log.

Use this flag to tell ngxtop to process the current content of the access log instead.

-t <seconds>, --interval <seconds> report interval when running in follow mode [default: 2.0]



-g <var>, --group-by <var> group by variable [default: request_path]

-w <var>, --having <expr> having clause [default: 1]

-o <var>, --order-by <var> order of output for default query [default: count]

-n <number>, --limit <number> limit the number of records included in report for top command [default: 10]

-a <exp> ..., --a <exp> ... add exp (must be aggregation exp: sum, avg, min, max, etc.) into output



-v, --verbose more verbose output

-d, --debug print every line and parsed record

-h, --help print this help message.

--version print version information.



Advanced / experimental options:

-c <file>, --config <file> allow ngxtop to parse nginx config file for log format and location.

-i <filter-expression>, --filter <filter-expression> filter in, records satisfied given expression are processed.

-p <filter-expression>, --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase.