Commit d7193027 authored by hnlq715's avatar hnlq715

#29 #55 add server info metric

parent 82382edc
......@@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof
nginx-vts-exporter
\ No newline at end of file
......@@ -90,11 +90,14 @@ For grafana dashboard please see [nginx-vts-exporter dashboard](https://grafana.
Nginx data | Name | Exposed informations
------------------ | ------------------------------- | ------------------------
**Info** | `{NAMESPACE}_server_info` | hostName, nginxVersion, uptimeSec |
**Connections** | `{NAMESPACE}_server_connections`| status [active, reading, writing, waiting, accepted, handled]
**Metrics output example**
``` txt
# Server Info
nginx_server_info{hostName="localhost", nginxVersion="1.11.1", uptimeSec="9527"} 1
# Server Connections
nginx_server_connections{status="accepted"} 70606
```
......
......@@ -9,6 +9,7 @@ import (
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
......@@ -17,6 +18,7 @@ import (
)
type NginxVts struct {
HostName string `json:"hostName"`
NginxVersion string `json:"nginxVersion"`
LoadMsec int64 `json:"loadMsec"`
NowMsec int64 `json:"nowMsec"`
......@@ -141,6 +143,7 @@ type Cache struct {
type Exporter struct {
URI string
infoMetric *prometheus.Desc
serverMetrics, upstreamMetrics, filterMetrics, cacheMetrics map[string]*prometheus.Desc
}
......@@ -174,7 +177,8 @@ func newCacheMetric(metricName string, docString string, labels []string) *prome
func NewExporter(uri string) *Exporter {
return &Exporter{
URI: uri,
URI: uri,
infoMetric: newServerMetric("info", "nginx info", []string{"hostName", "nginxVersion", "uptimeSec"}),
serverMetrics: map[string]*prometheus.Desc{
"connections": newServerMetric("connections", "nginx connections", []string{"status"}),
"requests": newServerMetric("requests", "requests counter", []string{"host", "code"}),
......@@ -237,6 +241,14 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
return
}
// info
uptime := nginxVtx.NowMsec - nginxVtx.LoadMsec
up := 0.0
if uptime > 0 {
up = 1.0
}
ch <- prometheus.MustNewConstMetric(e.infoMetric, prometheus.GaugeValue, up, nginxVtx.HostName, nginxVtx.NginxVersion, strconv.FormatInt(uptime/1000, 10))
// connections
ch <- prometheus.MustNewConstMetric(e.serverMetrics["connections"], prometheus.GaugeValue, float64(nginxVtx.Connections.Active), "active")
ch <- prometheus.MustNewConstMetric(e.serverMetrics["connections"], prometheus.GaugeValue, float64(nginxVtx.Connections.Reading), "reading")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment