-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
129 lines (114 loc) · 3.64 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
package main
import (
"log"
"net/http"
"strconv"
"time"
"github.com/equelin/gounity"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
//Create prometheus registry
reg := prometheus.NewPedanticRegistry()
log.Print("main.go:main - Loading Config and Metrics")
exporter, unityClients := readConfig("./config.json")
//Check if pool metrics are required
poolMetrics := make([]*prometheus.GaugeVec, 0)
if exporter.Pools {
labels := []string{"unity", "id", "name"}
poolFields := []string{"sizefree", "sizeTotal", "sizeUsed", "sizeSubscribed"}
for _, field := range poolFields {
poolMetric := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
//Namespace: "our_company",
//Subsystem: "blob_storage",
Name: "pool_" + field + "_" + "bytes",
Help: "Bytes of pool " + field,
},
labels,
)
poolMetrics = append(poolMetrics, poolMetric)
prometheus.WrapRegistererWith(prometheus.Labels{}, reg).MustRegister(poolMetric)
}
}
storageMetrics := make([]*prometheus.GaugeVec, 0)
if exporter.StorageResources {
labels := []string{"unity", "storageresource", "storageresourcename"}
storageResourceFields := []string{"sizeAllocated", "sizeTotal", "sizeUsed"}
for _, field := range storageResourceFields {
storageMetric := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
//Namespace: "our_company",
//Subsystem: "blob_storage",
Name: "storageresource_" + field + "_bytes",
Help: "Bytes of Storageresource " + field,
},
labels,
)
storageMetrics = append(storageMetrics, storageMetric)
prometheus.WrapRegistererWith(prometheus.Labels{}, reg).MustRegister(storageMetric)
}
}
//Prepare additional metrics
metrics := readMetrics("./unity_metrics.json")
//Get selected Metrics from metrics json
selectedMetrics := make([]Metric, 0)
for _, metric := range metrics {
for _, path := range exporter.Metrics {
if metric.PromPath == path {
//Add Prometheus.Desc to metric
metric.addPrometheusDesc()
metric.addPrometheusGaugeVec(reg)
prometheus.WrapRegistererWith(prometheus.Labels{}, reg).MustRegister(metric.PromGauge)
selectedMetrics = append(selectedMetrics, metric)
}
}
}
unityCollectors := make([]UnityCollector, 0)
//Create Unity Session
for _, u := range unityClients {
log.Print("main.go:main - Create unity Session: " + u.IP)
session, err := gounity.NewSession(u.IP, true, u.User, u.Password)
if err != nil {
log.Fatal(err)
}
u.Session = *session
//defer session.CloseSession()
// Get system informations
System, err := session.GetbasicSystemInfo()
if err != nil {
log.Fatal(err)
} else {
// Store the name of the Unity
u.Name = System.Entries[0].Content.Name
}
session.CloseSession()
log.Print("main.go:main - Unity Name: " + u.Name)
unityCollectors = append(unityCollectors, NewUnityCollector(u, selectedMetrics, exporter, poolMetrics, storageMetrics))
}
log.Print(len(unityCollectors))
go func() {
for {
for _, uc := range unityCollectors {
session, err := gounity.NewSession(uc.Unity.IP, true, uc.Unity.User, uc.Unity.Password)
if err != nil {
log.Fatal(err)
}
uc.Unity.Session = *session
//log.Print(uc.Unity.Name)
if exporter.Pools {
uc.CollectPoolMetrics()
}
if exporter.StorageResources {
uc.CollectStorageResourceMetrics()
}
uc.CollectMetrics()
uc.Unity.Session.CloseSession()
}
time.Sleep(time.Duration(exporter.Interval) * time.Second)
}
}()
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
log.Fatal(http.ListenAndServe(strconv.Itoa(exporter.Port), nil))
}