This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
collector.go
90 lines (72 loc) · 1.91 KB
/
collector.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
package main
import (
"log"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
// Exporter implements the prometheus.Collector interface. It exposes the metrics
// of a ipmi node.
type Exporter struct {
config Config
metrics []*prometheus.GaugeVec
totalScrapes prometheus.Counter
replacer *strings.Replacer
}
// Config -
type Config struct {
Xenhost string
Credentials struct {
Username string
Password string
}
}
// NewExporter instantiates a new ipmi Exporter.
func NewExporter(config Config) *Exporter {
var e = &Exporter{
config: config,
}
e.metrics = []*prometheus.GaugeVec{}
e.collect()
return e
}
// Describe Describes all the registered stats metrics from the xen master.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.metrics {
m.Describe(ch)
}
}
// Collect collects all the registered stats metrics from the xen master.
func (e *Exporter) Collect(metrics chan<- prometheus.Metric) {
e.collect()
for _, m := range e.metrics {
m.Collect(metrics)
}
}
func (e *Exporter) collect() {
var err error
stats := NewXenstats(e.config)
stats.GetApiCaller()
e.metrics, err = stats.createHostMemMetrics()
if err != nil {
log.Printf("Xen api error in creating host memory metrics: %v", err)
}
poolmetrics, err := stats.createPoolMetrics()
if err != nil {
log.Printf("Xen api error in creating ha pool metrics: %v", err)
}
e.metrics = append(e.metrics, poolmetrics...)
storagemetrics, err := stats.createStorageMetrics()
if err != nil {
log.Printf("Xen api error in creating storage metrics: %v", err)
}
e.metrics = append(e.metrics, storagemetrics...)
cpumetrics, err := stats.createHostCPUMetrics()
if err != nil {
log.Printf("Xen api error in creating host cpu metrics: %v", err)
}
e.metrics = append(e.metrics, cpumetrics...)
err = stats.CloseApi()
if err != nil {
log.Printf("Error during connection close: %v", err)
}
}