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。

电报渠道:

毫秒:

对于谁重视毫秒,请在此写信或投票 问题.

来源: habr.com

添加评论