使用 prometheus 监控 nginx

栏目: 服务器 · Nginx · 发布时间: 4年前

内容简介:由于nginx的快速和高效,生产中的大部分应用都会选择使用nginx来做反向代理,这时对于nginx的监控就显得非常有必要,需要能够直观的显示出nginx实时的流量,访问量,响应时间,http状态码等指标。prometheus具有由度量名称和键/值对标识的时间序列数据的多维数据模型,能够很好的对nginx的这些指标进行收集,并配合grafana进行图像展示。

使用 prometheus 监控 nginx

Mob技术专家 / 一勾 

前言

由于nginx的快速和高效,生产中的大部分应用都会选择使用nginx来做反向代理,这时对于nginx的监控就显得非常有必要,需要能够直观的显示出nginx实时的流量,访问量,响应时间,http状态码等指标。

prometheus具有由度量名称和键/值对标识的时间序列数据的多维数据模型,能够很好的对nginx的这些指标进行收集,并配合grafana进行图像展示。

收集nginx指标的较多采用的有nginx-vts模块,prometheus-lua两种方式进行采集,本文采用nginx-vts模块方式进行数据收集。

nginx-vts模块

1、添加nginx模块

nginx -V   #configure arguments中可以查看到当前nginx所包含的模块
cd /root/
git clone https://github.com/vozlt/nginx-module-vts   #获取vts源码
wget https://nginx.org/download/nginx-1.14.2.tar.gz   #获取nginx源码
tar -zxvf  https://nginx.org/download/nginx-1.14.2.tar.gz
cd nginx-1.14.2
./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'
#--add-module=/root/nginx-module-vts,并将现有参数全部加上
make && make install    #编译好的新nginx文件,可以直接用来替换掉旧的nginx文件,重启nginx服务进行升级
service nginx restart

2、nginx.conf加入以下配置,通过8088端口展示出数据

http {
    vhost_traffic_status_zone;
    server {
        listen 8088;
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

重新加载nginx配置后,访问 IP: 8088/status 可以看到如下页面,能够获取到请求时间,后端响应时间,http code等大部分需要的指标了。

使用 prometheus 监控 nginx

3、指标的数据类型转换

要将获取到的数据接入prometheus还需要将数据转为metrics类型的数据,vts模块中直接提供了 /status/format/prometheus 接口,访问 IP:8088/status/format/prometheus 即可看到转换后的数据。

部署nginx-vts-exporter

git clone https://github.com/hnlq715/nginx-vts-exporter

chmod +x nginx-vts-exporter

nginx-vts-exporter -nginx.scrape_uri=http://localhost:8088/status/format/json

启动后默认端口为 : 9913/metrics 

4、通过VTS获取其它指标

通过以上部署只能拿到默认的指标,生产中可能还会需要监控uri的请求量,监控IP访问情况(同一个IP出现大量访问时可能被攻击),获取不同agent请求量用于分析等,通过vts模块的vhost_traffic_status_filter_by_set_key功能可以自定义需要获取的指标。 此处的指标需要加到对应的server配置中

server {
        listen    80;
        server_name  nginx.test.com;
        vhost_traffic_status_filter_by_set_key $uri uri::$server_name;                                               #每个uri访问量
        vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;          #不同国家/区域请求量
        vhost_traffic_status_filter_by_set_key $filter_user_agent agent::$server_name;                   #获取用户所使用的agent
        vhost_traffic_status_filter_by_set_key $status $server_name;                                               #http code统计
        vhost_traffic_status_filter_by_set_key $upstream_addr upstream::backend;                         #后端转发统计
        vhost_traffic_status_filter_by_set_key $remote_port client::ports::$server_name;                 #请求端口统计
        vhost_traffic_status_filter_by_set_key $remote_addr client::addr::$server_name;                 #请求IP统计
        location ~ ^/storage/(.+)/.*$ {
            set $volume $1;
            vhost_traffic_status_filter_by_set_key $volume storage::$server_name;                           #请求路径统计
        }
       ......
}

使用 prometheus 监控 nginx

prometheus部署及相应配置

1、部署prometheus

prometheus server采用 go 语言编写,下载后解压可直接运行

tar xvfz prometheus-2.7.2.linux-amd64.tar.gz
cd prometheus-2.7.2.linux-amd64

/usr/local/prometheus-2.7.2.linux-amd64/prometheus --storage.tsdb.path=/data/prometheus --web.enable-lifecycle >> /data/prometheus/prometheus.log 2>&1 & 

#–storage.tsdb.path参数指定数据存储路径

#--web.enable-lifecycle参数允许热加载,设置该参数后可以通过 curl XPOST localhost: 9090/-/reload  快速加载配置

2、 nginx-vts-exporte接入

修改prometheus.yml配置文件,将nginx-vts-exporter对应端口接入prometheus,采用文件发现的方式

  - job_name: 'Nginx_vts_exporter'
    file_sd_configs:
      - files: ['./file_sd_configs/file_sd_configs_nginx.json']
        refresh_interval: 30s

编辑 ./file_sd_configs/file_sd_configs_nginx.json,nginx数量比较多的情况下,可以利用自定义labels对Nginx进行分组,便于后续图像展示

[
  {
    "targets": [ "10.21.141.25:9913" ],
    "labels": {
      "group": "ops-test"
    }
  },
  {
    "targets": [ "10.21.141.26:9913" ],
    "labels": {
      "group": "ops-test"
    }
  }
]

grafana接入prometheus

在grafana数据源设置中将部署好的prometheus加入,就可以对获取到的数据使用图像展示了

导入nginx-vts-exporter的示例图

https: //grafana.com/dashboards/2949

根据监控需求进行标签分组,增删图像。 最终效果如下:

使用 prometheus 监控 nginx

使用 prometheus 监控 nginx


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

用户力:需求驱动的产品、运营和商业模式

用户力:需求驱动的产品、运营和商业模式

郝志中 / 机械工业出版社 / 2015-11-1 / 59.00

《用户力:需求驱动的产品、运营和商业模式》从用户需求角度深刻阐释了互联网产品设计、网络运营、商业模式构建的本质与方法论! 本书以“用户需求”为主线,先用逆向思维进行倒推,从本质的角度分析了用户的需求是如何驱动企业的产品设计、网络运营和商业模式构建的,将这三个重要部分进行了系统性和结构化的串联,然后用顺向思维进行铺陈,从实践和方法论的角度总结了企业究竟应该如围绕用户的真实需求来进行产品设计、网......一起来看看 《用户力:需求驱动的产品、运营和商业模式》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换