This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_zabbix_sender.go
166 lines (133 loc) · 4.03 KB
/
storage_zabbix_sender.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
package main
import (
"fmt"
"log"
"net"
"net/url"
"strconv"
"strings"
"time"
"github.com/adubkov/go-zabbix"
)
const (
keyOfItem = `asterisk.statsd.discovery`
prefixKeyOfMetric = `asterisk.statsd.metrics`
minMetricNameCountSep = 2
separatorOfMetric = `.`
)
// StorageZabbixSender is the data storage layer using to send data to Zabbix Server
type StorageZabbixSender struct {
Sender *zabbix.Sender
Packet *zabbix.Packet
Metrics []*zabbix.Metric
}
// parseTimestampToInt64 parse time with timestamp format to int64
func parseTimestampToInt64(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
// parseMetricToItemValue parse metric name to item value for create this item in item prototype
func parseMetricToItemValue(metricName string) (valueOfMetric string) {
return fmt.Sprintf("{\"data\": [{\"{#ITEM}\": \"%s\"}]}", strings.ToLower(metricName))
}
// parseMetricToKey parse metric name to key of item prototype
func parseMetricToKey(metricName string) (key string) {
var suffix string
name := strings.ToLower(metricName)
splitted := strings.Split(name, separatorOfMetric)
// if th metric name dont have minimun count separators, use the first
if len(splitted) <= minMetricNameCountSep {
suffix = separatorOfMetric + splitted[0]
} else {
for i, k := range splitted {
if i >= minMetricNameCountSep {
break
}
suffix = suffix + separatorOfMetric + k
}
}
key = fmt.Sprintf("%s[%s]", prefixKeyOfMetric+suffix, name)
return
}
// parseMetricValue parse the metric value considering type of value
func parseMetricValue(metric Metric) (value string) {
switch metric.Stats.Type {
case "ms":
seconds := metric.Stats.Value / 1000
value = fmt.Sprintf("%f", seconds)
default:
value = fmt.Sprintf("%f", metric.Stats.Value)
}
return
}
// itemTypeIsSupported check if the item type is supported
func (s *StorageZabbixSender) itemTypeIsSupported(itemType string) (supported bool, err error) {
switch itemType {
case "ms":
supported = true
default:
err = fmt.Errorf("this item type is not supported: %s", itemType)
}
return
}
// parseURL parse the url configure to zabbix sender
func parseURL(zabbixURL string) (host, port string, err error) {
u, err := url.Parse(zabbixURL)
if err != nil {
return
}
if u.Scheme != "zabbix" {
err = fmt.Errorf("URL scheme is not Zabbix, is: %s", u.Scheme)
return
}
host, port, err = net.SplitHostPort(u.Host)
return
}
// NewStorageZabbixSender prepare a Zabbix Sender
func NewStorageZabbixSender(storageURL string) (*StorageZabbixSender, error) {
var err error
stg := new(StorageZabbixSender)
host, p, err := parseURL(storageURL)
if err != nil {
return stg, err
}
port, err := strconv.Atoi(p)
if err != nil {
return stg, err
}
stg.Sender = zabbix.NewSender(host, port)
return stg, err
}
// sendMetrics send the metrics in zabbix storage
func sendMetrics(s *StorageZabbixSender) {
for _, m := range s.Metrics {
log.Printf("sendMetrics: m=%+v", m)
}
res, err := s.Sender.Send(s.Packet)
log.Printf("sendMetrics res=%+v, err=%+v", string(res), err)
return
}
// SaveMetric save the metric on a item in zabbix server
func (s *StorageZabbixSender) SaveMetric(metric Metric) error {
if supported, err := s.itemTypeIsSupported(metric.Stats.Type); !supported || err != nil {
return err
}
key := parseMetricToKey(metric.Stats.Name)
value := parseMetricValue(metric)
s.Metrics = append(s.Metrics, zabbix.NewMetric(metric.Hostname, key, value))
s.Packet = zabbix.NewPacket(s.Metrics)
sendMetrics(s)
return nil
}
// SaveItem save/create the item on zabbix server
func (s *StorageZabbixSender) SaveItem(metric Metric) error {
if supported, err := s.itemTypeIsSupported(metric.Stats.Type); !supported || err != nil {
return err
}
value := parseMetricToItemValue(metric.Stats.Name)
s.Metrics = append(s.Metrics, zabbix.NewMetric(metric.Hostname, keyOfItem, value))
return nil
}
// ItemExists check if a item exists, but it's not implemented to this storage
func (s *StorageZabbixSender) ItemExists(metric Metric) (found bool) {
return
}