diff --git a/README.md b/README.md index 05e4ffb4..322c4c38 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,11 @@ Usage of ./nginx-prometheus-exporter: ### Exported Metrics -* For NGINX, all stub_status metrics are exported. Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their descriptions. +* For NGINX, the following metrics are exported: + * All [stub_status](http://nginx.org/en/docs/http/ngx_http_stub_status_module.html) metrics. + * `nginx_up` -- shows the status of the last metric scrape: `1` for a successful scrape and `0` for a failed one. + + Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their descriptions. * For NGINX Plus, the following metrics are exported: * [Connections](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_connections). * [HTTP](http://nginx.org/en/docs/http/ngx_http_api_module.html#http_). @@ -86,6 +90,7 @@ Usage of ./nginx-prometheus-exporter: * [Stream Server Zones](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_stream_server_zone). * [HTTP Upstreams](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_http_upstream). Note: for the `state` metric, the string values are converted to float64 using the following rule: `"up"` -> `1.0`, `"draining"` -> `2.0`, `"down"` -> `3.0`, `"unavail"` –> `4.0`, `"checking"` –> `5.0`, `"unhealthy"` -> `6.0`. * [Stream Upstreams](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_stream_upstream). Note: for the `state` metric, the string values are converted to float64 using the following rule: `"up"` -> `1.0`, `"down"` -> `3.0`, `"unavail"` –> `4.0`, `"checking"` –> `5.0`, `"unhealthy"` -> `6.0`. + * `nginxplus_up` -- shows the status of the last metric scrape: `1` for a successful scrape and `0` for a failed one. Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their descriptions. Note: to see server zones related metrics you must configure [status zones](https://nginx.org/en/docs/http/ngx_http_status_module.html#status_zone) and to see upstream related metrics you must configure upstreams with a [shared memory zone](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#zone). diff --git a/collector/helper.go b/collector/helper.go index c739a14d..6fcd67e3 100644 --- a/collector/helper.go +++ b/collector/helper.go @@ -2,6 +2,17 @@ package collector import "github.com/prometheus/client_golang/prometheus" +const nginxUp = 1 +const nginxDown = 0 + func newGlobalMetric(namespace string, metricName string, docString string) *prometheus.Desc { return prometheus.NewDesc(namespace+"_"+metricName, docString, nil, nil) } + +func newUpMetric(namespace string) prometheus.Gauge { + return prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: namespace, + Name: "up", + Help: "Status of the last metric scrape", + }) +} diff --git a/collector/nginx.go b/collector/nginx.go index 3325ca8d..2ac0bca0 100644 --- a/collector/nginx.go +++ b/collector/nginx.go @@ -12,6 +12,7 @@ import ( type NginxCollector struct { nginxClient *client.NginxClient metrics map[string]*prometheus.Desc + upMetric prometheus.Gauge mutex sync.Mutex } @@ -28,12 +29,15 @@ func NewNginxCollector(nginxClient *client.NginxClient, namespace string) *Nginx "connections_waiting": newGlobalMetric(namespace, "connections_waiting", "Idle client connections"), "http_requests_total": newGlobalMetric(namespace, "http_requests_total", "Total http requests"), }, + upMetric: newUpMetric(namespace), } } // Describe sends the super-set of all possible descriptors of NGINX metrics // to the provided channel. func (c *NginxCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- c.upMetric.Desc() + for _, m := range c.metrics { ch <- m } @@ -46,10 +50,15 @@ func (c *NginxCollector) Collect(ch chan<- prometheus.Metric) { stats, err := c.nginxClient.GetStubStats() if err != nil { + c.upMetric.Set(nginxDown) + ch <- c.upMetric log.Printf("Error getting stats: %v", err) return } + c.upMetric.Set(nginxUp) + ch <- c.upMetric + ch <- prometheus.MustNewConstMetric(c.metrics["connections_active"], prometheus.GaugeValue, float64(stats.Connections.Active)) ch <- prometheus.MustNewConstMetric(c.metrics["connections_accepted"], diff --git a/collector/nginx_plus.go b/collector/nginx_plus.go index 35f616aa..01524aa7 100644 --- a/collector/nginx_plus.go +++ b/collector/nginx_plus.go @@ -18,6 +18,7 @@ type NginxPlusCollector struct { streamServerZoneMetrics map[string]*prometheus.Desc streamUpstreamMetrics map[string]*prometheus.Desc streamUpstreamServerMetrics map[string]*prometheus.Desc + upMetric prometheus.Gauge mutex sync.Mutex } @@ -99,12 +100,15 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string "health_checks_fails": newStreamUpstreamServerMetric(namespace, "health_checks_fails", "Failed health checks"), "health_checks_unhealthy": newStreamUpstreamServerMetric(namespace, "health_checks_unhealthy", "How many times the server became unhealthy (state 'unhealthy')"), }, + upMetric: newUpMetric(namespace), } } // Describe sends the super-set of all possible descriptors of NGINX Plus metrics // to the provided channel. func (c *NginxPlusCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- c.upMetric.Desc() + for _, m := range c.totalMetrics { ch <- m } @@ -135,10 +139,15 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) { stats, err := c.nginxClient.GetStats() if err != nil { + c.upMetric.Set(nginxDown) + ch <- c.upMetric log.Printf("Error getting stats: %v", err) return } + c.upMetric.Set(nginxUp) + ch <- c.upMetric + ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_accepted"], prometheus.CounterValue, float64(stats.Connections.Accepted)) ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_dropped"],