美文网首页
Prometheus监控Nginx

Prometheus监控Nginx

作者: 袁先生的笔记 | 来源:发表于2018-11-13 10:46 被阅读0次

    环境

    系统:CentOS 7.5

    准备

    Nginx端

    • 下载lua脚本
      地址:https://github.com/knyar/nginx-lua-prometheus

    • 复制lua脚本

      # cp prometheus.lua /usr/local/lib/lua/5.1/
      
    • 添加Nginx配置

      # vim /etc/nginx/conf.d/prometheus.conf
      lua_shared_dict prometheus_metrics 10M;
      lua_package_path "/usr/local/lib/lua/5.1/?.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})
      ';
      
      
      server {
              listen 9145;
              server_name xxx.your.domain.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()
                      ';
              }
      }
      
    • 重启Nginx

      # systemctl restart nginx
      

    Prometheus端

    • 配置Prometheus

      # vim /usr/local/prometheus/prometheus.yml
      scrape_configs:
        - job_name: 'Nginx'
        static_configs:
          - targets:
            - 10.10.10.1:9145
      
    • 重启Prometheus

      # systemctl restart prometheus
      

    Grafana端

    • 添加dashboards
      点击Create - Import,输入dashboards的id(推荐462)

    相关文章

      网友评论

          本文标题:Prometheus监控Nginx

          本文链接:https://www.haomeiwen.com/subject/efskfqtx.html