-
Notifications
You must be signed in to change notification settings - Fork 17
/
bosh_exporter.go
376 lines (309 loc) · 11.1 KB
/
bosh_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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/cloudfoundry/bosh-cli/director"
"github.com/cloudfoundry/bosh-cli/uaa"
"github.com/cloudfoundry/bosh-utils/logger"
"github.com/cloudfoundry/bosh-utils/system"
"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"
"github.com/cloudfoundry/bosh_exporter/collectors"
"github.com/cloudfoundry/bosh_exporter/deployments"
"github.com/cloudfoundry/bosh_exporter/filters"
)
var (
boshURL = kingpin.Flag(
"bosh.url", "BOSH URL ($BOSH_EXPORTER_BOSH_URL)",
).Envar("BOSH_EXPORTER_BOSH_URL").Required().String()
boshUsername = kingpin.Flag(
"bosh.username", "BOSH Username ($BOSH_EXPORTER_BOSH_USERNAME)",
).Envar("BOSH_EXPORTER_BOSH_USERNAME").String()
boshPassword = kingpin.Flag(
"bosh.password", "BOSH Password ($BOSH_EXPORTER_BOSH_PASSWORD)",
).Envar("BOSH_EXPORTER_BOSH_PASSWORD").String()
boshUAAClientID = kingpin.Flag(
"bosh.uaa.client-id", "BOSH UAA Client ID ($BOSH_EXPORTER_BOSH_UAA_CLIENT_ID)",
).Envar("BOSH_EXPORTER_BOSH_UAA_CLIENT_ID").String()
boshUAAClientSecret = kingpin.Flag(
"bosh.uaa.client-secret", "BOSH UAA Client Secret ($BOSH_EXPORTER_BOSH_UAA_CLIENT_SECRET)",
).Envar("BOSH_EXPORTER_BOSH_UAA_CLIENT_SECRET").String()
boshLogLevel = kingpin.Flag(
"bosh.log-level", "BOSH Log Level ($BOSH_EXPORTER_BOSH_LOG_LEVEL)",
).Envar("BOSH_EXPORTER_BOSH_LOG_LEVEL").Default("ERROR").String()
boshCACertFile = kingpin.Flag(
"bosh.ca-cert-file", "BOSH CA Certificate file ($BOSH_EXPORTER_BOSH_CA_CERT_FILE)",
).Envar("BOSH_EXPORTER_BOSH_CA_CERT_FILE").Required().ExistingFile()
filterDeployments = kingpin.Flag(
"filter.deployments", "Comma separated deployments to filter ($BOSH_EXPORTER_FILTER_DEPLOYMENTS)",
).Envar("BOSH_EXPORTER_FILTER_DEPLOYMENTS").Default("").String()
filterAZs = kingpin.Flag(
"filter.azs", "Comma separated AZs to filter ($BOSH_EXPORTER_FILTER_AZS)",
).Envar("BOSH_EXPORTER_FILTER_AZS").Default("").String()
filterCollectors = kingpin.Flag(
"filter.collectors", "Comma separated collectors to filter (Deployments,Jobs,ServiceDiscovery) ($BOSH_EXPORTER_FILTER_COLLECTORS)",
).Envar("BOSH_EXPORTER_FILTER_COLLECTORS").Default("").String()
filterCIDRs = kingpin.Flag(
"filter.cidrs", "Comma separated CIDR to filter available instance IPs ($BOSH_EXPORTER_FILTER_CIDRS)",
).Envar("BOSH_EXPORTER_FILTER_CIDRS").Default("0.0.0.0/0").String()
metricsNamespace = kingpin.Flag(
"metrics.namespace", "Metrics Namespace ($BOSH_EXPORTER_METRICS_NAMESPACE)",
).Envar("BOSH_EXPORTER_METRICS_NAMESPACE").Default("bosh").String()
metricsEnvironment = kingpin.Flag(
"metrics.environment", "Environment label to be attached to metrics ($BOSH_EXPORTER_METRICS_ENVIRONMENT)",
).Envar("BOSH_EXPORTER_METRICS_ENVIRONMENT").Required().String()
sdFilename = kingpin.Flag(
"sd.filename", "Full path to the Service Discovery output file ($BOSH_EXPORTER_SD_FILENAME)",
).Envar("BOSH_EXPORTER_SD_FILENAME").Default("bosh_target_groups.json").String()
sdProcessesRegexp = kingpin.Flag(
"sd.processes_regexp", "Regexp to filter Service Discovery processes names ($BOSH_EXPORTER_SD_PROCESSES_REGEXP)",
).Envar("BOSH_EXPORTER_SD_PROCESSES_REGEXP").Default("").String()
listenAddress = kingpin.Flag(
"web.listen-address", "Address to listen on for web interface and telemetry ($BOSH_EXPORTER_WEB_LISTEN_ADDRESS)",
).Envar("BOSH_EXPORTER_WEB_LISTEN_ADDRESS").Default(":9190").String()
metricsPath = kingpin.Flag(
"web.telemetry-path", "Path under which to expose Prometheus metrics ($BOSH_EXPORTER_WEB_TELEMETRY_PATH)",
).Envar("BOSH_EXPORTER_WEB_TELEMETRY_PATH").Default("/metrics").String()
authUsername = kingpin.Flag(
"web.auth.username", "Username for web interface basic auth ($BOSH_EXPORTER_WEB_AUTH_USERNAME)",
).Envar("BOSH_EXPORTER_WEB_AUTH_USERNAME").String()
authPassword = kingpin.Flag(
"web.auth.password", "Password for web interface basic auth ($BOSH_EXPORTER_WEB_AUTH_PASSWORD)",
).Envar("BOSH_EXPORTER_WEB_AUTH_PASSWORD").String()
tlsCertFile = kingpin.Flag(
"web.tls.cert_file", "Path to a file that contains the TLS certificate (PEM format). If the certificate is signed by a certificate authority, the file should be the concatenation of the server's certificate, any intermediates, and the CA's certificate ($BOSH_EXPORTER_WEB_TLS_CERTFILE)",
).Envar("BOSH_EXPORTER_WEB_TLS_CERTFILE").ExistingFile()
tlsKeyFile = kingpin.Flag(
"web.tls.key_file", "Path to a file that contains the TLS private key (PEM format) ($BOSH_EXPORTER_WEB_TLS_KEYFILE)",
).Envar("BOSH_EXPORTER_WEB_TLS_KEYFILE").ExistingFile()
)
func init() {
prometheus.MustRegister(version.NewCollector(*metricsNamespace))
}
type basicAuthHandler struct {
handler http.HandlerFunc
username string
password string
}
func (h *basicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != h.username || password != h.password {
log.Errorf("Invalid HTTP auth from `%s`", r.RemoteAddr)
w.Header().Set("WWW-Authenticate", "Basic realm=\"metrics\"")
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}
h.handler(w, r)
}
type boshConfigUpdater struct{}
func (cu boshConfigUpdater) UpdateConfigWithToken(_ string, _ uaa.AccessToken) error {
return nil
}
func (cu boshConfigUpdater) Save() error {
return nil
}
func prometheusHandler() http.Handler {
handler := promhttp.Handler()
if *authUsername != "" && *authPassword != "" {
handler = &basicAuthHandler{
handler: promhttp.Handler().ServeHTTP,
username: *authUsername,
password: *authPassword,
}
}
return handler
}
func readCaCert(caCertFile string, logger logger.Logger) (string, error) {
if caCertFile != "" {
fs := system.NewOsFileSystem(logger)
CACertFileFullPath, err := fs.ExpandPath(caCertFile)
if err != nil {
return "", err
}
CACert, err := fs.ReadFileString(CACertFileFullPath)
if err != nil {
return "", err
}
return CACert, nil
}
return "", nil
}
func buildBOSHClient() (director.Director, error) {
logLevel, err := logger.Levelify(*boshLogLevel)
if err != nil {
return nil, err
}
logger := logger.NewLogger(logLevel)
directorConfig, err := director.NewConfigFromURL(*boshURL)
if err != nil {
return nil, err
}
boshCACert, err := readCaCert(*boshCACertFile, logger)
if err != nil {
return nil, err
}
directorConfig.CACert = boshCACert
anonymousDirector, err := director.NewFactory(logger).New(directorConfig, nil, nil)
if err != nil {
return nil, err
}
boshInfo, err := anonymousDirector.Info()
if err != nil {
return nil, err
}
if boshInfo.Auth.Type != "uaa" {
directorConfig.Client = *boshUsername
directorConfig.ClientSecret = *boshPassword
} else {
uaaURL := boshInfo.Auth.Options["url"]
uaaURLStr, ok := uaaURL.(string)
if !ok {
return nil, fmt.Errorf("expected UAA URL '%s' to be a string", uaaURL)
}
uaaConfig, err := uaa.NewConfigFromURL(uaaURLStr)
if err != nil {
return nil, err
}
uaaConfig.CACert = boshCACert
if *boshUAAClientID != "" && *boshUAAClientSecret != "" {
uaaConfig.Client = *boshUAAClientID
uaaConfig.ClientSecret = *boshUAAClientSecret
} else {
uaaConfig.Client = "bosh_cli"
}
uaaFactory := uaa.NewFactory(logger)
uaaClient, err := uaaFactory.New(uaaConfig)
if err != nil {
return nil, err
}
if *boshUAAClientID != "" && *boshUAAClientSecret != "" {
directorConfig.TokenFunc = uaa.NewClientTokenSession(uaaClient).TokenFunc
} else {
answers := []uaa.PromptAnswer{
{
Key: "username",
Value: *boshUsername,
},
{
Key: "password",
Value: *boshPassword,
},
}
accessToken, err := uaaClient.OwnerPasswordCredentialsGrant(answers)
if err != nil {
return nil, err
}
refreshToken := ""
if refreshableToken, ok := accessToken.(uaa.RefreshableAccessToken); ok {
refreshToken = refreshableToken.RefreshValue()
}
origToken := uaa.NewRefreshableAccessToken(accessToken.Type(), accessToken.Value(), refreshToken)
directorConfig.TokenFunc = uaa.NewAccessTokenSession(uaaClient, origToken, boshConfigUpdater{}, "").TokenFunc
}
}
boshFactory := director.NewFactory(logger)
boshClient, err := boshFactory.New(directorConfig, director.NewNoopTaskReporter(), director.NewNoopFileReporter())
if err != nil {
return nil, err
}
return boshClient, nil
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("fbosh_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting bosh_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
boshClient, err := buildBOSHClient()
if err != nil {
log.Errorf("Error creating BOSH Client: %s", err.Error())
os.Exit(1)
}
boshInfo, err := boshClient.Info()
if err != nil {
log.Errorf("Error reading BOSH Info: %s", err.Error())
os.Exit(1)
}
log.Infof("Using BOSH Director `%s` (%s)", boshInfo.Name, boshInfo.UUID)
var deploymentsFilters []string
if *filterDeployments != "" {
deploymentsFilters = strings.Split(*filterDeployments, ",")
}
deploymentsFilter := filters.NewDeploymentsFilter(deploymentsFilters, boshClient)
deploymentsFetcher := deployments.NewFetcher(*deploymentsFilter)
var azsFilters []string
if *filterAZs != "" {
azsFilters = strings.Split(*filterAZs, ",")
}
azsFilter := filters.NewAZsFilter(azsFilters)
var collectorsFilters []string
if *filterCollectors != "" {
collectorsFilters = strings.Split(*filterCollectors, ",")
}
collectorsFilter, err := filters.NewCollectorsFilter(collectorsFilters)
if err != nil {
log.Error(err)
os.Exit(1)
}
var cidrFilters []string
if *filterCIDRs != "" {
cidrFilters = strings.Split(*filterCIDRs, ",")
}
cidrsFilter, err := filters.NewCidrFilter(cidrFilters)
if err != nil {
log.Error(err)
os.Exit(1)
}
var processesFilters []string
if *sdProcessesRegexp != "" {
processesFilters = []string{*sdProcessesRegexp}
}
processesFilter, err := filters.NewRegexpFilter(processesFilters)
if err != nil {
log.Errorf("Error processing Processes Regexp: %v", err)
os.Exit(1)
}
boshCollector := collectors.NewBoshCollector(
*metricsNamespace,
*metricsEnvironment,
boshInfo.Name,
boshInfo.UUID,
*sdFilename,
deploymentsFetcher,
collectorsFilter,
azsFilter,
processesFilter,
cidrsFilter,
)
prometheus.MustRegister(boshCollector)
http.Handle(*metricsPath, prometheusHandler())
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>BOSH Exporter</title></head>
<body>
<h1>BOSH Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
server := &http.Server{
Addr: *listenAddress,
ReadTimeout: time.Second * 5,
ReadHeaderTimeout: time.Second * 10,
}
if *tlsCertFile != "" && *tlsKeyFile != "" {
log.Infoln("Listening TLS on", *listenAddress)
err = server.ListenAndServeTLS(*tlsCertFile, *tlsKeyFile)
} else {
log.Infoln("Listening on", *listenAddress)
err = server.ListenAndServe()
}
log.Fatal(err)
}