Prometheus 監控 Nginx

前言

Nginx官網有介紹各類監控方案,之前咱們經常使用stub_status和Log日誌來實現Nginx監控。本文主要介紹基於Prometheus的2種監控方案nginx-lua-prometheus和nginx-vts-exporter,在真實的生產環境中你可能須要研究和選擇哪一種方法纔是適合你的,F5收購Nginx後的將來讓咱們一塊兒拭目以待。nginx

Prometheus 監控 Nginx

更新歷史

2019年03月25日 - 初稿git

閱讀原文 - https://wsgzao.github.io/post...github

擴展閱讀redis

Monitoring NGINX - https://www.nginx.com/blog/mo...vim


Nginx監控

官網介紹的監控方案 - https://www.nginx.com/blog/mo...bash

Prometheus 集成的 HTTP exporter 方案 - https://prometheus.io/docs/in...curl

聊聊 Nginx 的監控 - https://zhuanlan.zhihu.com/p/...tcp

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

Prometheus 監控 Nginx

nginx-lua-prometheus

Nginx 須要添加 Lua 擴展

https://github.com/knyar/ngin...測試

# 下載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/ng...

對方正在輸入中

Grafana

nginx-lua-prometheus

https://grafana.com/dashboard...

nginx-vts-exporter

https://grafana.com/dashboard...

參考文獻

https://prometheus.io/docs/in...

相關文章
相關標籤/搜索