ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ืžืืžืจ ื–ื” ื™ื“ื•ืŸ ื‘ืคืจื•ื™ืงื˜ nginx-log-collector, ืฉื™ืงืจื ื™ื•ืžื ื™ nginx, ืฉืœื— ืื•ืชื ืœืืฉื›ื•ืœ Clickhouse. ื‘ื“ืจืš ื›ืœืœ ElasticSearch ืžืฉืžืฉ ืขื‘ื•ืจ ื™ื•ืžื ื™ื. Clickhouse ื“ื•ืจืฉ ืคื—ื•ืช ืžืฉืื‘ื™ื (ืฉื˜ื— ื“ื™ืกืง, ื–ื™ื›ืจื•ืŸ RAM, CPU). Clickhouse ื›ื•ืชื‘ ื ืชื•ื ื™ื ืžื”ืจ ื™ื•ืชืจ. Clickhouse ื“ื•ื—ืก ืืช ื”ื ืชื•ื ื™ื, ืžื” ืฉื”ื•ืคืš ืืช ื”ื ืชื•ื ื™ื ื‘ื“ื™ืกืง ืืคื™ืœื• ื™ื•ืชืจ ืงื•ืžืคืงื˜ื™ื™ื. ื ื™ืชืŸ ืœืจืื•ืช ืืช ื”ื™ืชืจื•ื ื•ืช ืฉืœ Clickhouse ื‘-2 ืฉืงื•ืคื™ื•ืช ืžื”ื“ื•ื— ื›ื™ืฆื“ VK ืžื›ื ื™ืก ื ืชื•ื ื™ื ืœ-ClickHouse ืžืขืฉืจื•ืช ืืœืคื™ ืฉืจืชื™ื.

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื›ื“ื™ ืœื”ืฆื™ื’ ื ื™ืชื•ื— ืœืคื™ ื™ื•ืžื ื™ื, ื‘ื•ืื• ื ื™ืฆื•ืจ ืœื•ื— ืžื—ื•ื•ื ื™ื ืขื‘ื•ืจ Grafana.

ืœืžื™ ืื›ืคืช, ื‘ืจื•ืš ื”ื‘ื ืžืชื—ืช ืœื—ืชื•ืœ.

ื”ืชืงืŸ nginx, grafana ื‘ืฆื•ืจื” ื”ืกื˜ื ื“ืจื˜ื™ืช.

ื”ืชืงืŸ ืืฉื›ื•ืœ ืงืœื™ืงื”ืื•ืก ืขื ืื ืกible-playbook ืž ื“ื ื™ืก ืคืจื•ืกืงื•ืจื™ืŸ.

ื™ืฆื™ืจืช ืžืกื“ ื ืชื•ื ื™ื ื•ื˜ื‘ืœืื•ืช ื‘ืงืœื™ืงื”ืื•ืก

ื‘ื–ื” ืงื•ื‘ืฅ ืžืชื•ืืจื•ืช ืฉืื™ืœืชื•ืช SQL ืœื™ืฆื™ืจืช ืžืกื“ื™ ื ืชื•ื ื™ื ื•ื˜ื‘ืœืื•ืช ืขื‘ื•ืจ nginx-log-collector ื‘-Clickhouse.

ืื ื• ืžื‘ืฆืขื™ื ื›ืœ ื‘ืงืฉื” ื‘ืชื•ืจื” ื‘ื›ืœ ืฉืจืช ืฉืœ ืืฉื›ื•ืœ Clickhouse.

ื”ืขืจื” ื—ืฉื•ื‘ื”. ื‘ืฉื•ืจื” ื–ื•, ื™ืฉ ืœื”ื—ืœื™ืฃ logs_cluster ื‘ืฉื ื”ืืฉื›ื•ืœ ืฉืœืš ืžื”ืงื•ื‘ืฅ clickhouse_remote_servers.xml ื‘ื™ืŸ "remote_servers" ื•-"shard".

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

ื”ืชืงื ื” ื•ื”ื’ื“ืจื” ืฉืœ nginx-log-collector-rpm

ืœ-Nginx-log-collector ืื™ืŸ ืกืœ"ื“. ื›ืืŸ https://github.com/patsevanton/nginx-log-collector-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-stub-server ืž ืžืงืกื™ื ืื™ื’ื ื˜ื ืงื•

ืœ-nodejs-stub-server ืื™ืŸ ืกืœ"ื“. ื›ืืŸ https://github.com/patsevanton/nodejs-stub-server ืœื™ืฆื•ืจ ืขื‘ื•ืจื• ืกืœ"ื“. ืกืœ"ื“ ื™ื™ื‘ื ื” ื‘ืืžืฆืขื•ืช ืคื“ื•ืจื” ืงื•ืคืจ

ื”ืชืงืŸ ืืช ื—ื‘ื™ืœืช nodejs-stub-server ื‘-nginx rpm ื‘ืžืขืœื” ื”ื–ืจื

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

ืื ื• ืžืชื—ื™ืœื™ื ืœื‘ื—ื•ืŸ ื‘ืืžืฆืขื•ืช benchmark ืฉืœ Apache ืž-5 ืฉืจืชื™ื ืฉื•ื ื™ื:

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.

ืœื›ืŸ, ื ืขืฉื” ื–ืืช ื‘ื™ื“.

ืืชื” ื™ื›ื•ืœ ืœืžืฆื•ื ืืช ืœื•ื— ื”ืžื—ื•ื•ื ื™ื ื”ืฉืžื•ืจ ืฉืœื™ ื›ืืŸ.

ืืชื” ื’ื ืฆืจื™ืš ืœื™ืฆื•ืจ ืžืฉืชื ื” ื˜ื‘ืœื” ืขื ื”ืชื•ื›ืŸ nginx.access_log.
ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ืกื”"ื› ื‘ืงืฉื•ืช Singlestat:

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

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื‘ืงืฉื•ืช ืฉื ื›ืฉืœื• ื‘-Singlestat:

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

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ืื—ื•ื– ื›ืฉืœ ื‘ืกื™ื ื’ืœ-ืกื˜ื˜:

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

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื–ืžืŸ ืชื’ื•ื‘ื” ืžืžื•ืฆืข ืฉืœ Singlestat:

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

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื–ืžืŸ ืชื’ื•ื‘ื” ืžืงืกื™ืžืœื™ ืฉืœ Singlestat:

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

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ืกื˜ื˜ื•ืก ืกืคื™ืจื”:

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

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื›ื“ื™ ืœื”ื•ืฆื™ื ื ืชื•ื ื™ื ื›ืžื• ืขื•ื’ื”, ืขืœื™ืš ืœื”ืชืงื™ืŸ ืืช ื”ืชื•ืกืฃ ื•ืœื˜ืขื•ืŸ ืžื—ื“ืฉ ืืช ื”ื’ืจืื ื”.

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

ืกื˜ื˜ื•ืก ืขื•ื’ื” ื˜ื•ืค 5:

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

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื‘ื”ืžืฉืš ืืชืŸ ื‘ืงืฉื•ืช ืœืœื ืฆื™ืœื•ืžื™ ืžืกืš:

ืกืคื™ืจื” http_user_agent:

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

GoodRate/BadRate:

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

ืชื–ืžื•ืŸ ืชื’ื•ื‘ื”:

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

ื–ืžืŸ ืชื’ื•ื‘ื” ื‘ืžืขืœื” ื”ื–ืจื (ื–ืžืŸ ืชื’ื•ื‘ื” ืฉืœ ื”-1 ื‘ืžืขืœื” ื”ื–ืจื):

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

ืกื˜ื˜ื•ืก ืกืคื™ืจืช ื˜ื‘ืœื” ืขื‘ื•ืจ ื›ืœ ื”-vhosts:

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

ืชืฆื•ื’ื” ื›ืœืœื™ืช ืฉืœ ืœื•ื— ื”ืžื—ื•ื•ื ื™ื

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ื”ืฉื•ื•ืืช avg() ื•-quantile()

avg()
ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse
quantile()
ื›ืœื™ Nginx-log-collector ืž-Avito ืœืฉืœื™ื—ืช ื™ื•ืžื ื™ nginx ืœ-Clickhouse

ืžืกืงื ื”:

ืžืงื•ื•ื” ืฉื”ืงื”ื™ืœื” ืชื”ื™ื” ืžืขื•ืจื‘ืช ื‘ืคื™ืชื•ื—/ื‘ื“ื™ืงื” ื•ืฉื™ืžื•ืฉ ื‘-nginx-log-collector.
ื•ื›ืฉืžื™ืฉื”ื• ืžื™ื™ืฉื ืืช nginx-log-collector, ื”ื•ื ื™ื’ื™ื“ ืœืš ื›ืžื” ื”ื•ื ื—ืกืš ื‘ื“ื™ืกืง, RAM, CPU.

ืขืจื•ืฆื™ ื˜ืœื’ืจื:

ืืœืคื™ื•ืช ืฉื ื™ื•ืช:

ืœืžื™ ืื›ืคืช ืžืืœืคื™ื•ืช ืฉื ื™ื•ืช, ื›ืชื•ื‘ ืื• ื”ืฆื‘ื™ืข, ื‘ื‘ืงืฉื”, ื‘ื–ื” ืกื•ื’ื™ื”.

ืžืงื•ืจ: www.habr.com

ื”ื•ืกืคืช ืชื’ื•ื‘ื”