forked from Nomon/nomad-exporter
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
123 lines (102 loc) · 3.24 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
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
122
123
package main
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/hashicorp/nomad/api"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"gitlab.com/yakshaving.art/nomad-exporter/version"
)
func main() {
a := parseArgs()
if a.ShowVersion {
fmt.Println(version.GetVersion())
os.Exit(0)
}
if a.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
apiClient, err := api.NewClient(configureWith(a))
if err != nil {
logrus.Fatalf("could not create api client: %s", err)
}
exporter := &Exporter{
client: apiClient,
AllowStaleReads: a.AllowStaleReads,
PeerMetricsEnabled: !a.NoPeerMetricsEnabled,
SerfMetricsEnabled: !a.NoSerfMetricsEnabled,
NodeMetricsEnabled: !a.NoNodeMetricsEnabled,
JobMetricEnabled: !a.NoJobMetricsEnabled,
AllocationsMetricsEnabled: !a.NoAllocationsMetricsEnabled,
EvalMetricsEnabled: !a.NoEvalMetricsEnabled,
DeploymentMetricsEnabled: !a.NoDeploymentMetricsEnabled,
AllocationStatsMetricsEnabled: !a.NoAllocationStatsMetricsEnabled,
Concurrency: a.Concurrency,
}
prometheus.MustRegister(exporter)
http.HandleFunc("/", rootFunc(a.MetricsPath))
http.HandleFunc("/status", statusFunc(exporter))
http.Handle(a.MetricsPath, prometheus.Handler())
logrus.Println("Listening on", a.ListenAddress)
logrus.Fatal(http.ListenAndServe(a.ListenAddress, nil))
}
func rootFunc(metricsPath string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(`<html>
<head><title>Nomad Exporter</title></head>
<body>
<h1>Nomad Exporter</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>`))
}
}
func statusFunc(e *Exporter) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, _ *http.Request) {
err := e.Probe()
status := "UP"
if err != nil {
status = "DOWN"
w.WriteHeader(http.StatusServiceUnavailable)
}
w.Write([]byte(`<html>
<head><title>Nomad Exporter Status</title></head>
<body>
<h1>Nomad Exporter Status</h1>
<p><strong>` + status + `</strong></p>
</body>
</html>`))
}
}
func configureWith(a args) *api.Config {
timeout := time.Duration(a.NomadTimeout) * time.Millisecond
waitTime := time.Duration(a.NomadWaitTime) * time.Millisecond
cfg := api.DefaultConfig()
cfg.Address = a.NomadAddress
httpClient := cleanhttp.DefaultClient()
transport := httpClient.Transport.(*http.Transport)
transport.TLSHandshakeTimeout = 10 * time.Second
transport.TLSClientConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
httpClient.Timeout = timeout
cfg.HttpClient = httpClient
cfg.WaitTime = waitTime
if strings.HasPrefix(cfg.Address, "https://") {
cfg.TLSConfig.CACert = a.TLSCaFile
cfg.TLSConfig.CAPath = a.TLSCaPath
cfg.TLSConfig.ClientKey = a.TLSKey
cfg.TLSConfig.ClientCert = a.TLSCert
cfg.TLSConfig.Insecure = a.TLSInsecure
cfg.TLSConfig.TLSServerName = a.TLSServerName
if err := api.ConfigureTLS(httpClient, cfg.TLSConfig); err != nil {
logrus.Fatalf("failed to configure TLS: %s", err)
}
}
return cfg
}