forked from nginx/nginx-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.go
121 lines (104 loc) · 3.78 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"crypto/tls"
"flag"
"log"
"net/http"
"os"
"strconv"
"time"
plusclient "github.com/nginxinc/nginx-plus-go-sdk/client"
"github.com/nginxinc/nginx-prometheus-exporter/client"
"github.com/nginxinc/nginx-prometheus-exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func getEnv(key, defaultValue string) string {
value, ok := os.LookupEnv(key)
if !ok {
return defaultValue
}
return value
}
func getEnvBool(key string, defaultValue bool) bool {
value, ok := os.LookupEnv(key)
if !ok {
return defaultValue
}
b, err := strconv.ParseBool(value)
if err != nil {
log.Fatalf("Environment Variable value for %s must be a boolean", key)
}
return b
}
var (
// Set during go build
version string
gitCommit string
// Defaults values
defaultListenAddress = getEnv("LISTEN_ADDRESS", ":9113")
defaultMetricsPath = getEnv("TELEMETRY_PATH", "/metrics")
defaultNginxPlus = getEnvBool("NGINX_PLUS", false)
defaultScrapeURI = getEnv("SCRAPE_URI", "http://127.0.0.1:8080/stub_status")
defaultSslVerify = getEnvBool("SSL_VERIFY", true)
// Command-line flags
listenAddr = flag.String("web.listen-address", defaultListenAddress,
"An address to listen on for web interface and telemetry. The default value can be overwritten by LISTEN_ADDRESS environment variable.")
metricsPath = flag.String("web.telemetry-path", defaultMetricsPath,
"A path under which to expose metrics. The default value can be overwritten by TELEMETRY_PATH environment variable.")
nginxPlus = flag.Bool("nginx.plus", defaultNginxPlus,
"Start the exporter for NGINX Plus. By default, the exporter is started for NGINX. The default value can be overwritten by NGINX_PLUS environment variable.")
scrapeURI = flag.String("nginx.scrape-uri", defaultScrapeURI,
`A URI for scraping NGINX or NGINX Plus metrics.
For NGINX, the stub_status page must be available through the URI. For NGINX Plus -- the API. The default value can be overwritten by SCRAPE_URI environment variable.`)
sslVerify = flag.Bool("nginx.ssl-verify", defaultSslVerify,
"Perform SSL certificate verification. The default value can be overwritten by SSL_VERIFY environment variable.")
timeout = flag.Duration("nginx.timeout", 5*time.Second, "A timeout for scraping metrics from NGINX or NGINX Plus.")
)
func main() {
flag.Parse()
log.Printf("Starting NGINX Prometheus Exporter Version=%v GitCommit=%v", version, gitCommit)
registry := prometheus.NewRegistry()
buildInfoMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "nginxexporter_build_info",
Help: "Exporter build information",
ConstLabels: prometheus.Labels{
"version": version,
"gitCommit": gitCommit,
},
},
)
buildInfoMetric.Set(1)
registry.MustRegister(buildInfoMetric)
httpClient := &http.Client{
Timeout: *timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !*sslVerify},
},
}
if *nginxPlus {
client, err := plusclient.NewNginxClient(httpClient, *scrapeURI)
if err != nil {
log.Fatalf("Could not create Nginx Plus Client: %v", err)
}
registry.MustRegister(collector.NewNginxPlusCollector(client, "nginxplus"))
} else {
client, err := client.NewNginxClient(httpClient, *scrapeURI)
if err != nil {
log.Fatalf("Could not create Nginx Client: %v", err)
}
registry.MustRegister(collector.NewNginxCollector(client, "nginx"))
}
http.Handle(*metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>NGINX Exporter</title></head>
<body>
<h1>NGINX Exporter</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddr, nil))
}