Skip to content

reinvented-stuff/nginx-njs-prometheus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nginx-njs-prometheus

Nginx stub_status as an exporter for Prometheus

Dependencies

  1. nginx
  2. ngx_http_js_module (http://nginx.org/en/docs/njs/index.html)
  3. ngx_http_stub_status_module (http://nginx.org/en/docs/http/ngx_http_stub_status_module.html)

Nginx configuration

Install njs module

Follow the official documentation: http://nginx.org/en/docs/njs/install.html

Option 1: Check njs dynamic module loaded

In nginx.conf:

load_module modules/ngx_http_js_module.so;

Option 2: Check njs module is built-in

In shell:

nginx -V 2>&1 | grep njs

Import njs application

In nginx.conf:

http {

    ...

    js_import /etc/nginx/nginx_njs_prometheus/nginx_njs_prometheus.js;

}

Refer to the official docs for more information: http://nginx.org/en/docs/http/ngx_http_js_module.html#js_import

Configure VirtualHost

http {

    ...

    server {
        listen       80 default_server;
        server_name  localhost _;

        access_log  /var/log/nginx/access.json.log main;
        error_log  /var/log/nginx/error.log info;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        location /nginx_stats {
            stub_status;
        }

        location /metrics {
            js_content nginx_njs_prometheus.index;
            add_header Content-Type text/plain;
        }


    }

}

Check configuration

sudo nginx -t

Metrics example

# HELP Currently open connections
# TYPE gauge
nginx_current_active{instance="hostname.example.com"} 5
# HELP Number of request nginx is reading headers from
# TYPE gauge
nginx_current_reading{instance="hostname.example.com"} 0
# HELP Number of request body readings, request processings, or response writings
# TYPE gauge
nginx_current_writing{instance="hostname.example.com"} 1
# HELP Number of connections that are Keep-Alive'ing
# TYPE gauge
nginx_current_waiting{instance="hostname.example.com"} 0
# HELP Total accepted connectons
# TYPE counter
nginx_accepted_connectons{instance="hostname.example.com"} 729658
# HELP Total handled connections
# TYPE counter
nginx_handled_connections{instance="hostname.example.com"} 729658
# HELP Total handled requests
# TYPE counter
nginx_handled_requests{instance="hostname.example.com"} 398198

Prometheus Scraper configuration

In prometheus.yml:

global:
  scrape_interval:     30s

scrape_configs:
  - job_name: 'nginx-status'
    static_configs:
      - targets:
          - '127.0.0.1'
        labels:
          hostname: 'hostname.example.com'
          service: 'nginx'
      - targets:
          - '313.29.260.21'
        labels:
          hostname: 'reachable.example.com'
          service: 'nginx'

Grafana dashboard

Here is an example of Grafana Dashboard

Alt text

About

Nginx stub_status as an exporter for Prometheus

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published