Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Bydd yr erthygl hon yn trafod y prosiect nginx-log-gasglwr, a fydd yn darllen logiau nginx a'u hanfon at y clwstwr Clickhouse. Fel arfer defnyddir ElasticSearch ar gyfer logiau. Mae angen llai o adnoddau ar Clickhouse (lle ar y ddisg, RAM, CPU). Mae Clickhouse yn cofnodi data yn gyflymach. Mae Clickhouse yn cywasgu data, gan wneud y data ar ddisg hyd yn oed yn fwy cryno. Mae manteision Clickhouse i'w gweld mewn 2 sleid o'r adroddiad Sut mae VK yn mewnosod data i ClickHouse o ddegau o filoedd o weinyddion.

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

I weld dadansoddeg yn seiliedig ar logiau, byddwn yn creu dangosfwrdd ar gyfer Grafana.

Unrhyw un â diddordeb, croeso i gath.

Gosod nginx, grafana yn y ffordd safonol.

Gosod clwstwr clickhouse gan ddefnyddio ansible-playbook o Denis Proskurin.

Creu cronfeydd data a thablau yn Clickhouse

Yn hynny ffeil Disgrifir ymholiadau SQL ar gyfer creu cronfeydd data a thablau ar gyfer nginx-log-collector yn Clickhouse.

Rydyn ni'n gwneud pob cais fesul un ar bob gweinydd yng nghlwstwr Clickhouse.

Nodyn pwysig. Yn y llinell hon, mae angen disodli logs_cluster gyda'ch enw clwstwr o'r ffeil clickhouse_remote_servers.xml rhwng "remote_servers" a "shard".

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

Gosod a ffurfweddu nginx-log-collector-rpm

Nid oes gan Nginx-log-collector rpm. Yma https://github.com/patsevanton/nginx-log-collector-rpm creu rpm ar ei gyfer. Bydd rpm yn cael ei lunio gan ddefnyddio Fedora Copr

Gosodwch y pecyn 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

Golygu'r ffurfwedd /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/

Sefydlu nginx

Cyfluniad nginx cyffredinol:

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

Un gwesteiwr rhithwir:

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

Ychwanegu gwesteiwyr rhithwir i'r ffeil /etc/hosts:

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

Efelychydd gweinydd HTTP

Fel efelychydd gweinydd HTTP byddwn yn ei ddefnyddio nodejs-stub-gweinydd o Maxim Ignatenko

Nid oes gan Nodejs-stub-server rpm. Yma https://github.com/patsevanton/nodejs-stub-server creu rpm ar ei gyfer. Bydd rpm yn cael ei lunio gan ddefnyddio Fedora Copr

Gosod pecyn nodejs-stub-server ar nginx rpm i fyny'r afon

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

Profi Straen

Rydym yn cynnal profion gan ddefnyddio meincnod Apache.

Ei osod:

yum install -y httpd-tools

Rydym yn dechrau profi gan ddefnyddio meincnod Apache o 5 gweinydd gwahanol:

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

Sefydlu Grafana

Ni fyddwch yn dod o hyd i ddangosfwrdd ar wefan swyddogol Grafana.

Felly, byddwn yn ei wneud â llaw.

Gallwch ddod o hyd i fy dangosfwrdd arbed yma.

Mae angen i chi hefyd greu newidyn tabl gyda'r cynnwys nginx.access_log.
Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Cyfanswm y Ceisiadau Singlestat:

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

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Ceisiadau a Fethodd Singlestat:

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

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Canran Methu 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

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Amser Ymateb Singlestat Avg:

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

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Amser Ymateb Singlestat Max:

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

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Statws Cyfrif:

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

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

I allbynnu data fel pastai, mae angen i chi osod yr ategyn ac ailgychwyn grafana.

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

Pei Statws 5 UCHAF:

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

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Ymhellach byddaf yn rhoi ceisiadau heb sgrinluniau:

Cyfrif http_user_agent:

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

Cyfradd Da/Cyfradd Wael:

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

Amseriad Ymateb:

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

Amser ymateb i fyny'r afon (amser ymateb 1af i fyny'r afon):

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

Statws Cyfrif Tabl ar gyfer pob vhost:

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

Golygfa gyffredinol o'r dangosfwrdd

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Cymhariaeth o avg() a quantile()

cyf()
Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse
cwantol ()
Cyfleustodau Nginx-log-collector o Avito ar gyfer anfon logiau nginx i Clickhouse

Casgliad:

Rwy'n gobeithio y bydd y gymuned yn cymryd rhan mewn datblygu/profi a defnyddio nginx-log-collector.
A phan fydd rhywun yn gweithredu nginx-log-collector, byddant yn dweud wrthych faint y maent wedi'i arbed ar ddisg, RAM, a CPU.

Sianeli telegram:

Milieiliadau:

I bwy mae milieiliadau o bwys, ysgrifennwch neu pleidleisiwch yn hwn mater.

Ffynhonnell: hab.com

Ychwanegu sylw