forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
190 lines (163 loc) · 4.5 KB
/
api.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
package collectd
import (
"math"
"net"
"sync"
log "code.google.com/p/log4go"
"github.com/influxdb/influxdb/api"
"github.com/influxdb/influxdb/cluster"
"github.com/influxdb/influxdb/configuration"
"github.com/influxdb/influxdb/protocol"
collectd "github.com/kimor79/gollectd"
)
type Server struct {
listenAddress string
database string
coordinator api.Coordinator
clusterConfig *cluster.ClusterConfiguration
conn *net.UDPConn
shutdown chan bool
user *cluster.ClusterAdmin
typesdb collectd.Types
typesdbpath string
}
func NewServer(config *configuration.Configuration, coord api.Coordinator, clusterConfig *cluster.ClusterConfiguration) *Server {
s := &Server{}
s.listenAddress = config.CollectdBindString()
s.database = config.CollectdDatabase
s.coordinator = coord
s.shutdown = make(chan bool, 1)
s.clusterConfig = clusterConfig
s.typesdbpath = config.CollectdTypesDB
s.typesdb = make(collectd.Types)
return s
}
func (s *Server) getAuth() {
// just use any (the first) of the list of admins.
names := s.clusterConfig.GetClusterAdmins()
s.user = s.clusterConfig.GetClusterAdmin(names[0])
}
func (s *Server) ListenAndServe() {
var err error
s.getAuth()
s.typesdb, err = collectd.TypesDBFile(s.typesdbpath)
if err != nil {
log.Error("CollectdServer: TypesDB: ", err)
return
}
addr, err := net.ResolveUDPAddr("udp4", s.listenAddress)
if err != nil {
log.Error("CollectdServer: ResolveUDPAddr: ", err)
return
}
if s.listenAddress != "" {
s.conn, err = net.ListenUDP("udp", addr)
if err != nil {
log.Error("CollectdServer: Listen: ", err)
return
}
}
defer s.conn.Close()
s.HandleSocket(s.conn)
}
func (s *Server) HandleSocket(socket *net.UDPConn) {
// From https://collectd.org/wiki/index.php/Binary_protocol
// 1024 bytes (payload only, not including UDP / IP headers)
// In versions 4.0 through 4.7, the receive buffer has a fixed size
// of 1024 bytes. When longer packets are received, the trailing data
// is simply ignored. Since version 4.8, the buffer size can be
// configured. Version 5.0 will increase the default buffer size to
// 1452 bytes (the maximum payload size when using UDP/IPv6 over
// Ethernet).
buffer := make([]byte, 1452)
for {
n, _, err := socket.ReadFromUDP(buffer)
if err != nil || n == 0 {
log.Error("Collectd ReadFromUDP error: %s", err)
continue
}
packets, err := collectd.Packets(buffer[:n], s.typesdb)
if err != nil {
log.Error("Collectd parse error: %s", err)
continue
}
for _, packet := range *packets {
series := packetToSeries(&packet)
err = s.coordinator.WriteSeriesData(s.user, s.database, series)
if err != nil {
log.Error("Collectd cannot write data: %s", err)
continue
}
}
}
}
func packetToSeries(p *collectd.Packet) []*protocol.Series {
// Prefer high resolution timestamp. TimeHR is 2^-30 seconds, so shift
// right 30 to get seconds then convert to microseconds for InfluxDB
uts := (p.TimeHR >> 30) * 1000 * 1000
// Fallback on unix timestamp if high res is 0
if uts == 0 {
uts = p.Time * 1000 * 1000
}
// Collectd time is uint64 but influxdb expects int64
ts := int64(uts % math.MaxInt64)
if uts > math.MaxInt64 {
logTimerWrapOnce()
}
series := make([]*protocol.Series, len(p.Values))
for i, dataSet := range p.Values {
metricName := p.FormatName()
values := []*protocol.FieldValue{}
values = append(values, &protocol.FieldValue{
StringValue: &p.Hostname,
})
values = append(values, &protocol.FieldValue{
StringValue: &p.Plugin,
})
values = append(values, &protocol.FieldValue{
StringValue: &p.PluginInstance,
})
values = append(values, &protocol.FieldValue{
StringValue: &p.Type,
})
values = append(values, &protocol.FieldValue{
StringValue: &p.TypeInstance,
})
values = append(values, &protocol.FieldValue{
StringValue: &dataSet.Name,
})
values = append(values, &protocol.FieldValue{
StringValue: &dataSet.TypeName,
})
values = append(values, &protocol.FieldValue{
DoubleValue: &dataSet.Value,
})
points := []*protocol.Point{
{
Timestamp: &ts,
Values: values,
},
}
series[i] = &protocol.Series{
Name: &metricName,
Fields: []string{
"host",
"plugin",
"plugin_instance",
"type",
"type_instance",
"dsname",
"dstype",
"value",
},
Points: points,
}
}
return series
}
var wrapOnce sync.Once
func logTimerWrapOnce() {
wrapOnce.Do(func() {
log.Error("Collectd timestamp too large for InfluxDB. Wrapping around to 0.")
})
}