1 实现

使用的是 nginx-lua-prometheus 这个库,负责去采集 nginx 内部的指标,暴露给 Prometheus 拉取

2 安装

[root@VM-10-48-centos logs]# wget -O /etc/yum.repos.d/openresty.repo  https://openresty.org/package/centos/openresty.repo
[root@VM-10-48-centos logs]# yum install -y openresty
[root@VM-10-48-centos logs]# systemctl start openresty.service

Prometheus监控Nginx服务_Prometheus

3 配置

下载nginx-lua-prometheus

[root@VM-10-48-centos logs]# cd /usr/local/openresty/lualib/
[root@VM-10-48-centos logs]# git clone https://hub.fastgit.org/knyar/nginx-lua-prometheus.git
# 确保以下三个文件存在
[root@VM-10-48-centos lualib]# ls -l nginx-lua-prometheus/
total 40
-rw-r--r-- 1 root root 3371 May 28 13:56 prometheus_keys.lua
-rw-r--r-- 1 root root 28754 May 28 13:56 prometheus.lua
-rw-r--r-- 1 root root 2212 May 28 13:56 prometheus_resty_counter.lua

配置openresty

[root@VM-10-48-centos openresty]# vim /usr/local/openresty/nginx/conf/nginx.conf
include /usr/local/openresty/nginx/conf.d/*.conf;
[root@VM-10-48-centos openresty]# vim /usr/local/openresty/nginx/conf.d/prometheus.conf
lua_shared_dict prometheus_metrics 10M;
lua_package_path "/usr/local/openresty/lualib/nginx-lua-prometheus/?.lua;;";
init_worker_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
metric_requests = prometheus:counter(
"nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
metric_latency = prometheus:histogram(
"nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
metric_connections = prometheus:gauge(
"nginx_http_connections", "Number of HTTP connections", {"state"})
}
log_by_lua_block {
metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
}
server {
listen 9145;
location /metrics {
content_by_lua_block {
metric_connections:set(ngx.var.connections_reading, {"reading"})
metric_connections:set(ngx.var.connections_waiting, {"waiting"})
metric_connections:set(ngx.var.connections_writing, {"writing"})
prometheus:collect()
}
}
}

重启openresty

[root@VM-10-48-centos prometheus]# systemctl restart openresty.service

Prometheus监控Nginx服务_nginx_02

测试访问

[root@VM-10-48-centos openresty]# curl localhost:9145/metrics
# HELP nginx_http_connections Number of HTTP connections
# TYPE nginx_http_connections gauge
nginx_http_connections{state="reading"} 0
nginx_http_connections{state="waiting"} 1
nginx_http_connections{state="writing"} 1
# HELP nginx_http_request_duration_seconds HTTP request latency
# TYPE nginx_http_request_duration_seconds histogram
nginx_http_request_duration_seconds_bucket{host="",le="0.005"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.01"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.02"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.03"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.05"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.075"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.1"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.2"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.3"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.4"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.5"} 6
nginx_http_request_duration_seconds_bucket{host="",le="0.75"} 6
nginx_http_request_duration_seconds_bucket{host="",le="1"} 6
nginx_http_request_duration_seconds_bucket{host="",le="1.5"} 6
nginx_http_request_duration_seconds_bucket{host="",le="2"} 6
nginx_http_request_duration_seconds_bucket{host="",le="3"} 6
nginx_http_request_duration_seconds_bucket{host="",le="4"} 6
nginx_http_request_duration_seconds_bucket{host="",le="5"} 6
nginx_http_request_duration_seconds_bucket{host="",le="10"} 6
nginx_http_request_duration_seconds_bucket{host="",le="+Inf"} 6
nginx_http_request_duration_seconds_count{host=""} 6
nginx_http_request_duration_seconds_sum{host=""} 0
# HELP nginx_http_requests_total Number of HTTP requests
# TYPE nginx_http_requests_total counter
nginx_http_requests_total{host="",status="200"} 6
# HELP nginx_metric_errors_total Number of nginx-lua-prometheus errors
# TYPE nginx_metric_errors_total counter
nginx_metric_errors_total 0

4 配置prometheus

[root@VM-10-48-centos prometheus]# cat prometheus.yml
...
- job_name: 'Nginx'
static_configs:
- targets: ['10.1.10.48:9145']

Prometheus监控Nginx服务_nginx_03