@@ -7,19 +7,11 @@ import (
7
7
"path"
8
8
"strconv"
9
9
"strings"
10
- "time"
11
10
12
11
"github.com/go-kit/kit/log"
13
12
"github.com/go-kit/kit/log/level"
14
-
15
- "github.com/OpenNebula/one/src/oca/go/src/goca"
16
-
17
- "github.com/prometheus/client_golang/prometheus"
18
- "github.com/prometheus/client_golang/prometheus/promauto"
19
13
"github.com/prometheus/client_golang/prometheus/promhttp"
20
-
21
14
"github.com/spf13/viper"
22
-
23
15
"gopkg.in/alecthomas/kingpin.v2"
24
16
)
25
17
@@ -33,128 +25,6 @@ type config struct {
33
25
path string
34
26
}
35
27
36
- var (
37
- clusterMetrics = make (map [string ]* prometheus.GaugeVec )
38
- hostMetrics = make (map [string ]* prometheus.GaugeVec )
39
- )
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
-
99
- // recordMetrics from OpenNebula
100
- func recordMetrics (config config , logger log.Logger ) {
101
-
102
- level .Info (logger ).Log ("msg" , "recording metrics from opennebula frontend" , "interval" , config .interval )
103
-
104
- client := goca .NewDefaultClient (goca .NewConfig (config .user , config .password , config .endpoint ))
105
- controller := goca .NewController (client )
106
-
107
- for {
108
-
109
- pool , err := controller .Hosts ().Info ()
110
- if err != nil {
111
- level .Error (logger ).Log ("msg" , "error retrieving hosts info" , "error" , err )
112
- return
113
- }
114
-
115
- type metrics struct {
116
- cluster , metric string
117
- }
118
- sum := make (map [metrics ]int )
119
-
120
- for _ , host := range pool .Hosts {
121
-
122
- level .Debug (logger ).Log ("msg" , "host metrics" ,
123
- "host" , host .Name ,
124
- "TotalMem" , host .Share .TotalMem ,
125
- "UsedMem" , host .Share .UsedMem ,
126
- "TotalCPU" , host .Share .TotalCPU ,
127
- "UsedCPU" , host .Share .UsedCPU ,
128
- "RunningVMs" , host .Share .RunningVMs )
129
-
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
143
-
144
- if host .StateRaw == 2 {
145
- sum [metrics {host .Cluster , "ActiveHosts" }] = sum [metrics {host .Cluster , "ActiveHosts" }] + 1
146
- }
147
- }
148
-
149
- for key , value := range sum {
150
- // record cluster metrics
151
- clusterMetrics [key .metric ].With (prometheus.Labels {"cluster" : key .cluster }).Set (float64 (value ))
152
- }
153
-
154
- time .Sleep (time .Duration (config .interval ) * time .Second )
155
- }
156
- }
157
-
158
28
func newConfig (fileName string , logger log.Logger ) (config , error ) {
159
29
160
30
viper .SetDefault ("endpoint" , "" ) // "" will be set to "http://localhost:2633/RPC2" by goca
@@ -207,8 +77,10 @@ func main() {
207
77
logger := log .NewLogfmtLogger (log .NewSyncWriter (os .Stderr ))
208
78
logger = log .With (logger , "ts" , log .DefaultTimestampUTC , "caller" , log .DefaultCaller )
209
79
210
- cfgFile := kingpin .Flag ("config" , "config file for one_exporter" ).Short ('c' ).String ()
80
+ cfgFile := kingpin .Flag ("config" , "config file for one_exporter" ).Short ('c' ).String ()
211
81
logLevel := kingpin .Flag ("loglevel" , "the log level to output. options are error, info or debug. defaults to info" ).Short ('l' ).Default ("info" ).String ()
82
+
83
+ kingpin .Version (Version )
212
84
kingpin .Parse ()
213
85
214
86
logger = level .NewFilter (logger , allowedLevel (* logLevel ))
@@ -222,8 +94,7 @@ func main() {
222
94
223
95
level .Debug (logger ).Log ("msg" , "loaded config" , "user" , config .user , "endpoint" , config .endpoint )
224
96
225
- initCollectors ()
226
-
97
+ initMetrics ()
227
98
go recordMetrics (config , logger )
228
99
229
100
level .Info (logger ).Log ("msg" , "starting exporter" , "host" , config .host , "port" , config .port , "path" , config .path )
@@ -233,5 +104,4 @@ func main() {
233
104
if err != nil {
234
105
level .Error (logger ).Log ("error" , err )
235
106
}
236
-
237
107
}
0 commit comments