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 等大部分需要的指标了.
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 部署及相应配置
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.YAML 配置文件, 将 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
根据监控需求进行标签分组, 增删图像. 最终效果如下:
精 选推荐
01
openvpn - 实现双网互通
- 02
- Google ARCore
- 03
Android - 性能优化
来源: http://www.tuicool.com/articles/7JZZN3Z