转:https://www.phpsong.com/3391.html
ngxtop实时监控Nginx工具
Nginx 2017-09-25 10:00:32 722浏览 0评论
今天在微信的公众号上推送的文章里看到介绍ngxtop的文章,感觉这个工具还是挺有用的, ngxtop 从界面和名称都借鉴了著名的top命令,ngxtop 是通过分析nginx的日志文件显示内容的,ngxtop是用python编写的一个工具
安装 ngxtop
安装pip
- yum -y install python-pip
安装ngxtop
- pip install ngxtop
ngxtop命令使用
- Usage:
- ngxtop [options]
- ngxtop [options] (print|top|avg|sum) <var> ...
- ngxtop info
- ngxtop [options] query <query> ...
- Options:
- -l <file>, --access-log <file> access log file to parse.【选择日志文件】
- -f <format>, --log-format <format> log format as specify in log_format directive. [default: combined]
- --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]【类似于mysql分组】
- -w <var>, --having <expr> having clause [default: 1]【类似于mysql having】
- -o <var>, --order-by <var> order of output for default query [default: count]【类似于mysql orderby 排序】
- -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【添加表达式(一般是聚合表达式如: sum, avg, min, max 等)到输出中】
- -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.
- Examples:【下面是使用例子】
- All examples read nginx config file for access log location and format.
- If you want to specify the access log file and / or log format, use the -f and -a options.
- "top" like view of nginx requests【类似于top命令】
- $ ngxtop
- Top 10 requested path with status 404:【显示10条404的请求】
- $ ngxtop top request_path --filter 'status == 404'
- Top 10 requests with highest total bytes sent【显示10条最大流量的请求】
- $ ngxtop --order-by 'avg(bytes_sent) * count'
- Top 10 remote address, e.g., who's hitting you the most【10个地址,可以知道谁在访问你,攻击你】
- $ ngxtop --group-by remote_addr
- Print requests with 4xx or 5xx status, together with status and http referer【显示4XX和5XX的请求】
- $ ngxtop -i 'status >= 400' print request status http_referer
- Average body bytes sent of 200 responses of requested path begin with 'foo':【foo开头200状态码的请求】
- $ ngxtop avg bytes_sent --filter 'status == 200 and request_path.startswith("foo")'
- Analyze apache access log from remote machine using 'common' log format【统计apache日志】
- $ ssh remote tail -f /var/log/apache2/access.log | ngxtop -f common
安装之后默认会自动识别nginx的日志,使用
- ngxtop info