Prometheus 监控 Nginx

前言

Nginx官网有介绍各种监控方案,以前我们常用stub_status和Log日志来实现Nginx监控。本文主要介绍基于Prometheus的2种监控方案nginx-lua-prometheus和nginx-vts-exporter,在真实的生产环境中你可能需要研究和选择哪种方法才是适合你的,F5收购Nginx后的未来让我们一起拭目以待。

Prometheus 监控 Nginx

更新历史

2019年03月25日 - 初稿

阅读原文 - https://wsgzao.github.io/post/prometheus-nginx/

扩展阅读

Monitoring NGINX - https://www.nginx.com/blog/monitoring-nginx/


Nginx监控

官网介绍的监控方案 - https://www.nginx.com/blog/monitoring-nginx/

Prometheus 集成的 HTTP exporter 方案 - https://prometheus.io/docs/instrumenting/exporters/#http

聊聊 Nginx 的监控 - https://zhuanlan.zhihu.com/p/34050716

使用rpmbuild制作Nginx的RPM包 - https://wsgzao.github.io/post/rpmbuild/

Prometheus 监控 Nginx

nginx-lua-prometheus

Nginx 需要添加 Lua 扩展

https://github.com/knyar/nginx-lua-prometheus

# 下载redis_exporter
https://github.com/knyar/nginx-lua-prometheus/releases
wget https://github.com/knyar/nginx-lua-prometheus/archive/0.20181120.tar.gz
tar xf 0.20181120.tar.gz
cd nginx-lua-prometheus-0.20181120

# 创建prometheus.lua目录
mkdir -p /etc/nginx/lua/
cp prometheus.lua /etc/nginx/lua/

# 编辑nginx配置文件修改,注意修改lua_package_path "/etc/nginx/lua/prometheus.lua";
vim /etc/nginx/nginx.conf

lua_shared_dict prometheus_metrics 10M;
lua_package_path "/etc/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"})
';
log_by_lua '
  metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
  metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
';

# 创建nginx-lua-prometheus
vim /etc/nginx/sites-available/nginx-lua-prometheus

server {
  listen 9145;
  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()
    ';
  }
}

# 创建软链接
cd /etc/nginx/sites-enabled/
ln -s ../sites-available/prometheus

# 测试Nginx语法并reload测试metrics
nginx -t
nginx -s reload
curl http://127.0.0.1:9145/metrics

# iptables rule for Prometheus Nginx 
-A INPUT -s xxx -p tcp --dport 9145 -j ACCEPT

nginx-vts-exporter

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

对方正在输入中

Grafana

nginx-lua-prometheus

https://grafana.com/dashboards/462

nginx-vts-exporter

https://grafana.com/dashboards/2949

参考文献

https://prometheus.io/docs/instrumenting/exporters/#http

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 环境 系统:CentOS 7.5 准备 Nginx添加Lua扩展 Nginx端 下载lua脚本地址:https:/...
    袁先生的笔记阅读 5,410评论 1 2
  • 1、使用 nginx-vts-exporter 监控 nginx 状态,前提条件是 nginx 要编译进 ngin...
    awker阅读 6,801评论 0 1
  • 主机数据收集 主机数据的采集是集群监控的基础;外部模块收集各个主机采集到的数据分析就能对整个集群完成监控和告警等功...
    梅_梅阅读 6,729评论 0 7
  • 文章目的: 1、向没听过或者刚听过但是还对这个监控系统没有任何概念的开发者介绍Prometheus的应用场景。2、...
    whaike阅读 39,768评论 15 59
  • 这几天天天开车回家听一个临在群同修念的回到当下的旅程,常常听着听着会流泪,今天也是这样,听着听着开始痛哭不已...
    誼君阅读 167评论 1 0