forked from openstack-exporter/openstack-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (69 loc) · 2.84 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
package main
import (
"fmt"
"github.com/openstack-exporter/openstack-exporter/exporters"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
"os"
)
var defaultEnabledServices = []string{"network", "compute", "image", "volume", "identity"}
var DEFAULT_OS_CLIENT_CONFIG = "/etc/openstack/clouds.yaml"
func main() {
var (
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()
disabledMetrics = kingpin.Flag("disable-metric", "multiple --disable-metric can be specified in the format: service-metric (i.e: cinder-snapshots)").Default("").Short('d').Strings()
cloud = kingpin.Arg("cloud", "name or id of the cloud to gather metrics from").Required().String()
)
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.HelpFlag.Short('h')
kingpin.Parse()
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)
}
enabledExporters := 0
for service, disabled := range services {
if !*disabled {
_, err := exporters.EnableExporter(service, *prefix, *cloud, *disabledMetrics)
if err != nil {
// Log error and continue with enabling other exporters
log.Errorf("enabling exporter for service %s failed: %s", service, err)
continue
}
log.Infof("Enabled exporter for service: %s", service)
enabledExporters++
}
}
if enabledExporters == 0 {
log.Errorln("No exporter has been enabled, exiting")
os.Exit(-1)
}
http.Handle(*metrics, promhttp.Handler())
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)
}
})
log.Infoln("Starting HTTP server on", *bind)
log.Fatal(http.ListenAndServe(*bind, nil))
}