forked from openstack-exporter/openstack-exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
213 lines (179 loc) · 7.9 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
package main
import (
"context"
"fmt"
"net"
"net/http"
"os"
"strings"
"github.com/openstack-exporter/openstack-exporter/exporters"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
var defaultEnabledServices = []string{"network", "compute", "computeWithTeam", "image", "volume", "identity", "object-store", "load-balancer", "container-infra", "dns", "baremetal", "gnocchi", "database", "orchestration", "placement"}
var DEFAULT_OS_CLIENT_CONFIG = "/etc/openstack/clouds.yaml"
var (
logLevel = kingpin.Flag("log.level", "Log level: [debug, info, warn, error, fatal]").Default("info").String()
bind = kingpin.Flag("web.listen-address", "address:port to listen on").Default(":9180").String()
metrics = kingpin.Flag("web.telemetry-path", "uri path to expose metrics").Default("/metrics").String()
osClientConfig = kingpin.Flag("os-client-config", "Path to the cloud configuration file").Default(DEFAULT_OS_CLIENT_CONFIG).String()
prefix = kingpin.Flag("prefix", "Prefix for metrics").Default("openstack").String()
endpointType = kingpin.Flag("endpoint-type", "openstack endpoint type to use (i.e: public, internal, admin)").Default("public").String()
collectTime = kingpin.Flag("collect-metric-time", "time spent collecting each metric").Default("false").Bool()
disabledMetrics = kingpin.Flag("disable-metric", "multiple --disable-metric can be specified in the format: service-metric (i.e: cinder-snapshots)").Default("").Short('d').Strings()
disableSlowMetrics = kingpin.Flag("disable-slow-metrics", "Disable slow metrics for performance reasons").Default("false").Bool()
disableDeprecatedMetrics = kingpin.Flag("disable-deprecated-metrics", "Disable deprecated metrics").Default("false").Bool()
disableCinderAgentUUID = kingpin.Flag("disable-cinder-agent-uuid", "Disable UUID generation for Cinder agents").Default("false").Bool()
cloud = kingpin.Arg("cloud", "name or id of the cloud to gather metrics from").String()
multiCloud = kingpin.Flag("multi-cloud", "Toggle the multiple cloud scraping mode under /probe?cloud=").Default("false").Bool()
domainID = kingpin.Flag("domain-id", "Gather metrics only for the given Domain ID (defaults to all domains)").String()
)
func main() {
services := make(map[string]*bool)
for _, service := range defaultEnabledServices {
flagName := fmt.Sprintf("disable-service.%s", service)
flagHelp := fmt.Sprintf("Disable the %s service exporter", service)
services[service] = kingpin.Flag(flagName, flagHelp).Default().Bool()
}
kingpin.Version(version.Print("openstack-exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
if *cloud == "" && !*multiCloud {
log.Fatalln("openstack-exporter: error: required argument 'cloud' or flag --multi-cloud not provided, try --help")
}
err := log.Base().SetLevel(*logLevel)
if err != nil {
log.Errorf("Cannot init set logger level: %s", err)
os.Exit(-1)
}
log.Infoln("Build context", version.BuildContext())
if *osClientConfig != DEFAULT_OS_CLIENT_CONFIG {
log.Debugf("Setting Env var OS_CLIENT_CONFIG_FILE = %s", *osClientConfig)
os.Setenv("OS_CLIENT_CONFIG_FILE", *osClientConfig)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>OpenStack Exporter</title></head>
<body>
<h1>OpenStack Exporter</h1>
<p><a href='` + *metrics + `'>Metrics</a></p>
</body>
</html>`))
if err != nil {
log.Error(err)
}
})
if *multiCloud {
http.HandleFunc("/probe", probeHandler(services))
http.Handle("/metrics", promhttp.Handler())
log.Infoln("openstack exporter started in multi cloud mode (/probe?cloud=)")
} else {
log.Infoln("openstack exporter started in legacy mode")
http.HandleFunc(*metrics, metricHandler(services))
}
if *bind == "" {
log.Info("--web.listen-address is empty. HTTP server will start on :9180")
*bind = ":9180"
}
if *domainID != "" {
log.Infoln("Gathering metrics for domain ID", *domainID)
}
tcp := exporters.IP4or6(*bind)
l, err := net.Listen(tcp, *bind)
if err != nil {
log.Fatal(err)
}
log.Infoln("Starting HTTP server on", *bind)
log.Fatal(http.Serve(l, nil))
}
func probeHandler(services map[string]*bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
r = r.WithContext(ctx)
registry := prometheus.NewPedanticRegistry()
cloud := r.URL.Query().Get("cloud")
if cloud == "" {
http.Error(w, "'cloud' parameter is missing", http.StatusBadRequest)
return
}
enabledServices := []string{}
for service, disabled := range services {
if !*disabled {
enabledServices = append(enabledServices, service)
}
}
includeServices := r.URL.Query().Get("include_services")
if includeServices != "" {
enabledServices = strings.Split(includeServices, ",")
}
excludeServices := strings.Split(r.URL.Query().Get("exclude_services"), ",")
enabledServices = exporters.RemoveElements(enabledServices, excludeServices)
log.Infof("Enabled services: %v", enabledServices)
for _, service := range enabledServices {
exp, err := exporters.EnableExporter(service, *prefix, cloud, *disabledMetrics, *endpointType, *collectTime, *disableSlowMetrics, *disableDeprecatedMetrics, *disableCinderAgentUUID, *domainID, nil)
if err != nil {
log.Errorf("enabling exporter for service %s failed: %s", service, err)
continue
}
registry.MustRegister(*exp)
log.Infof("Enabled exporter for service: %s", service)
}
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}
func metricHandler(services map[string]*bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Infof("Starting openstack exporter version %s for cloud: %s", version.Info(), *cloud)
log.Infoln("Build context", version.BuildContext())
if *osClientConfig != DEFAULT_OS_CLIENT_CONFIG {
log.Debugf("Setting Env var OS_CLIENT_CONFIG_FILE = %s", *osClientConfig)
os.Setenv("OS_CLIENT_CONFIG_FILE", *osClientConfig)
}
registry := prometheus.NewPedanticRegistry()
enabledExporters := 0
//prepare Team Exporter
services = prepareTeam(services, cloud, endpointType)
for service, disabled := range services {
if !*disabled {
exp, err := exporters.EnableExporter(service, *prefix, *cloud, *disabledMetrics, *endpointType, *collectTime, *disableSlowMetrics, *disableDeprecatedMetrics, *disableCinderAgentUUID, *domainID, nil)
if err != nil {
// Log error and continue with enabling other exporters
log.Errorf("enabling exporter for service %s failed: %s", service, err)
continue
}
registry.MustRegister(*exp)
log.Infof("Enabled exporter for service: %s", service)
enabledExporters++
}
}
if enabledExporters == 0 {
log.Errorln("No exporter has been enabled, exiting")
os.Exit(-1)
}
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}
func prepareTeam(services map[string]*bool, cloud *string, endpointType *string) map[string]*bool {
if !*services["computeWithTeam"] {
//Disable Original Compute exporter
computeServiceState := *services["compute"]
*services["compute"] = true
client, err := exporters.NewTeamServiceClient(*cloud, *endpointType)
if err != nil {
// if registering computeWithTeam failed, then enable original compute exporter
log.Errorf("enabling exporter for service %s failed: %s", "team-identity", err)
*services["compute"] = computeServiceState
*services["computeWithTeam"] = true
} else {
exporters.TeamServiceClient = client
go exporters.UpdateProjectIDTeamMap()
}
}
return services
}