-
Notifications
You must be signed in to change notification settings - Fork 4
/
cinder.go
87 lines (72 loc) · 2.25 KB
/
cinder.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"gopkg.in/niedbalski/goose.v3/cinder"
"gopkg.in/niedbalski/goose.v3/client"
"net/http"
"net/url"
)
type CinderExporter struct {
BaseOpenStackExporter
Client *cinder.Client
}
var defaultCinderMetrics = []Metric{
{Name: "volumes"},
{Name: "snapshots"},
{Name: "agent_state", Labels: []string{"hostname", "service", "adminState", "zone"}},
}
func NewCinderExporter(client client.AuthenticatingClient, prefix string, config *Cloud) (*CinderExporter, error) {
endpoint := client.EndpointsForRegion(config.Region)["volumev3"]
endpointUrl, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
exporter := CinderExporter{BaseOpenStackExporter{
Name: "cinder",
Prefix: prefix,
Config: config,
}, cinder.NewClient(client.TenantId(), endpointUrl,
cinder.SetAuthHeaderFn(client.Token,
http.DefaultClient.Do),
)}
for _, metric := range defaultCinderMetrics {
exporter.AddMetric(metric.Name, metric.Labels, nil)
}
return &exporter, nil
}
func (exporter *CinderExporter) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range exporter.Metrics {
ch <- metric
}
}
func (exporter *CinderExporter) Collect(ch chan<- prometheus.Metric) {
log.Infoln("Fetching volumes info")
volumes, err := exporter.Client.GetVolumesSimple()
if err != nil {
log.Errorf("%s", err)
}
log.Infoln("Fetching services state information")
services, err := exporter.Client.GetServices()
if err != nil {
log.Errorf("%s", err)
}
for _, service := range services.Services {
var state int = 0
if service.State == "up" {
state = 1
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["agent_state"],
prometheus.CounterValue, float64(state), service.Host, service.Binary, service.Status, service.Zone)
}
log.Infoln("Fetching volumes information")
ch <- prometheus.MustNewConstMetric(exporter.Metrics["volumes"],
prometheus.GaugeValue, float64(len(volumes.Volumes)))
log.Infoln("Fetching snapshots information")
snapshots, err := exporter.Client.GetSnapshotsSimple()
if err != nil {
log.Errorf("%s", err)
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["snapshots"],
prometheus.GaugeValue, float64(len(snapshots.Snapshots)))
}