-
Notifications
You must be signed in to change notification settings - Fork 4
/
sources.go
357 lines (297 loc) · 8.49 KB
/
sources.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"fmt"
"io/ioutil"
"os"
"plugin"
"reflect"
"time"
yaml "gopkg.in/yaml.v3"
"github.com/cert-lv/graphoscope/pdk"
)
var (
// Loaded plugins
plugins map[string]interface{}
// Collectors for the preconfigured data sources.
// Is a map of data source's name -> related plugin
collectors map[string]pdk.SourcePlugin
// Processors of the data received by the collectors,
// runs in a background for each entry
processors = []pdk.ProcessorPlugin{}
// In a Web GUI do not display some elements
// when there are no non-global data sources
nonGlobalExist = false
// A list of all known data sources fields
// for the Web GUI autocomplete
fields map[string][]string
)
/*
* Load plugins from a configured directory
*/
func loadPlugins() error {
plugins = make(map[string]interface{})
// Load several types of plugins
for _, group := range []string{"sources", "processors", "outputs"} {
files, err := ioutil.ReadDir(config.Plugins + "/" + group)
if err != nil {
return fmt.Errorf("Can't read from '%s' directory: %s", config.Plugins+"/"+group, err.Error())
}
for _, f := range files {
// Skip non-plugin files
name := f.Name()
if name[len(name)-3:] != ".so" {
continue
}
// Open a .so file to load the symbols
plug, err := plugin.Open(config.Plugins + "/" + group + "/" + name)
if err != nil {
return fmt.Errorf("Can't open '%s': %s", name, err.Error())
}
// Look up the main plugin's symbol
symPlugin, err := plug.Lookup("Plugin")
if err != nil {
return fmt.Errorf("Can't lookup symbol 'Plugin' in '%s': %s", name, err.Error())
}
// Get plugin name
symName, err := plug.Lookup("Name")
if err != nil {
return fmt.Errorf("Can't lookup symbol 'Name' in '%s': %s", name, err.Error())
}
pName, ok := symName.(*string)
if !ok {
return fmt.Errorf("Unexpected plugin name type in '%s': %T, '*string' expected", name, pName)
}
// Get plugin version
symVersion, err := plug.Lookup("Version")
if err != nil {
return fmt.Errorf("Can't lookup symbol 'Version' in '%s': %s", name, err.Error())
}
pVersion, ok := symVersion.(*string)
if !ok {
return fmt.Errorf("Unexpected plugin version type in '%s': %T, '*string' expected", name, pVersion)
}
// Assert that loaded symbol is of a desired type
// and make plugin globally available
// switch group {
// case "source":
// plugin, ok := symPlugin.(pdk.SourcePlugin)
// plugins[*pName] = plugin
// case "process":
// plugin, ok := symPlugin.(pdk.ProcessPlugin)
// plugins[*pName] = plugin
// case "output":
// plugin, ok := symPlugin.(pdk.OutputPlugin)
// plugins[*pName] = plugin
// }
// if !ok {
// return fmt.Errorf("Invalid plugin's type of '%s': %T, all methods must be implemented", name, symPlugin)
// }
plugins[*pName] = symPlugin
log.Info().
Str("plugin", *pName).
Msg("Plugin loaded, version " + *pVersion)
}
}
return nil
}
/*
* Setup collectors for the predefined data sources
*/
func setupCollectors() error {
// Clear old content
collectors = make(map[string]pdk.SourcePlugin)
files, err := ioutil.ReadDir(config.Definitions + "/sources")
if err != nil {
return fmt.Errorf("Can't read directory '%s': %s", config.Definitions+"/sources", err.Error())
}
// A map of data sources fields,
// source name -> list
fields = make(map[string][]string)
// Reset flag in case collectors are reloaded without service restart
nonGlobalExist = false
for _, f := range files {
// Skip not YAML files
name := f.Name()
if len(name) <= 5 || name[len(name)-5:] != ".yaml" {
continue
}
def, err := loadSource(config.Definitions + "/sources/" + name)
if err != nil {
log.Error().Msgf("Can't load source file '%s': %s", name, err.Error())
continue
}
// Use needed plugin
collector, ok := plugins[def.Plugin].(pdk.SourcePlugin)
if !ok {
log.Error().
Str("source", def.Name).
Str("plugin", def.Plugin).
Msg("No such plugin required by a collector")
continue
}
// Clone interface to avoid pointers in "collectors" to the same value
collectorIntf := reflect.New(reflect.TypeOf(collector).Elem())
clone := collectorIntf.Interface().(pdk.SourcePlugin)
// Close previous connection if exists
err = clone.Stop()
if err != nil {
log.Error().
Str("source", def.Name).
Str("plugin", def.Plugin).
Msg("Can't stop collector: " + err.Error())
continue
}
// Set current unique parameters
err = clone.Setup(def, config.Limit)
if err != nil {
log.Error().
Str("source", def.Name).
Str("plugin", def.Plugin).
Msg("Can't setup: " + err.Error())
continue
}
// Get all the possible field names
list, err := clone.Fields()
if err != nil {
log.Error().
Str("source", def.Name).
Str("plugin", def.Plugin).
Msg("Can't get fields: " + err.Error())
}
// Prevent NULL values in resulting JSON
if len(list) == 0 {
list = make([]string, 0)
}
// Rename common field names
for renamed, old := range clone.Conf().ReplaceFields {
for i, field := range list {
if field == old {
list[i] = renamed
break
}
}
}
// Merge field names with a global list
fields[def.Name] = list
if !clone.Conf().InGlobal {
nonGlobalExist = true
}
// Store collectors to be usable by the end-users
collectors[def.Name] = clone
log.Info().
Str("source", def.Name).
Str("plugin", def.Plugin).
Msg("Collector initialized")
}
return nil
}
/*
* Setup processors of the data sources received data
*/
func setupProcessors() error {
// Clear old content
processors = []pdk.ProcessorPlugin{}
files, err := ioutil.ReadDir(config.Definitions + "/processors")
if err != nil {
return fmt.Errorf("Can't read directory '%s': %s", config.Definitions+"/processors", err.Error())
}
for _, f := range files {
// Skip not YAML files
name := f.Name()
if len(name) <= 5 || name[len(name)-5:] != ".yaml" {
continue
}
def, err := loadProcessor(config.Definitions + "/processors/" + name)
if err != nil {
log.Error().Msgf("Can't load processor file '%s': %s", name, err.Error())
continue
}
// Use needed plugin
processor, ok := plugins[def.Plugin].(pdk.ProcessorPlugin)
if !ok {
log.Error().
Str("process", def.Name).
Str("plugin", def.Plugin).
Msg("No such plugin required by a processor")
continue
}
// Clone interface to avoid pointers in "processors" to the same value
processorIntf := reflect.New(reflect.TypeOf(processor).Elem())
clone := processorIntf.Interface().(pdk.ProcessorPlugin)
// Close previous connection if exists
err = clone.Stop()
if err != nil {
log.Error().
Str("process", def.Name).
Str("plugin", def.Plugin).
Msg("Can't stop processor: " + err.Error())
continue
}
// Set current unique parameters
err = clone.Setup(def)
if err != nil {
log.Error().
Str("process", def.Name).
Str("plugin", def.Plugin).
Msg("Can't setup: " + err.Error())
continue
}
// Store processors to be usable by the end-users
processors = append(processors, clone)
log.Info().
Str("processor", def.Name).
Str("plugin", def.Plugin).
Msg("Processor initialized")
}
return nil
}
/*
* Load data source configuration file
*/
func loadSource(filename string) (*pdk.Source, error) {
confFile, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("Can't open: " + err.Error())
}
fi, _ := confFile.Stat()
buffer := make([]byte, fi.Size())
_, err = confFile.Read(buffer)
if err != nil {
return nil, fmt.Errorf("Can't read: " + err.Error())
}
source := &pdk.Source{}
err = yaml.Unmarshal(buffer, &source)
if err != nil {
return nil, fmt.Errorf("Can't unmarshall: " + err.Error())
}
// Set default values if not specified
if source.Timeout == 0*time.Second {
source.Timeout = 60 * time.Second
}
return source, nil
}
/*
* Load processor configuration file
*/
func loadProcessor(filename string) (*pdk.Processor, error) {
confFile, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("Can't open: " + err.Error())
}
fi, _ := confFile.Stat()
buffer := make([]byte, fi.Size())
_, err = confFile.Read(buffer)
if err != nil {
return nil, fmt.Errorf("Can't read: " + err.Error())
}
processor := &pdk.Processor{}
err = yaml.Unmarshal(buffer, &processor)
if err != nil {
return nil, fmt.Errorf("Can't unmarshall: " + err.Error())
}
// Set default values if not specified
if processor.Timeout == 0*time.Second {
processor.Timeout = 60 * time.Second
}
return processor, nil
}