Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Bài viết này sẽ thảo luận về dự án nginx-log-collector, sẽ đọc nhật ký nginx, gửi chúng đến cụm Clickhouse. Thông thường ElasticSearch được sử dụng cho nhật ký. Clickhouse yêu cầu ít tài nguyên hơn (dung lượng ổ đĩa, RAM, CPU). Clickhouse ghi dữ liệu nhanh hơn. Clickhouse nén dữ liệu, giúp dữ liệu trên đĩa trở nên nhỏ gọn hơn. Lợi ích của Clickhouse có thể được thấy trong 2 slide của báo cáo Cách VK chèn dữ liệu vào ClickHouse từ hàng chục nghìn máy chủ.

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Để xem phân tích theo nhật ký, hãy tạo trang tổng quan cho Grafana.

Ai quan tâm, chào mừng dưới con mèo.

Cài nginx, grafana theo cách chuẩn.

Cài đặt cụm clickhouse với ansible-playbook từ Denis Proskurin.

Tạo cơ sở dữ liệu và bảng trong Clickhouse

Trong này tập tin Các truy vấn SQL để tạo cơ sở dữ liệu và bảng cho nginx-log-collector trong Clickhouse được mô tả.

Chúng tôi thực hiện lần lượt từng yêu cầu trên từng máy chủ của cụm Clickhouse.

Lưu ý quan trọng. Trong dòng này, logs_cluster phải được thay thế bằng tên cụm của bạn từ tệp clickhouse_remote_servers.xml giữa "remote_servers" và "shard".

ENGINE = Distributed('logs_cluster', 'nginx', 'access_log_shard', rand())

Cài đặt và cấu hình nginx-log-collector-rpm

Nginx-log-collector không có vòng/phút. Đây https://github.com/patsevanton/nginx-log-collector-rpm tạo vòng/phút cho nó. vòng/phút sẽ được xây dựng bằng cách sử dụng Fedora Copr

Cài đặt gói vòng/phút nginx-log-collector-rpm

yum -y install yum-plugin-copr
yum copr enable antonpatsev/nginx-log-collector-rpm
yum -y install nginx-log-collector
systemctl start nginx-log-collector

Chỉnh sửa cấu hình /etc/nginx-log-collector/config.yaml:

  .......
  upload:
    table: nginx.access_log
    dsn: http://ip-адрес-кластера-clickhouse:8123/

- tag: "nginx_error:"
  format: error  # access | error
  buffer_size: 1048576
  upload:
    table: nginx.error_log
    dsn: http://ip-адрес-кластера-clickhouse:8123/

Thiết lập nginx

Cấu hình nginx chung:

user  nginx;
worker_processes  auto;

#error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format avito_json escape=json
                     '{'
                     '"event_datetime": "$time_iso8601", '
                     '"server_name": "$server_name", '
                     '"remote_addr": "$remote_addr", '
                     '"remote_user": "$remote_user", '
                     '"http_x_real_ip": "$http_x_real_ip", '
                     '"status": "$status", '
                     '"scheme": "$scheme", '
                     '"request_method": "$request_method", '
                     '"request_uri": "$request_uri", '
                     '"server_protocol": "$server_protocol", '
                     '"body_bytes_sent": $body_bytes_sent, '
                     '"http_referer": "$http_referer", '
                     '"http_user_agent": "$http_user_agent", '
                     '"request_bytes": "$request_length", '
                     '"request_time": "$request_time", '
                     '"upstream_addr": "$upstream_addr", '
                     '"upstream_response_time": "$upstream_response_time", '
                     '"hostname": "$hostname", '
                     '"host": "$host"'
                     '}';

    access_log     syslog_server=unix:/var/run/nginx_log.sock,nohostname,tag=nginx avito_json; #ClickHouse
    error_log      syslog_server=unix:/var/run/nginx_log.sock,nohostname,tag=nginx_error; #ClickHouse

    #access_log  /var/log/nginx/access.log  main;

    proxy_ignore_client_abort on;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

Máy chủ ảo một:

vhost1.conf:

upstream backend {
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
}

server {
    listen   80;
    server_name vhost1;
    location / {
        proxy_pass http://backend;
    }
}

Thêm máy chủ ảo vào tệp /etc/hosts:

ip-адрес-сервера-с-nginx vhost1

Trình mô phỏng máy chủ HTTP

Là trình mô phỏng máy chủ HTTP, chúng tôi sẽ sử dụng nodejs-stub-server từ Maxim Ignatenko

nodejs-stub-server không có vòng/phút. Đây https://github.com/patsevanton/nodejs-stub-server tạo vòng/phút cho nó. vòng/phút sẽ được xây dựng bằng cách sử dụng Fedora Copr

Cài đặt gói nodejs-stub-server trên vòng/phút nginx ngược dòng

yum -y install yum-plugin-copr
yum copr enable antonpatsev/nodejs-stub-server
yum -y install stub_http_server
systemctl start stub_http_server

Bài kiểm tra về áp lực

Việc kiểm tra được thực hiện bằng cách sử dụng điểm chuẩn Apache.

Cài đặt nó:

yum install -y httpd-tools

Chúng tôi bắt đầu thử nghiệm bằng điểm chuẩn Apache từ 5 máy chủ khác nhau:

while true; do ab -H "User-Agent: 1server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 2server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 3server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 4server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 5server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done

Thiết lập Grafana

Bạn sẽ không tìm thấy bảng điều khiển trên trang web chính thức của Grafana.

Vì vậy, chúng tôi sẽ làm điều đó bằng tay.

Bạn có thể tìm thấy trang tổng quan đã lưu của tôi đây.

Bạn cũng cần tạo một biến bảng có nội dung nginx.access_log.
Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Tổng số yêu cầu Singlestat:

SELECT
 1 as t,
 count(*) as c
 FROM $table
 WHERE $timeFilter GROUP BY t

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Yêu cầu Singlestat không thành công:

SELECT
 1 as t,
 count(*) as c
 FROM $table
 WHERE $timeFilter AND status NOT IN (200, 201, 401) GROUP BY t

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Phần trăm lỗi Singlestat:

SELECT
 1 as t, (sum(status = 500 or status = 499)/sum(status = 200 or status = 201 or status = 401))*100 FROM $table
 WHERE $timeFilter GROUP BY t

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Thời gian phản hồi trung bình của Singlestat:

SELECT
 1, avg(request_time) FROM $table
 WHERE $timeFilter GROUP BY 1

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Thời gian phản hồi tối đa của Singlestat:

SELECT
 1 as t, max(request_time) as c
 FROM $table
 WHERE $timeFilter GROUP BY t

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Trạng thái đếm:

$columns(status, count(*) as c) from $table

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Để xuất dữ liệu như một chiếc bánh, bạn cần cài đặt plugin và tải lại grafana.

grafana-cli plugins install grafana-piechart-panel
service grafana-server restart

Trạng thái TOP 5 của Pie:

SELECT
    1, /* fake timestamp value */
    status,
    sum(status) AS Reqs
FROM $table
WHERE $timeFilter
GROUP BY status
ORDER BY Reqs desc
LIMIT 5

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Hơn nữa tôi sẽ đưa ra yêu cầu mà không cần ảnh chụp màn hình:

Đếm http_user_agent:

$columns(http_user_agent, count(*) c) FROM $table

Tỷ lệ Tốt/Tỷ lệ Xấu:

$rate(countIf(status = 200) AS good, countIf(status != 200) AS bad) FROM $table

thời gian đáp ứng:

$rate(avg(request_time) as request_time) FROM $table

Thời gian phản hồi ngược dòng (thời gian phản hồi của ngược dòng thứ 1):

$rate(avg(arrayElement(upstream_response_time,1)) as upstream_response_time) FROM $table

Trạng thái đếm bảng cho tất cả vhost:

$columns(status, count(*) as c) from $table

Chế độ xem tổng thể của bảng điều khiển

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

So sánh avg() và quantile()

trung bình ()
Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse
lượng tử()
Tiện ích Nginx-log-collector từ Avito để gửi nhật ký nginx tới Clickhouse

Kết luận:

Hy vọng cộng đồng sẽ tham gia phát triển/thử nghiệm và sử dụng nginx-log-collector.
Và khi ai đó triển khai nginx-log-collector, anh ta sẽ cho bạn biết anh ta đã tiết kiệm được bao nhiêu đĩa, RAM, CPU.

Các kênh Telegram:

Mili giây:

Ai quan tâm đến mili giây, hãy viết hoặc bỏ phiếu, trong này vấn đề.

Nguồn: www.habr.com

Thêm một lời nhận xét