This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
263 lines (238 loc) · 7.43 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"net/http"
_ "net/http/pprof"
vault_api "github.com/hashicorp/vault/api"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
listenAddress = kingpin.Flag("web.listen-address",
"Address to listen on for web interface and telemetry.").
Default(":9410").String()
metricsPath = kingpin.Flag("web.telemetry-path",
"Path under which to expose metrics.").
Default("/metrics").String()
vaultCACert = kingpin.Flag("vault-tls-cacert",
"The path to a PEM-encoded CA cert file to use to verify the Vault server SSL certificate.").String()
vaultClientCert = kingpin.Flag("vault-tls-client-cert",
"The path to the certificate for Vault communication.").String()
vaultClientKey = kingpin.Flag("vault-tls-client-key",
"The path to the private key for Vault communication.").String()
sslInsecure = kingpin.Flag("insecure-ssl",
"Set SSL to ignore certificate validation.").
Default("false").Bool()
)
const (
namespace = "vault"
)
var (
up = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Was the last query of Vault successful.",
nil, nil,
)
initialized = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "initialized"),
"Is the Vault initialised (according to this node).",
nil, nil,
)
sealed = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "sealed"),
"Is the Vault node sealed.",
nil, nil,
)
standby = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "standby"),
"Is this Vault node in standby.",
nil, nil,
)
replicationDrPrimary = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "replication_dr_primary"),
"Is this Vault node a primary disaster recovery replica.",
nil, nil,
)
replicationDrSecondary = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "replication_dr_secondary"),
"Is this Vault node a secondary disaster recovery replica.",
nil, nil,
)
replicationPerformancePrimary = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "replication_performance_primary"),
"Is this Vault node a primary performance replica.",
nil, nil,
)
replicationPerformanceSecondary = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "replication_performance_secondary"),
"Is this Vault node a secondary performance replica.",
nil, nil,
)
info = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "info"),
"Version of this Vault node.",
[]string{"version", "cluster_name", "cluster_id"}, nil,
)
)
// Exporter collects Vault health from the given server and exports them using
// the Prometheus metrics package.
type Exporter struct {
client *vault_api.Client
}
// NewExporter returns an initialized Exporter.
func NewExporter() (*Exporter, error) {
vaultConfig := vault_api.DefaultConfig()
if *sslInsecure {
tlsconfig := &vault_api.TLSConfig{
Insecure: true,
}
err := vaultConfig.ConfigureTLS(tlsconfig)
if err != nil {
return nil, err
}
}
if *vaultCACert != "" || *vaultClientCert != "" || *vaultClientKey != "" {
tlsconfig := &vault_api.TLSConfig{
CACert: *vaultCACert,
ClientCert: *vaultClientCert,
ClientKey: *vaultClientKey,
Insecure: *sslInsecure,
}
err := vaultConfig.ConfigureTLS(tlsconfig)
if err != nil {
return nil, err
}
}
client, err := vault_api.NewClient(vaultConfig)
if err != nil {
return nil, err
}
return &Exporter{
client: client,
}, nil
}
// Describe describes all the metrics ever exported by the Vault exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- up
ch <- initialized
ch <- sealed
ch <- standby
ch <- replicationDrPrimary
ch <- replicationDrSecondary
ch <- replicationPerformancePrimary
ch <- replicationPerformanceSecondary
ch <- info
}
func bool2float(b bool) float64 {
if b {
return 1
}
return 0
}
// Collect fetches the stats from configured Vault and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
health, err := e.client.Sys().Health()
if err != nil {
ch <- prometheus.MustNewConstMetric(
up, prometheus.GaugeValue, 0,
)
log.Errorf("Failed to collect health from Vault server: %v", err)
return
}
ch <- prometheus.MustNewConstMetric(
up, prometheus.GaugeValue, 1,
)
ch <- prometheus.MustNewConstMetric(
initialized, prometheus.GaugeValue, bool2float(health.Initialized),
)
ch <- prometheus.MustNewConstMetric(
sealed, prometheus.GaugeValue, bool2float(health.Sealed),
)
ch <- prometheus.MustNewConstMetric(
standby, prometheus.GaugeValue, bool2float(health.Standby),
)
if health.ReplicationDRMode == "disabled" {
ch <- prometheus.MustNewConstMetric(
replicationDrPrimary, prometheus.GaugeValue, 0,
)
ch <- prometheus.MustNewConstMetric(
replicationDrSecondary, prometheus.GaugeValue, 0,
)
} else if health.ReplicationDRMode == "primary" {
ch <- prometheus.MustNewConstMetric(
replicationDrPrimary, prometheus.GaugeValue, 1,
)
ch <- prometheus.MustNewConstMetric(
replicationDrSecondary, prometheus.GaugeValue, 0,
)
} else if health.ReplicationDRMode == "secondary" {
ch <- prometheus.MustNewConstMetric(
replicationDrPrimary, prometheus.GaugeValue, 0,
)
ch <- prometheus.MustNewConstMetric(
replicationDrSecondary, prometheus.GaugeValue, 1,
)
}
if health.ReplicationPerformanceMode == "disabled" {
ch <- prometheus.MustNewConstMetric(
replicationPerformancePrimary, prometheus.GaugeValue, 0,
)
ch <- prometheus.MustNewConstMetric(
replicationPerformanceSecondary, prometheus.GaugeValue, 0,
)
} else if health.ReplicationPerformanceMode == "primary" {
ch <- prometheus.MustNewConstMetric(
replicationPerformancePrimary, prometheus.GaugeValue, 1,
)
ch <- prometheus.MustNewConstMetric(
replicationPerformanceSecondary, prometheus.GaugeValue, 0,
)
} else if health.ReplicationPerformanceMode == "secondary" {
ch <- prometheus.MustNewConstMetric(
replicationPerformancePrimary, prometheus.GaugeValue, 0,
)
ch <- prometheus.MustNewConstMetric(
replicationPerformanceSecondary, prometheus.GaugeValue, 1,
)
}
ch <- prometheus.MustNewConstMetric(
info, prometheus.GaugeValue, 1, health.Version, health.ClusterName, health.ClusterID,
)
}
func init() {
prometheus.MustRegister(version.NewCollector("vault_exporter"))
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("vault_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting vault_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
exporter, err := NewExporter()
if err != nil {
log.Fatalln(err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>Vault Exporter</title></head>
<body>
<h1>Vault Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<h2>Build</h2>
<pre>` + version.Info() + ` ` + version.BuildContext() + `</pre>
</body>
</html>`))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}