forked from giantswarm/vault-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (185 loc) · 5.36 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
package main
import (
"net/http"
"os"
"github.com/giantswarm/microerror"
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,
)
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, microerror.Mask(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, microerror.Mask(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 <- 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),
)
ch <- prometheus.MustNewConstMetric(
info, prometheus.GaugeValue, 1, health.Version, health.ClusterName, health.ClusterID,
)
}
func init() {
prometheus.MustRegister(version.NewCollector("vault_exporter"))
}
func main() {
err := mainE()
if err != nil {
panic(microerror.JSON(err))
}
}
func mainE() error {
if (len(os.Args) > 1) && (os.Args[1] == "version") {
version.Print("vault_exporter")
return nil
}
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 {
return microerror.Mask(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)
err = http.ListenAndServe(*listenAddress, nil)
if err != nil {
return microerror.Mask(err)
}
return nil
}