HomeLinuxGiám sát NGINX log theo thời gian thực bằng Ngxtop trên CentOS 7

Giám sát NGINX log theo thời gian thực bằng Ngxtop trên CentOS 7

by Quốc Cường
18 views

Công cụ Ngxtop trên CentOS 7 tương tự như lệnh top của Linux giúp giám sát hoạt động log của NGINX một cách dễ dàng hơn cho người dùng.

Ngxtop là gì?

Ngxtop là công cụ được viết bằng ngôn ngữ python phân phối dưới dạng mã nguồn mở và hoàn toàn miễn phí. Ngxtop tương tự như lệnh top của hệ điều hành Linux mục đích là để phân tích cú pháp access log của NGINX và xuất ra các thông số theo thời gian thực giúp cung cấp 1 cái mình trực quan hơn cho quản trị viên hệ thống.

Tác giả của bộ công cụ này là người Việt Nam đó nha các bạn, nào chúng ta cùng xem công cụ này có gì nhé.

Cài đặt ngxtop trên CentOS 7

Trước tiên, bạn cần cài đặt pip nếu chưa có:

yum install python-pip -y

Cài đặt ngxtop từ pip

pip install ngxtop

Hoặc có thể clone từ Github

git clone https://github.com/lebinh/ngxtop.git

Kiểm tra lại phiên bản cài đặt bằng cách chạy lệnh: ngxtop --version

[root@systuts ~]# ngxtop --version
xstat 0.1

Sử dụng ngxtop trên CentOS 7

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.

Mặc định khi chạy ngxtop mà không đi kèm với các tùy chọn nào ngxtop sẽ đọc file log từ đường dẫn log mặc định của NGINX là /var/log/nginx/access.log.

ngxtop
running for 411 seconds, 64332 records processed: 156.60 req/sec

Summary:
|   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|---------+------------------+-------+-------+-------+-------|
|   64332 |         2775.251 | 61262 |  2994 |    71 |     5 |

Detailed:
| request_path                             |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|------------------------------------------+---------+------------------+-------+-------+-------+-------|
| /abc/xyz/xxxx                            |   20946 |          434.693 | 20935 |     0 |    11 |     0 |
| /xxxxx.json                              |    5633 |         1483.723 |  5633 |     0 |     0 |     0 |
| /xxxxx/xxx/xxxxxxxxxxxxx                 |    3629 |         6835.499 |  3626 |     0 |     3 |     0 |
| /xxxxx/xxx/xxxxxxxx                      |    3627 |        15971.885 |  3623 |     0 |     4 |     0 |
| /xxxxx/xxx/xxxxxxx                       |    3624 |         7830.236 |  3621 |     0 |     3 |     0 |
| /static/js/minified/utils.min.js         |    3031 |         1781.155 |  2104 |   927 |     0 |     0 |
| /static/js/minified/xxxxxxx.min.v1.js    |    2889 |         2210.235 |  2068 |   821 |     0 |     0 |
| /static/tracking/js/xxxxxxxx.js          |    2594 |         1325.681 |  1927 |   667 |     0 |     0 |
| /xxxxx/xxx.html                          |    2521 |          573.597 |  2520 |     0 |     1 |     0 |
| /xxxxx/xxxx.json                         |    1840 |          800.542 |  1839 |     0 |     1 |     0 |

Phân tích access.log với các đường dẫn tùy chỉnh

Cú pháp: ngxtop -l [đường_dẫn_access_log], ví dụ:

ngxtop -l /home/domain/logs/access.log

Trong ví dụ này, đường dẫn của tôi là /home/domain/logs/access.log, bạn cần thay thế đường dẫn đúng với cấu hình của bạn.

Giám Sát NGINX Log Theo Thời Gian Thực Bằng Ngxtop Trên CentOS 7

Hiển thị top 10 địa chỉ IP nguồn kết nối đến bạn

ngxtop --group-by remote_addr -l /home/domain/logs/access.log
Giám Sát NGINX Log Theo Thời Gian Thực Bằng Ngxtop Trên CentOS 7

Liệt kệ các HTTP Response với status 404

Các bạn cũng có thể thay đổi status code 404 thành các status khác 4XX hoặc 5XX…

ngxtop top request_path --filter 'status == 404' -l /home/domain/logs/access.log
Giám Sát NGINX Log Theo Thời Gian Thực Bằng Ngxtop Trên CentOS 7

Top 10 request với tổng số bytes gửi cao nhất.

ngxtop --order-by 'avg(bytes_sent) * count' -l /home/domain/logs/access.log
Giám Sát NGINX Log Theo Thời Gian Thực Bằng Ngxtop Trên CentOS 7

Giám sát log của Apache

Ngoài ra bộ công cụ này còn có thể phân tích log của Apache thông qua remote ssh bằng ngxtop trên CentOS 7, cú pháp như sau:

# ssh user@remote_server tail -f /var/log/apache2/access.log | ngxtop -f common
running for 20 seconds, 1068 records processed: 53.01 req/sec

Summary:
|   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|---------+------------------+-------+-------+-------+-------|
|    1068 |        28026.763 |  1029 |    20 |    19 |     0 |

Detailed:
| request_path                             |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|------------------------------------------+---------+------------------+-------+-------+-------+-------|
| /xxxxxxxxxx                              |     199 |        55150.402 |   199 |     0 |     0 |     0 |
| /xxxxxxxx/xxxxx                          |     167 |        47591.826 |   167 |     0 |     0 |     0 |
| /xxxxxxxxxxxxx/xxxxxx                    |      25 |         7432.200 |    25 |     0 |     0 |     0 |
| /xxxx/xxxxx/x/xxxxxxxxxxxxx/xxxxxxx      |      22 |          698.727 |    22 |     0 |     0 |     0 |
| /xxxx/xxxxx/x/xxxxxxxxxxxxx/xxxxxx       |      19 |         7431.632 |    19 |     0 |     0 |     0 |
| /xxxxx/xxxxx/                            |      18 |         7840.889 |    18 |     0 |     0 |     0 |
| /xxxxxxxx/xxxxxxxxxxxxxxxxx              |      15 |         7356.000 |    15 |     0 |     0 |     0 |
| /xxxxxxxxxxx/xxxxxxxx                    |      15 |         9978.800 |    15 |     0 |     0 |     0 |
| /xxxxx/                                  |      14 |            0.000 |     0 |    14 |     0 |     0 |
| /xxxxxxxxxx/xxxxxxxx/xxxxx               |      13 |        20530.154 |    13 |     0 |     0 |     0 |

Lời kết

Chúng ta thật sự tự hào khi bộ công cụ này được viết bởi người Việt Nam và được yêu thích bởi các người dùng khác trên thế giới. Hy vọng thông qua bài viết Giám Sát NGINX Log Theo Thời Gian Thực Bằng Ngxtop Trên CentOS 7 có thể giúp ích được cho các bạn.

Chúc các bạn thành công.

You may also like

Leave a Comment