/
routines.go
240 lines (220 loc) · 5.21 KB
/
routines.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*************************************************************************
* Copyright 2017 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/gravwell/gravwell/v3/ingest"
"github.com/gravwell/gravwell/v3/ingest/entry"
"github.com/gravwell/gravwell/v3/ingest/processors"
"collectd.org/api"
"collectd.org/network"
)
const (
empty runState = iota
ready runState = iota
running runState = iota
done runState = iota
closed runState = iota
)
type runState int
type collConfig struct {
wg *sync.WaitGroup
igst *ingest.IngestMuxer
pl passlookup
seclevel network.SecurityLevel
defTag entry.EntryTag
overrides map[string]entry.EntryTag
srcOverride net.IP
src net.IP // used if srcOverride is not set
proc *processors.ProcessorSet
}
func (bc collConfig) Validate() error {
if bc.wg == nil {
return errors.New("nil wait group")
} else if bc.igst == nil {
return errors.New("Nil ingest muxer")
} else if bc.proc == nil {
return errors.New("Nil processor set")
}
return nil
}
type collectdInstance struct {
collConfig
sync.Mutex
srv network.Server
state runState
errCh chan error
useOverrides bool
useSrcOverride bool
cancel *context.CancelFunc
}
func newCollectdInstance(cc collConfig, laddr *net.UDPAddr) (*collectdInstance, error) {
if err := cc.Validate(); err != nil {
return nil, err
}
conn, err := net.ListenUDP(`udp`, laddr)
if err != nil {
return nil, err
}
//build out server for each listener
srv := network.Server{
Conn: conn,
SecurityLevel: cc.seclevel,
PasswordLookup: cc.pl,
}
//we have some self referencing circles here, but the call chain shouldn't ever let it be expressed
ci := &collectdInstance{
collConfig: cc,
srv: srv,
state: ready,
useOverrides: len(cc.overrides) > 0,
useSrcOverride: len(cc.srcOverride) > 0,
}
ci.srv.Writer = ci
return ci, nil
}
func (ci *collectdInstance) Start() error {
ci.Lock()
defer ci.Unlock()
if ci.state != ready {
return errors.New("Not ready")
}
ci.state = running
ci.errCh = make(chan error, 1)
go ci.routine(ci.errCh)
return nil
}
func (ci *collectdInstance) routine(ch chan error) {
ctx, cancel := context.WithCancel(context.Background())
ci.Lock()
ci.cancel = &cancel
ci.Unlock()
err := ci.srv.ListenAndWrite(ctx)
if err == context.Canceled {
err = nil //just closing
}
ch <- err
ci.Lock()
ci.state = done
ci.Unlock()
}
func (ci *collectdInstance) Close() (err error) {
ci.Lock()
if ci.cancel != nil {
(*ci.cancel)()
}
ch := ci.errCh
if ci.state != running && ci.state != done {
ci.Unlock()
err = errors.New("Not running")
return
}
ci.Unlock()
if ch != nil {
if err = <-ch; err == nil {
err = ci.proc.Close()
} else {
ci.proc.Close()
}
}
return
}
func (ci *collectdInstance) Write(ctx context.Context, vl *api.ValueList) error {
var tag entry.EntryTag
var src net.IP
dts, err := marshalJSON(vl)
if err != nil {
return err
}
if ci.useOverrides {
var ok bool
if tag, ok = ci.overrides[vl.Plugin]; !ok {
tag = ci.defTag
}
} else {
tag = ci.defTag
}
if ci.useSrcOverride {
src = ci.srcOverride
} else {
// if src is set, use it, otherwise ask the ingester
if ci.src.IsUnspecified() {
if src, err = ci.igst.SourceIP(); err == nil {
ci.src = src
}
}
src = ci.src // worst case we send 0.0.0.0
}
ts := entry.FromStandard(vl.Time)
for i := range dts {
ent := &entry.Entry{
TS: ts,
Tag: tag,
SRC: src,
Data: dts[i],
}
if err := ci.proc.ProcessContext(ent, ctx); err != nil {
return err
}
}
return nil
}
type dumbprinter struct {
}
func (df dumbprinter) Write(ctx context.Context, vl *api.ValueList) error {
if dts, err := marshalJSON(vl); err != nil {
return err
} else {
for i := range dts {
fmt.Println(string(dts[i]))
}
}
return nil
}
type jvl struct {
Host string `json:"host,omitempty"`
Plugin string `json:"plugin,omitempty"`
PluginInstance string `json:"plugin_instance,omitempty"`
Type string `json:"type,omitempty"`
TypeInstance string `json:"type_instance,omitempty"`
Value api.Value `json:"value,omitempty"`
DS string `json:"dsname,omitemtpy"`
Time time.Time `json:"time"`
Interval time.Duration `json:"interval"`
}
func marshalJSON(vl *api.ValueList) (dts [][]byte, err error) {
if vl == nil {
err = errors.New("empty value list")
return
}
v := jvl{
Host: vl.Host,
Plugin: vl.Plugin,
PluginInstance: vl.PluginInstance,
Type: vl.Type,
TypeInstance: vl.TypeInstance,
Time: vl.Time,
Interval: vl.Interval,
}
for i := range vl.Values {
var dt []byte
v.Value = vl.Values[i]
v.DS = vl.DSName(i)
if dt, err = json.Marshal(v); err != nil {
return
}
dts = append(dts, dt)
}
return
}