@@ -34,32 +34,68 @@ type config struct {
34
34
}
35
35
36
36
var (
37
- poolTotalMemGauge = promauto .NewGauge (prometheus.GaugeOpts {
38
- Name : "one_pool_totalmem" ,
39
- Help : "total memory of all hosts in opennebula" ,
40
- })
41
- poolUsedMemGauge = promauto .NewGauge (prometheus.GaugeOpts {
42
- Name : "one_pool_usedmem" ,
43
- Help : "used memory in all hosts in opennebula" ,
44
- })
45
- poolTotalCPUGauge = promauto .NewGauge (prometheus.GaugeOpts {
46
- Name : "one_pool_totalcpu" ,
47
- Help : "total cpu of all hosts in opennebula" ,
48
- })
49
- poolUsedCPUGauge = promauto .NewGauge (prometheus.GaugeOpts {
50
- Name : "one_pool_usedcpu" ,
51
- Help : "used cpu in all hosts in opennebula" ,
52
- })
53
- poolActiveHostsGauge = promauto .NewGauge (prometheus.GaugeOpts {
54
- Name : "one_pool_activehosts" ,
55
- Help : "number of active hosts in opennebula" ,
56
- })
57
- poolRunningVMsGauge = promauto .NewGauge (prometheus.GaugeOpts {
58
- Name : "one_pool_runningvms" ,
59
- Help : "number of running virtual machines in opennebula" ,
60
- })
37
+ clusterMetrics = make (map [string ]* prometheus.GaugeVec )
38
+ hostMetrics = make (map [string ]* prometheus.GaugeVec )
61
39
)
62
40
41
+ func initCollectors () {
42
+ clusterMetrics ["TotalMem" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
43
+ Name : "one_cluster_totalmem" ,
44
+ Help : "total memory available in cluster" ,
45
+ },[]string {"cluster" })
46
+
47
+ clusterMetrics ["UsedMem" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
48
+ Name : "one_cluster_usedmem" ,
49
+ Help : "real used memory in cluster" ,
50
+ },[]string {"cluster" })
51
+
52
+ clusterMetrics ["TotalCPU" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
53
+ Name : "one_cluster_totalcpu" ,
54
+ Help : "total cpu available in cluster" ,
55
+ },[]string {"cluster" })
56
+
57
+ clusterMetrics ["UsedCPU" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
58
+ Name : "one_cluster_usedcpu" ,
59
+ Help : "real used cpu in cluster" ,
60
+ },[]string {"cluster" })
61
+
62
+ clusterMetrics ["RunningVMs" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
63
+ Name : "one_cluster_runningvms" ,
64
+ Help : "running virtual machines in cluster" ,
65
+ },[]string {"cluster" })
66
+
67
+ clusterMetrics ["ActiveHosts" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
68
+ Name : "one_cluster_activehosts" ,
69
+ Help : "succesfully monitored hosts in cluster" ,
70
+ },[]string {"cluster" })
71
+
72
+ hostMetrics ["TotalMem" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
73
+ Name : "one_host_totalmem" ,
74
+ Help : "total memory available on host" ,
75
+ },[]string {"cluster" , "host" })
76
+
77
+ hostMetrics ["UsedMem" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
78
+ Name : "one_host_usedmem" ,
79
+ Help : "real used memory on host" ,
80
+ },[]string {"cluster" , "host" })
81
+
82
+ hostMetrics ["TotalCPU" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
83
+ Name : "one_host_totalcpu" ,
84
+ Help : "total cpu available on host" ,
85
+ },[]string {"cluster" , "host" })
86
+
87
+ hostMetrics ["UsedCPU" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
88
+ Name : "one_host_usedcpu" ,
89
+ Help : "real used cpu on host" ,
90
+ },[]string {"cluster" , "host" })
91
+
92
+ hostMetrics ["RunningVMs" ] = promauto .NewGaugeVec (prometheus.GaugeOpts {
93
+ Name : "one_host_runningvms" ,
94
+ Help : "running virtual machines on host" ,
95
+ },[]string {"cluster" , "host" })
96
+
97
+ }
98
+
63
99
// recordMetrics from OpenNebula
64
100
func recordMetrics (config config , logger log.Logger ) {
65
101
@@ -76,13 +112,10 @@ func recordMetrics(config config, logger log.Logger) {
76
112
panic (err )
77
113
}
78
114
79
- var totalMem int = 0
80
- var usedMem int = 0
81
- var totalCPU int = 0
82
- var usedCPU int = 0
83
- var runningVMs int = 0
84
-
85
- var activeHosts int = 0
115
+ type metrics struct {
116
+ cluster , metric string
117
+ }
118
+ sum := make (map [metrics ]int )
86
119
87
120
for _ , host := range pool .Hosts {
88
121
@@ -94,24 +127,29 @@ func recordMetrics(config config, logger log.Logger) {
94
127
"UsedCPU" , host .Share .UsedCPU ,
95
128
"RunningVMs" , host .Share .RunningVMs )
96
129
97
- totalMem = totalMem + host .Share .TotalMem
98
- usedMem = usedMem + host .Share .UsedMem
99
- totalCPU = totalCPU + host .Share .TotalCPU
100
- usedCPU = usedCPU + host .Share .UsedCPU
101
- runningVMs = runningVMs + host .Share .RunningVMs
130
+ // record host metrics
131
+ hostMetrics ["TotalMem" ].With (prometheus.Labels {"cluster" : host .Cluster , "host" : host .Name }).Set (float64 (host .Share .TotalMem ))
132
+ hostMetrics ["UsedMem" ].With (prometheus.Labels {"cluster" : host .Cluster , "host" : host .Name }).Set (float64 (host .Share .UsedMem ))
133
+ hostMetrics ["TotalCPU" ].With (prometheus.Labels {"cluster" : host .Cluster , "host" : host .Name }).Set (float64 (host .Share .TotalCPU ))
134
+ hostMetrics ["UsedMem" ].With (prometheus.Labels {"cluster" : host .Cluster , "host" : host .Name }).Set (float64 (host .Share .UsedMem ))
135
+ hostMetrics ["RunningVMs" ].With (prometheus.Labels {"cluster" : host .Cluster , "host" : host .Name }).Set (float64 (host .Share .RunningVMs ))
136
+
137
+ // sum cluster metrics
138
+ sum [metrics {host .Cluster , "TotalMem" }] = sum [metrics {host .Cluster , "TotalMem" }] + host .Share .TotalMem
139
+ sum [metrics {host .Cluster , "UsedMem" }] = sum [metrics {host .Cluster , "UsedMem" }] + host .Share .UsedMem
140
+ sum [metrics {host .Cluster , "TotalCPU" }] = sum [metrics {host .Cluster , "TotalCPU" }] + host .Share .TotalCPU
141
+ sum [metrics {host .Cluster , "UsedCPU" }] = sum [metrics {host .Cluster , "UsedCPU" }] + host .Share .UsedCPU
142
+ sum [metrics {host .Cluster , "RunningVMs" }] = sum [metrics {host .Cluster , "RunningVMs" }] + host .Share .RunningVMs
102
143
103
144
if host .StateRaw == 2 {
104
- activeHosts = activeHosts + 1
145
+ sum [ metrics { host . Cluster , "ActiveHosts" }] = sum [ metrics { host . Cluster , "ActiveHosts" }] + 1
105
146
}
106
147
}
107
148
108
- poolTotalMemGauge .Set (float64 (totalMem ))
109
- poolUsedMemGauge .Set (float64 (usedMem ))
110
- poolTotalCPUGauge .Set (float64 (totalCPU ))
111
- poolUsedCPUGauge .Set (float64 (usedCPU ))
112
- poolRunningVMsGauge .Set (float64 (runningVMs ))
113
-
114
- poolActiveHostsGauge .Set (float64 (activeHosts ))
149
+ for key , value := range sum {
150
+ // record cluster metrics
151
+ clusterMetrics [key .metric ].With (prometheus.Labels {"cluster" : key .cluster }).Set (float64 (value ))
152
+ }
115
153
116
154
time .Sleep (time .Duration (config .interval ) * time .Second )
117
155
}
@@ -181,6 +219,8 @@ func main() {
181
219
config := newConfig (* cfgFile , logger )
182
220
level .Debug (logger ).Log ("msg" , "loaded config" , "user" , config .user , "endpoint" , config .endpoint )
183
221
222
+ initCollectors ()
223
+
184
224
go recordMetrics (config , logger )
185
225
186
226
level .Info (logger ).Log ("msg" , "starting exporter" , "host" , config .host , "port" , config .port , "path" , config .path )
0 commit comments