-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter.go
325 lines (276 loc) · 10.2 KB
/
exporter.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"log"
"net/http"
"strconv"
"strings"
"sync"
"github.com/bjin01/exporters/getyaml"
"github.com/bjin01/go-xmlrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace = "suma" // For Prometheus metrics.
)
var (
jobLableNames = []string{"type"}
scoreLableNames = []string{"hostname", "total_scores", "important", "critical"}
)
type metricInfo struct {
Desc *prometheus.Desc
Type prometheus.ValueType
}
type metrics map[int]metricInfo
func newJobsMetric(metricName string, docString string, t prometheus.ValueType, constLabels prometheus.Labels) metricInfo {
return metricInfo{
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "jobs", metricName),
docString,
jobLableNames,
constLabels,
),
Type: t,
}
}
func newSystemsMetric(metricName string, docString string, t prometheus.ValueType, constLabels prometheus.Labels) metricInfo {
return metricInfo{
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "systems", metricName),
docString,
jobLableNames,
constLabels,
),
Type: t,
}
}
func newScoreMetric(metricName string, docString string, t prometheus.ValueType, constLabels prometheus.Labels) metricInfo {
return metricInfo{
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "scores", metricName),
docString,
scoreLableNames,
constLabels,
),
Type: t,
}
}
var (
sumaUp = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "up"), "Was the last scrape of SUSE Manager successful.", nil, nil)
systemsMetrics = metrics{
2: newSystemsMetric("physical_systems", "Number of physical bare metal systems in SUSE Manager.", prometheus.GaugeValue, nil),
3: newSystemsMetric("virtual_systems", "Number of virtual systems in SUSE Manager.", prometheus.GaugeValue, nil),
4: newSystemsMetric("active_systems", "Number of active online systems in SUSE Manager.", prometheus.GaugeValue, nil),
5: newSystemsMetric("offline_systems", "Number of inactive systems in SUSE Manager.", prometheus.GaugeValue, nil),
6: newSystemsMetric("outdated_systems", "Number of out of date systems in SUSE Manager.", prometheus.GaugeValue, nil),
}
jobMetrics = metrics{
2: newJobsMetric("pending_jobs", "Current number of active pending jobs in SUSE Manager.", prometheus.GaugeValue, nil),
3: newJobsMetric("completed_jobs", "Current number of completed jobs in SUSE Manager.", prometheus.GaugeValue, nil),
4: newJobsMetric("failed_jobs", "Current number of failed jobs in SUSE Manager.", prometheus.GaugeValue, nil),
5: newJobsMetric("archived_jobs", "Current number of archived jobs in SUSE Manager.", prometheus.CounterValue, nil),
}
productMetrics = metrics{
2: newJobsMetric("base_product", "Number of each base product in SUSE Manager", prometheus.GaugeValue, nil),
}
scoretMetrics = metrics{
2: newScoreMetric("system_currency", "system currency of the top10 nodes", prometheus.GaugeValue, nil),
}
)
type Exporter struct {
suma_server_url string
username string
password string
mutex sync.RWMutex
up prometheus.Gauge
totalScrapes prometheus.Counter
suma_jobMetrics map[int]metricInfo
suma_systemsMetrics map[int]metricInfo
suma_baseprodMetrics map[int]metricInfo
suma_scoretMetrics map[int]metricInfo
}
func NewExporter(suma_server_url string, username string, password string, jobmetrics map[int]metricInfo, systemsMetrics map[int]metricInfo) *Exporter {
return &Exporter{
suma_server_url: suma_server_url,
username: username,
password: password,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Was the last scrape of SUSE Manager successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrapes_total",
Help: "Current total SUMA scrapes.",
}),
suma_jobMetrics: jobmetrics,
suma_systemsMetrics: systemsMetrics,
suma_baseprodMetrics: productMetrics,
suma_scoretMetrics: scoretMetrics,
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range jobMetrics {
ch <- m.Desc
}
for _, m := range systemsMetrics {
ch <- m.Desc
}
ch <- e.totalScrapes.Desc()
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
up := e.scrape(ch)
ch <- prometheus.MustNewConstMetric(sumaUp, prometheus.GaugeValue, up)
ch <- e.totalScrapes
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) (up float64) {
e.totalScrapes.Inc()
for _, metric := range e.suma_jobMetrics {
value, labelValue := e.query_suma(metric.Desc.String())
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.Type, value, labelValue...)
}
for _, metric := range e.suma_systemsMetrics {
value, labelValue := e.query_suma(metric.Desc.String())
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.Type, value, labelValue...)
}
for _, metric := range e.suma_baseprodMetrics {
values := e.query_suma_baseproducts(metric.Desc.String())
for a, b := range values {
if strings.Contains(a, "SUSE Linux Enterprise Server for SAP Applications") {
a = strings.Replace(a, "SUSE Linux Enterprise Server for SAP Applications", "SLES4SAP", -1)
} else if strings.Contains(a, "SUSE Linux Enterprise Server") {
a = strings.Replace(a, "SUSE Linux Enterprise Server", "SLES", -1)
}
if strings.Contains(a, "Expanded Support") {
a = strings.Replace(a, "Expanded Support", "RES", -1)
}
labelValue := []string{a}
value := float64(b)
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.Type, value, labelValue...)
}
}
for _, metric := range e.suma_scoretMetrics {
result := e.makeithapopen(metric.Desc.String())
for _, b := range result {
value := float64(b.total_scores)
labelValue := []string{b.systemname, strconv.Itoa(b.total_scores), strconv.Itoa(b.important_patches), strconv.Itoa(b.critical_patches)}
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.Type, value, labelValue...)
}
}
return 1
}
func (e *Exporter) query_suma_baseproducts(metric_desc string) map[string]int {
var final_base_prod map[string]int
client := xmlrpc.NewClient(e.suma_server_url)
f, err := client.Call("auth.login", e.username, e.password)
if err != nil {
log.Fatal("Couldn't login to suse manager host.")
}
if strings.Contains(metric_desc, "base_product") {
a := e.get_suma_systemid(client, f.String(), "system.listSystems")
serverid, _ := a.([]int)
final_base_prod = e.get_suma_baseprod(client, f.String(), "system.getInstalledProducts", serverid)
return final_base_prod
}
client.Call("auth.logout", f.String())
return final_base_prod
}
func (e *Exporter) query_suma(metric_desc string) (value float64, labels []string) {
x := 0
physicals_checked := false
client := xmlrpc.NewClient(e.suma_server_url)
f, err := client.Call("auth.login", e.username, e.password)
if err != nil {
log.Fatal("Couldn't login to suse manager host.")
}
if strings.Contains(metric_desc, "failed_jobs") {
a := e.get_suma_values(client, f.String(), "schedule.listFailedActions")
int_val, _ := a.(int)
labelNames := []string{"failed_jobs"}
return float64(int_val), labelNames
}
if strings.Contains(metric_desc, "pending_jobs") {
a := e.get_suma_values(client, f.String(), "schedule.listInProgressActions")
int_val, _ := a.(int)
labelNames := []string{"pending_jobs"}
return float64(int_val), labelNames
}
if strings.Contains(metric_desc, "completed_jobs") {
a := e.get_suma_values(client, f.String(), "schedule.listCompletedActions")
int_val, _ := a.(int)
labelNames := []string{"completed_jobs"}
return float64(int_val), labelNames
}
if strings.Contains(metric_desc, "archived_jobs") {
a := e.get_suma_values(client, f.String(), "schedule.listArchivedActions")
int_val, _ := a.(int)
labelNames := []string{"archived_jobs"}
return float64(int_val), labelNames
}
if strings.Contains(metric_desc, "physical_systems") {
a := e.get_suma_values(client, f.String(), "system.listPhysicalSystems")
x = a.(int)
physicals_checked = true
int_val, _ := a.(int)
labelNames := []string{"physical_systems"}
return float64(int_val), labelNames
}
if strings.Contains(metric_desc, "virtual_systems") {
a := e.get_suma_values(client, f.String(), "system.listSystems")
// x is the number of physical systems, a is total number of systems.
int_val := 0
// Need to do be sure that physical systems number is already known, if not we call listPhysicalSystems
if physicals_checked == true {
b := a.(int) - x
int_val = b
} else {
a1 := e.get_suma_values(client, f.String(), "system.listPhysicalSystems")
x = a1.(int)
b := a.(int) - x
int_val = b
}
labelNames := []string{"virtual_systems"}
return float64(int_val), labelNames
}
if strings.Contains(metric_desc, "active_systems") {
a := e.get_suma_values(client, f.String(), "system.listActiveSystems")
int_val, _ := a.(int)
labelNames := []string{"active_systems"}
return float64(int_val), labelNames
}
if strings.Contains(metric_desc, "offline_systems") {
a := e.get_suma_values(client, f.String(), "system.listInactiveSystems")
int_val, _ := a.(int)
labelNames := []string{"offline_systems"}
return float64(int_val), labelNames
}
if strings.Contains(metric_desc, "outdated_systems") {
a := e.get_suma_values(client, f.String(), "system.listOutOfDateSystems")
int_val, _ := a.(int)
labelNames := []string{"outdated_systems"}
return float64(int_val), labelNames
}
client.Call("auth.logout", f.String())
labelNames := []string{"something went wrong"}
return 0.00, labelNames
}
func main() {
metricsPath := "/metrics"
cfgPath, err := getyaml.ParseFlags()
if err != nil {
log.Fatal(err)
}
cfg, err := getyaml.NewConfig(cfgPath)
if err != nil {
log.Fatal(err)
}
listenAddress := ":" + cfg.Server.Ports
log.Printf("Scraping %v as %v. exporter on port: %v", cfg.Server.ApiUrl, cfg.Server.Username, listenAddress)
exporter := NewExporter(cfg.Server.ApiUrl, cfg.Server.Username, cfg.Server.Password, jobMetrics, systemsMetrics)
prometheus.MustRegister(exporter)
http.Handle(metricsPath, promhttp.Handler())
log.Fatal(http.ListenAndServe(listenAddress, nil))
}