forked from cloudfoundry/cf_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_exporter.go
242 lines (197 loc) · 9.46 KB
/
cf_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
package main
import (
"fmt"
"net/http"
"os"
"strings"
cfclient "github.com/cloudfoundry-community/go-cfclient"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/bosh-prometheus/cf_exporter/collectors"
"github.com/bosh-prometheus/cf_exporter/filters"
)
var (
cfAPIUrl = kingpin.Flag(
"cf.api_url", "Cloud Foundry API URL ($CF_EXPORTER_CF_API_URL)",
).Envar("CF_EXPORTER_CF_API_URL").Required().String()
cfUsername = kingpin.Flag(
"cf.username", "Cloud Foundry Username ($CF_EXPORTER_CF_USERNAME)",
).Envar("CF_EXPORTER_CF_USERNAME").String()
cfPassword = kingpin.Flag(
"cf.password", "Cloud Foundry Password ($CF_EXPORTER_CF_PASSWORD)",
).Envar("CF_EXPORTER_CF_PASSWORD").String()
cfClientID = kingpin.Flag(
"cf.client-id", "Cloud Foundry Client ID ($CF_EXPORTER_CF_CLIENT_ID)",
).Envar("CF_EXPORTER_CF_CLIENT_ID").String()
cfClientSecret = kingpin.Flag(
"cf.client-secret", "Cloud Foundry Client Secret ($CF_EXPORTER_CF_CLIENT_SECRET)",
).Envar("CF_EXPORTER_CF_CLIENT_SECRET").String()
cfDeploymentName = kingpin.Flag(
"cf.deployment-name", "Cloud Foundry Deployment Name to be reported as a metric label ($CF_EXPORTER_CF_DEPLOYMENT_NAME)",
).Envar("CF_EXPORTER_CF_DEPLOYMENT_NAME").Required().String()
cfAPIV3Enabled = kingpin.Flag(
"cf.api-v3-enabled", "Enable Cloud Foundry API V3 calls ($CF_EXPORTER_CF_API_V3_ENABLED)",
).Envar("CF_EXPORTER_CF_API_V3_ENABLED").Default("false").Bool()
eventsQuery = kingpin.Flag(
"events.query", "When the Events filter is enabled and this value is set, this query is sent to the CloudController to limit the number of results returned. Syntax is exactly as documented at the Cloud Foundry API ($CF_EXPORTER_EVENTS_QUERY)",
).Envar("CF_EXPORTER_EVENTS_QUERY").Default("").String()
filterCollectors = kingpin.Flag(
"filter.collectors", "Comma separated collectors to filter (Applications,Events,IsolationSegments,Organizations,Routes,SecurityGroups,ServiceBindings,ServiceInstances,ServicePlans,Services,Spaces,Stacks). If not set, all collectors except Events is enabled ($CF_EXPORTER_FILTER_COLLECTORS)",
).Envar("CF_EXPORTER_FILTER_COLLECTORS").Default("").String()
metricsNamespace = kingpin.Flag(
"metrics.namespace", "Metrics Namespace ($CF_EXPORTER_METRICS_NAMESPACE)",
).Envar("CF_EXPORTER_METRICS_NAMESPACE").Default("cf").String()
metricsEnvironment = kingpin.Flag(
"metrics.environment", "Environment label to be attached to metrics ($CF_EXPORTER_METRICS_ENVIRONMENT)",
).Envar("CF_EXPORTER_METRICS_ENVIRONMENT").Required().String()
skipSSLValidation = kingpin.Flag(
"skip-ssl-verify", "Disable SSL Verify ($CF_EXPORTER_SKIP_SSL_VERIFY)",
).Envar("CF_EXPORTER_SKIP_SSL_VERIFY").Default("false").Bool()
listenAddress = kingpin.Flag(
"web.listen-address", "Address to listen on for web interface and telemetry ($CF_EXPORTER_WEB_LISTEN_ADDRESS)",
).Envar("CF_EXPORTER_WEB_LISTEN_ADDRESS").Default(":9193").String()
metricsPath = kingpin.Flag(
"web.telemetry-path", "Path under which to expose Prometheus metrics ($CF_EXPORTER_WEB_TELEMETRY_PATH)",
).Envar("CF_EXPORTER_WEB_TELEMETRY_PATH").Default("/metrics").String()
authUsername = kingpin.Flag(
"web.auth.username", "Username for web interface basic auth ($CF_EXPORTER_WEB_AUTH_USERNAME)",
).Envar("CF_EXPORTER_WEB_AUTH_USERNAME").String()
authPassword = kingpin.Flag(
"web.auth.password", "Password for web interface basic auth ($CF_EXPORTER_WEB_AUTH_PASSWORD)",
).Envar("CF_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 ($CF_EXPORTER_WEB_TLS_CERTFILE)",
).Envar("CF_EXPORTER_WEB_TLS_KEYFILE").ExistingFile()
tlsKeyFile = kingpin.Flag(
"web.tls.key_file", "Path to a file that contains the TLS private key (PEM format) ($CF_EXPORTER_WEB_TLS_KEYFILE)",
).Envar("CF_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)
return
}
func prometheusHandler() http.Handler {
handler := prometheus.Handler()
if *authUsername != "" && *authPassword != "" {
handler = &basicAuthHandler{
handler: prometheus.Handler().ServeHTTP,
username: *authUsername,
password: *authPassword,
}
}
return handler
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("cf_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting cf_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
cfConfig := &cfclient.Config{
ApiAddress: *cfAPIUrl,
Username: *cfUsername,
Password: *cfPassword,
ClientID: *cfClientID,
ClientSecret: *cfClientSecret,
SkipSslValidation: *skipSSLValidation,
UserAgent: fmt.Sprintf("cf_exporter/%s", version.Version),
}
cfClient, err := cfclient.NewClient(cfConfig)
if err != nil {
log.Errorf("Error creating Cloud Foundry client: %s", err.Error())
os.Exit(1)
}
var collectorsFilters []string
if *filterCollectors != "" {
collectorsFilters = strings.Split(*filterCollectors, ",")
}
collectorsFilter, err := filters.NewCollectorsFilter(collectorsFilters, *cfAPIV3Enabled)
if err != nil {
log.Error(err)
os.Exit(1)
}
if collectorsFilter.Enabled(filters.ApplicationsCollector) {
applicationsCollector := collectors.NewApplicationsCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(applicationsCollector)
}
if collectorsFilter.Enabled(filters.IsolationSegmentsCollector) {
isolationSegmentsCollector := collectors.NewIsolationSegmentsCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(isolationSegmentsCollector)
}
if collectorsFilter.Enabled(filters.OrganizationsCollector) {
organizationsCollector := collectors.NewOrganizationsCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(organizationsCollector)
}
if collectorsFilter.Enabled(filters.RoutesCollector) {
routesCollector := collectors.NewRoutesCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(routesCollector)
}
if collectorsFilter.Enabled(filters.SecurityGroupsCollector) {
securityGroupsCollector := collectors.NewSecurityGroupsCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(securityGroupsCollector)
}
if collectorsFilter.Enabled(filters.ServiceBindingsCollector) {
serviceBindingsCollector := collectors.NewServiceBindingsCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(serviceBindingsCollector)
}
if collectorsFilter.Enabled(filters.ServiceInstancesCollector) {
serviceInstancesCollector := collectors.NewServiceInstancesCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(serviceInstancesCollector)
}
if collectorsFilter.Enabled(filters.ServicePlansCollector) {
servicePlansCollector := collectors.NewServicePlansCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(servicePlansCollector)
}
if collectorsFilter.Enabled(filters.ServicesCollector) {
servicesCollector := collectors.NewServicesCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(servicesCollector)
}
if collectorsFilter.Enabled(filters.SpacesCollector) {
spacesCollector := collectors.NewSpacesCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(spacesCollector)
}
if collectorsFilter.Enabled(filters.StacksCollector) {
stacksCollector := collectors.NewStacksCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient)
prometheus.MustRegister(stacksCollector)
}
if collectorsFilter.Enabled(filters.EventsCollector) {
eventsCollector := collectors.NewEventsCollector(*metricsNamespace, *metricsEnvironment, *cfDeploymentName, cfClient, *eventsQuery)
prometheus.MustRegister(eventsCollector)
}
handler := prometheusHandler()
http.Handle(*metricsPath, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Cloud Foundry Exporter</title></head>
<body>
<h1>Cloud Foundry Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
if *tlsCertFile != "" && *tlsKeyFile != "" {
log.Infoln("Listening TLS on", *listenAddress)
log.Fatal(http.ListenAndServeTLS(*listenAddress, *tlsCertFile, *tlsKeyFile, nil))
} else {
log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
}