prometheus配置nginx监控

经过网上一波查找到了两种比较合适的方案
https://github.com/knyar/nginx-lua-prometheus
https://github.com/hnlq715/nginx-vts-exporter
记录一下踩坑过程

一、nginx-vts方案
vts源码 https://github.com/vozlt/nginx-module-vts
exporter源码 https://github.com/hnlq715/nginx-vts-exporter
1、下载nginx,编译安装,把vts模块加进去 --add-module=/root/nginx-module-vts(这里我是直接用yum安装出来的nginx参数,nginx -V查看参数)

./configure --add-module=/root/nginx-module-vts --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

make && make install

2、nginx.conf加入以下配置,server可写到conf.d/default.conf 等目录

3、下载nginx-vts-exporter并启动(修改端口等其他参数配置使用–help参看)
nohup /bin/nginx-vts-exporter -nginx.scrape_uri=http://localhost:8088/status/format/json &
(这里的uri端口需要指定成上面配置的vts界面端口,我用的8088)
4、需要监控uri情况的域名配置文件中写入该行配置vhost_traffic_status_filter_by_set_key $uri uri::$server_name; 不监控uri情况的域名无需配置。这种uri信息情况是我最需要拿到的,好多教程都没讲这玩意,至于国家分布那些对我来说没啥用,就全都没加
在这里插入图片描述示例

server {
listen 80;
server_name nginx.test.com;
vhost_traffic_status_filter_by_set_key $uri uri::$server_name;   #这行用于显示uri信息
location / {
    proxy_pass http://nginx_test_worker/;
   }
}

5、grafana图我用的是2949,但是并不符合要求,主要是加上了如下这种展示uri访问情况的图
在这里插入图片描述

二、nginx-lua(openresty)方案

1、安装openresty

sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

sudo yum install openresty

2、git clone https://github.com/knyar/nginx-lua-prometheus

3、添加 nginx 配置文件 conf.d/prometheus.conf

lua_shared_dict prometheus_metrics 10M;
lua_package_path "/usr/local/openresty/lualib/ngx/?.lua";   #指定为git上扒下来的nginx-lua-prometheus代码路径
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})  #这行配置是git上默认配置没有的,目的是拿到uri信息
        metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
';


server {
        listen 9145;
        server_name nginxlua.test.com;

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

4、nginx机器9145对应端口接入prometheus
5、gafana上用的图是 462,加上下面这个promql显示出uri分布情况
在这里插入图片描述
两种方法最后经过压测选择了第一种vts的方案,具体用那种根据自身情况选择吧╮(╯▽╰)╭

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值