forked from openstack-exporter/openstack-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neutron.go
102 lines (84 loc) · 2.54 KB
/
neutron.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"gopkg.in/niedbalski/goose.v3/client"
"gopkg.in/niedbalski/goose.v3/neutron"
)
type NeutronExporter struct {
BaseOpenStackExporter
Client *neutron.Client
}
var defaultNeutronMetrics = []Metric{
{Name: "floating_ips"},
{Name: "networks"},
{Name: "security_groups"},
{Name: "subnets"},
{Name: "agent_state", Labels: []string{"hostname", "service", "adminState"}},
}
func NewNeutronExporter(client client.AuthenticatingClient, prefix string, config *Cloud) (*NeutronExporter, error) {
exporter := NeutronExporter{
BaseOpenStackExporter{
Name: "neutron",
Prefix: prefix,
Config: config,
},
neutron.New(client),
}
for _, metric := range defaultNeutronMetrics {
exporter.AddMetric(metric.Name, metric.Labels, nil)
}
return &exporter, nil
}
func (exporter *NeutronExporter) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range exporter.Metrics {
ch <- metric
}
}
func (exporter *NeutronExporter) Collect(ch chan<- prometheus.Metric) {
log.Infoln("Fetching floating ips list")
floatingips, err := exporter.Client.ListFloatingIPsV2()
if err != nil {
log.Errorf("%s", err)
}
log.Infoln("Fetching agents list")
agents, err := exporter.Client.ListAgentsV2()
if err != nil {
log.Errorf(err.Error())
}
for _, agent := range agents {
var state int = 0
if agent.Alive {
state = 1
}
adminState := "down"
if agent.AdminStateUp {
adminState = "up"
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["agent_state"],
prometheus.CounterValue, float64(state), agent.Host, agent.Binary, adminState)
}
log.Infoln("Fetching list of networks")
networks, err := exporter.Client.ListNetworksV2()
if err != nil {
log.Errorf("%s", err)
}
log.Infoln("Fetching list of security groups")
securityGroups, err := exporter.Client.ListSecurityGroupsV2()
if err != nil {
log.Errorf("%s", err)
}
log.Infoln("Fetching list of subnets")
subnets, err := exporter.Client.ListSubnetsV2()
if err != nil {
log.Errorf("%s", err)
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["subnets"],
prometheus.GaugeValue, float64(len(subnets)))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["floating_ips"],
prometheus.GaugeValue, float64(len(floatingips)))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["networks"],
prometheus.GaugeValue, float64(len(networks)))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["security_groups"],
prometheus.GaugeValue, float64(len(securityGroups)))
}