本文將討論該項目
要按日誌查看分析,讓我們為 Grafana 創建一個儀表板。
誰在乎,歡迎在貓下。
按照標準方式安裝nginx、grafana。
使用 ansible-playbook 安裝 clickhouse 集群
在Clickhouse中創建數據庫和表
在這
我們在 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。 這裡
安裝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 沒有 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
.
Singlestat 請求總數:
SELECT
1 as t,
count(*) as c
FROM $table
WHERE $timeFilter GROUP BY t
Singlestat 失敗的請求:
SELECT
1 as t,
count(*) as c
FROM $table
WHERE $timeFilter AND status NOT IN (200, 201, 401) GROUP BY t
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
Singlestat 平均響應時間:
SELECT
1, avg(request_time) FROM $table
WHERE $timeFilter GROUP BY 1
Singlestat 最大響應時間:
SELECT
1 as t, max(request_time) as c
FROM $table
WHERE $timeFilter GROUP BY t
計數狀態:
$columns(status, count(*) as c) from $table
要像餅圖一樣輸出數據,需要安裝插件並重新加載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
此外,我將提出不帶屏幕截圖的請求:
計算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
儀表板的總體視圖
比較 avg() 和 quantile()
平均()
分位數()
結論:
希望社區能夠參與開發/測試和使用 nginx-log-collector。
當有人實現 nginx-log-collector 時,他會告訴你他節省了多少磁盤、RAM、CPU。
電報渠道:
毫秒:
誰在乎毫秒,請在此寫下或投票
來源: www.habr.com