-
Notifications
You must be signed in to change notification settings - Fork 1
/
zabbix.go
128 lines (115 loc) · 2.71 KB
/
zabbix.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
package metrics
import (
"runtime"
"sync"
"time"
"github.com/blacked/go-zabbix"
)
type zabbixTransport struct {
logger ErrorLogger
sender *zabbix.Sender
done chan bool
metricsHost string
collectors []Collector
interval uint64
run sync.Once
runnerMutex sync.Mutex
}
// NewZabbix creates new metrics transport for zabbix server.
func NewZabbix(
collectors []Collector, sender *zabbix.Sender, metricsHost string, interval uint64, logger ErrorLogger) Transport {
t := &zabbixTransport{
sender: sender,
metricsHost: metricsHost,
collectors: collectors,
interval: interval,
logger: logger,
}
runtime.SetFinalizer(t, StoppableFinalizer)
return t
}
// WithCollector adds collector to the list of collectors that will be used for metrics collection.
func (t *zabbixTransport) WithCollector(col Collector) Transport {
if t.collectors == nil {
t.collectors = []Collector{col}
return t
}
t.collectors = append(t.collectors, col)
return t
}
// Run zabbix transport.
func (t *zabbixTransport) Run() {
t.run.Do(func() {
t.done = make(chan bool, 1)
go func() {
t.runnerMutex.Lock()
for _, col := range t.collectors {
if runnable, ok := col.(Runnable); ok {
runnable.Run()
}
}
t.runnerMutex.Unlock()
for {
select {
case <-t.done:
t.runnerMutex.Lock()
defer t.runnerMutex.Unlock()
close(t.done)
t.done = nil
t.run = sync.Once{}
return
case <-time.After(time.Duration(t.interval) * time.Second):
if err := t.Send(); err != nil {
t.logger.Errorf("cannot send metrics to Zabbix: %v", err)
}
}
}
}()
})
}
// Send metrics to zabbix.
func (t *zabbixTransport) Send() error {
if t.sender == nil {
return ErrNilSender
}
var total int
metrics := make([][]Metric, len(t.collectors))
for i, col := range t.collectors {
rec := col.Metrics()
total += len(rec)
metrics[i] = rec
}
i := 0
flattened := make([]*zabbix.Metric, total)
namesMap := make(map[string]struct{}, total)
for _, metricList := range metrics {
for _, item := range metricList {
if _, ok := namesMap[item.Name]; ok {
continue
}
flattened[i] = zabbix.NewMetric(t.metricsHost, item.Name, item.Value)
namesMap[item.Name] = struct{}{}
i++
}
}
_, err := t.sender.Send(zabbix.NewPacket(flattened))
return err
}
// Stop zabbix transport.
func (t *zabbixTransport) Stop() error {
t.runnerMutex.Lock()
defer t.runnerMutex.Unlock()
if t.done == nil {
return ErrTransportInactive
}
t.done <- true
for _, col := range t.collectors {
if stoppable, ok := col.(Stoppable); ok {
err := stoppable.Stop()
if err != nil {
t.logger.Errorf("cannot stop collector: %s", err)
}
}
}
return nil
}