本文将讨论该项目
为了查看基于日志的分析,我们将为 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。
电报渠道:
毫秒:
对于谁重视毫秒,请在此写信或投票
来源: habr.com