Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

本文將討論該項目 nginx 日誌收集器,它將讀取 nginx 日誌,並將其發送到 Clickhouse 集群。 通常ElasticSearch用於日誌。 Clickhouse 需要更少的資源(磁盤空間、RAM、CPU)。 Clickhouse 寫入數據速度更快。 Clickhouse對數據進行了壓縮,使得磁盤上的數據更加緊湊。 Clickhouse 的優勢可以從報告的 2 張幻燈片中看出 VK 如何將數万台服務器的數據插入 ClickHouse。

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

要按日誌查看分析,讓我們為 Grafana 創建一個儀表板。

誰在乎,歡迎在貓下。

按照標準方式安裝nginx、grafana。

使用 ansible-playbook 安裝 clickhouse 集群 丹尼斯·普羅斯庫林.

在Clickhouse中創建數據庫和表

在這 文件 描述了在 Clickhouse 中為 nginx-log-collector 創建數據庫和表的 SQL 查詢。

我們在 Clickhouse 集群的每台服務器上依次發出每個請求。

重要的提示。 在此行中,logs_cluster 應替換為“remote_servers”和“shard”之間的 clickhouse_remote_servers.xml 文件中的集群名稱。

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

安裝和配置 nginx-log-collector-rpm

Nginx-log-collector 沒有 rpm。 這裡 https://github.com/patsevanton/nginx-log-collector-rpm 為它創建rpm。 rpm 將使用構建 軟呢帽

安裝rpm包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

編輯配置/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/

設置 nginx

一般 nginx 配置:

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;
}

虛擬主機一:

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;
    }
}

將虛擬主機添加到 /etc/hosts 文件中:

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

HTTP 服務器模擬器

作為 HTTP 服務器模擬器,我們將使用 nodejs存根服務器馬克西姆·伊格納堅科

nodejs-stub-server 沒有 rpm。 這裡 https://github.com/patsevanton/nodejs-stub-server 為它創建rpm。 rpm 將使用構建 軟呢帽

在上游 nginx rpm 上安裝 nodejs-stub-server 包

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

壓力測試

測試是使用 Apache 基准進行的。

安裝它:

yum install -y httpd-tools

我們開始使用來自 5 個不同服務器的 Apache 基準測試:

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

設置 Grafana

您在 Grafana 官方網站上找不到儀表板。

因此,我們將手工完成。

你可以找到我保存的儀表板 這裡.

您還需要創建一個包含內容的表變量 nginx.access_log.
Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

Singlestat 請求總數:

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

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

Singlestat 失敗的請求:

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

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

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

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

Singlestat 平均響應時間:

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

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

Singlestat 最大響應時間:

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

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

計數狀態:

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

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

要像餅圖一樣輸出數據,需要安裝插件並重新加載grafana。

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

餅圖 TOP 5 狀態:

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

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

此外,我將提出不帶屏幕截圖的請求:

計算http_user_agent:

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

良好率/不良率:

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

響應時間:

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

上行響應時間(第一上行響應時間):

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

所有虛擬主機的表計數狀態:

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

儀表板的總體視圖

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

比較 avg() 和 quantile()

平均()
Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse
分位數()
Avito 的 Nginx-log-collector 實用程序,用於將 nginx 日誌發送到 Clickhouse

結論:

希望社區能夠參與開發/測試和使用 nginx-log-collector。
當有人實現 nginx-log-collector 時,他會告訴你他節省了多少磁盤、RAM、CPU。

電報渠道:

毫秒:

誰在乎毫秒,請在此寫下或投票 問題.

來源: www.habr.com

添加評論