/
instances.go
93 lines (76 loc) · 2.05 KB
/
instances.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
package ipmi
import (
"log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs/ipmi/exporter"
util "flashcat.cloud/categraf/pkg/metrics"
"flashcat.cloud/categraf/types"
)
// Instance stores the configuration values for the ipmi_sensor input plugin
type Instance struct {
config.InstanceConfig
Target string `toml:"target"`
Path string `toml:"path"`
exporter.IPMIConfig
}
func (m *Instance) Init() error {
if len(m.Target) == 0 {
return types.ErrInstancesEmpty
}
// Set defaults
if m.Timeout == 0 {
m.Timeout = 20
}
return nil
}
// Gather is the main execution function for the plugin
func (m *Instance) Gather(slist *types.SampleList) {
constLabels := m.GetLabels()
metricChan := make(chan prometheus.Metric, 500)
go func() {
exporter.Collect(metricChan, m.Target, m.Path, m.IPMIConfig)
close(metricChan)
}()
for metric := range metricChan {
if metric == nil {
continue
}
desc := metric.Desc()
if desc.Err() != nil {
log.Println("E! got invalid metric:", desc.Name(), desc.Err())
continue
}
dtoMetric := &dto.Metric{}
err := metric.Write(dtoMetric)
if err != nil {
log.Println("E! failed to write metric:", desc.String())
continue
}
labels := map[string]string{}
for _, kv := range desc.ConstLabels() {
labels[*kv.Name] = *kv.Value
}
for _, kv := range dtoMetric.Label {
labels[*kv.Name] = *kv.Value
}
for k, v := range constLabels {
labels[k] = v
}
switch {
case dtoMetric.Counter != nil:
slist.PushSample("", desc.Name(), *dtoMetric.Counter.Value, labels)
case dtoMetric.Gauge != nil:
slist.PushSample("", desc.Name(), *dtoMetric.Gauge.Value, labels)
case dtoMetric.Summary != nil:
util.HandleSummary("", dtoMetric, nil, desc.Name(), nil, slist)
case dtoMetric.Histogram != nil:
util.HandleHistogram("", dtoMetric, nil, desc.Name(), nil, slist)
default:
slist.PushSample("", desc.Name(), *dtoMetric.Untyped.Value, labels)
}
}
}
func (m *Instance) Drop() {
}