Prometheus使用nginx-lua监控Nginx

这里只是对prometheus监控Nginx的配置过程做个记录,方便以后查阅。

1.下载安装LuaJIT

1
2
3
4
5
http://luajit.org/download.html
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar xf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make && sudo make install

2.下载ngx_devel_kit模块

1
2
https://github.com/simplresty/ngx_devel_kit/releases
tar xf ngx_devel_kit-0.3.0.tar.gz -C /usr/local/src/

3.下载lua-nginx模块

1
2
3
4
https://github.com/openresty/lua-nginx-module/releases
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
tar xf v0.10.15.tar.gz
sudo mv -f lua-nginx-module-0.10.15/ /usr/local/lib/

4.安装nginx依赖包zlib,pcre

1
2
sudo yum -y install pcre-devel openssl openssl-devel
sudo yum install zlib-devel

5.下载安装nginx

1
2
wget http://nginx.org/download/nginx-1.13.5.tar.gz
tar -xvf nginx-1.13.5.tar.gz

安装nginx、nginx-lua 模块

1
2
3
4
5
6
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1
cd nginx-1.13.5
./configure --prefix=/work/admin/nginx --add-module=/usr/local/lib/lua-nginx-module-0.10.15 --add-module=/usr/local/src/ngx_devel_kit-0.3.0
make
sudo make install

查看nginx模块,可能会报以下错误

1
2
./nginx/sbin/nginx -V
./nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解决方法

1
2
3
4
5
sudo ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
./nginx/sbin/nginx -V
nginx version: nginx/0.0.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/work/admin/nginx --add-module=/usr/local/lib/lua-nginx-module-0.10.15

6.下载nginx-lua-prometheus

1
2
3
wget https://github.com/knyar/nginx-lua-prometheus/archive/0.20181120.tar.gz
tar xf 0.20181120.tar.gz
mv -f nginx-lua-prometheus-0.20181120 /work/admin/nginx-lua-prometheus

7.nginx lua 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#nginx monitor lua
lua_shared_dict prometheus_metrics 10M;
lua_package_path "/work/admin/nginx-lua-prometheus/?.lua";
init_by_lua '
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"})
metric_requests_uri = prometheus:counter(
"nginx_http_requests_uri_total", "Number of HTTP requests_uri", {"host","uri", "status"})';
log_by_lua '
metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
metric_requests_uri:inc(1, {ngx.var.server_name,ngx.var.request_uri, ngx.var.status})
metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})';


server {

listen 9145;
server_name nginx-metrics.feiersmart.local;
allow 192.168.0.0/16;
deny all;

location /metrics {
access_log off;
content_by_lua '
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()';
}
}

8.prometheus 拉取监控指标

1
2
3
4
5
6
- job_name: 'wan'
static_configs:
- targets: ['192.168.6.214:9100']
labels: {cluster: 'product',type: 'basic',env: 'wan',job: 'wan1',export: 'wan1'}
- targets: ['192.168.6.214:9145']
labels: {cluster: 'product',type: 'middle',env: 'wan',job: 'wan1',export: 'nginx-lua-exporter'}

重启prometheus

如果开启了api管理功能,可以如下方式热加载配置

1
curl -X POST http://127.0.0.1:9090/-/reload

9.grafana出图

推荐使用9516

效果如下

上图监控面板我经过一些改动,仅供参考

-------------本文结束感谢您的阅读-------------