-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
87 lines (72 loc) · 2.56 KB
/
main.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
package main
import (
"flag"
"bytes"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
"github.com/prometheus/common/expfmt"
"github.com/gjflsl/nginx_exporter/ngx_status_exporter"
req_status_exporter "github.com/gjflsl/nginx_exporter/req_status_exporter"
)
const (
namespace = "nginx" // For Prometheus metrics.
)
var (
listeningAddress = flag.String("telemetry.address", ":9113", "Address on which to expose metrics.")
metricsEndpoint = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics.")
)
func main() {
flag.Parse()
log.Printf("Starting Server: %s", *listeningAddress)
http.Handle(*metricsEndpoint, prometheus.Handler())
http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
nginxStatus := params.Get("module")
nginxScrapeURI := params.Get("target")
if nginxStatus == "" {
nginxStatus = "ngx_status"
}
if nginxScrapeURI == "" {
http.Error(w, "Url parameter is missing", 400)
return
}
if nginxStatus == "ngx_status" || nginxStatus == "" {
exp := ngx_status_exporter.NewNgxStatusExporter(nginxScrapeURI, true)
registry := prometheus.NewRegistry()
registry.Register(exp)
mfs, err := registry.Gather()
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
for _, mf := range mfs {
if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil {
log.Fatal(err)
}
}
w.Write(buf.Bytes())
} else if nginxStatus == "req_status" {
exp := req_status_exporter.NewReqStatusExporter(nginxScrapeURI, true)
registry := prometheus.NewRegistry()
registry.Register(exp)
mfs, err := registry.Gather()
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
for _, mf := range mfs {
if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil {
log.Fatal(err)
}
}
w.Write(buf.Bytes())
} else {
http.Error(w, "Type parameter is error", 400)
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>what the fuck!</html>`))
})
log.Fatal(http.ListenAndServe(*listeningAddress, nil))
}