@@ -110,50 +110,45 @@ type reporterFactory interface {
110
110
CreateReporter (* conf.C ) (func (beat.Event ), error )
111
111
}
112
112
113
- func (s ProtocolsStruct ) Init (
114
- testMode bool ,
115
- pub reporterFactory ,
116
- watcher * procs.ProcessesWatcher ,
117
- configs map [string ]* conf.C ,
118
- listConfigs []* conf.C ,
119
- ) error {
120
- if len (configs ) > 0 {
113
+ func (s ProtocolsStruct ) Init (test bool , pub reporterFactory , watch * procs.ProcessesWatcher , cfgs map [string ]* conf.C , list []* conf.C ) error {
114
+ return s .InitFiltered (test , "" , pub , watch , cfgs , list )
115
+ }
116
+
117
+ func (s ProtocolsStruct ) InitFiltered (test bool , device string , pub reporterFactory , watch * procs.ProcessesWatcher , cfgs map [string ]* conf.C , list []* conf.C ) error {
118
+ if len (cfgs ) != 0 {
121
119
cfgwarn .Deprecate ("7.0.0" , "dictionary style protocols configuration has been deprecated. Please use list-style protocols configuration." )
122
120
}
123
121
124
122
for proto := range protocolSyms {
125
123
logp .Debug ("protos" , "registered protocol plugin: %v" , proto )
126
124
}
127
125
128
- for name , config := range configs {
129
- if err := s .configureProtocol (testMode , pub , watcher , name , config ); err != nil {
126
+ for name , cfg := range cfgs {
127
+ err := s .configureProtocol (test , device , pub , watch , name , cfg )
128
+ if err != nil {
130
129
return err
131
130
}
132
131
}
133
132
134
- for _ , config := range listConfigs {
133
+ for _ , cfg := range list {
135
134
module := struct {
136
135
Name string `config:"type" validate:"required"`
137
136
}{}
138
- if err := config .Unpack (& module ); err != nil {
137
+ err := cfg .Unpack (& module )
138
+ if err != nil {
139
139
return err
140
140
}
141
141
142
- if err := s .configureProtocol (testMode , pub , watcher , module .Name , config ); err != nil {
142
+ err = s .configureProtocol (test , device , pub , watch , module .Name , cfg )
143
+ if err != nil {
143
144
return err
144
145
}
145
146
}
146
147
147
148
return nil
148
149
}
149
150
150
- func (s ProtocolsStruct ) configureProtocol (
151
- testMode bool ,
152
- pub reporterFactory ,
153
- watcher * procs.ProcessesWatcher ,
154
- name string ,
155
- config * conf.C ,
156
- ) error {
151
+ func (s ProtocolsStruct ) configureProtocol (test bool , device string , pub reporterFactory , watch * procs.ProcessesWatcher , name string , config * conf.C ) error {
157
152
// XXX: icmp is special, ignore here :/
158
153
if name == "icmp" {
159
154
return nil
@@ -176,17 +171,31 @@ func (s ProtocolsStruct) configureProtocol(
176
171
return nil
177
172
}
178
173
174
+ if device != "" {
175
+ // This could happen earlier, but let any errors be found first.
176
+ var protocol struct {
177
+ Device string `config:"interface"`
178
+ }
179
+ err := config .Unpack (& protocol )
180
+ if err != nil {
181
+ return err
182
+ }
183
+ if protocol .Device != "" && protocol .Device != device {
184
+ return nil
185
+ }
186
+ }
187
+
179
188
var client beat.Client
180
189
results := func (beat.Event ) {}
181
- if ! testMode {
190
+ if ! test {
182
191
var err error
183
192
results , err = pub .CreateReporter (config )
184
193
if err != nil {
185
194
return err
186
195
}
187
196
}
188
197
189
- inst , err := plugin (testMode , results , watcher , config )
198
+ inst , err := plugin (test , results , watch , config )
190
199
if err != nil {
191
200
logp .Err ("Failed to register protocol plugin: %v" , err )
192
201
return err
@@ -196,6 +205,30 @@ func (s ProtocolsStruct) configureProtocol(
196
205
return nil
197
206
}
198
207
208
+ func (s ProtocolsStruct ) register (proto Protocol , client beat.Client , plugin Plugin ) {
209
+ if _ , exists := s .all [proto ]; exists {
210
+ logp .Warn ("Protocol (%s) plugin will overwritten by another plugin" , proto .String ())
211
+ }
212
+
213
+ s .all [proto ] = protocolInstance {
214
+ client : client ,
215
+ plugin : plugin ,
216
+ }
217
+
218
+ success := false
219
+ if tcp , ok := plugin .(TCPPlugin ); ok {
220
+ s .tcp [proto ] = tcp
221
+ success = true
222
+ }
223
+ if udp , ok := plugin .(UDPPlugin ); ok {
224
+ s .udp [proto ] = udp
225
+ success = true
226
+ }
227
+ if ! success {
228
+ logp .Warn ("Protocol (%s) register failed, port: %v" , proto .String (), plugin .GetPorts ())
229
+ }
230
+ }
231
+
199
232
func (s ProtocolsStruct ) GetTCP (proto Protocol ) TCPPlugin {
200
233
plugin , exists := s .tcp [proto ]
201
234
if ! exists {
@@ -228,7 +261,7 @@ func (s ProtocolsStruct) GetAllUDP() map[Protocol]UDPPlugin {
228
261
// and unencapsulated packets
229
262
func (s ProtocolsStruct ) BpfFilter (withVlans bool , withICMP bool ) string {
230
263
// Sort the protocol IDs so that the return value is consistent.
231
- var protos []int
264
+ protos := make ( []int , 0 , len ( s . all ))
232
265
for proto := range s .all {
233
266
protos = append (protos , int (proto ))
234
267
}
@@ -272,27 +305,3 @@ func (s ProtocolsStruct) BpfFilter(withVlans bool, withICMP bool) string {
272
305
}
273
306
return filter
274
307
}
275
-
276
- func (s ProtocolsStruct ) register (proto Protocol , client beat.Client , plugin Plugin ) {
277
- if _ , exists := s .all [proto ]; exists {
278
- logp .Warn ("Protocol (%s) plugin will overwritten by another plugin" , proto .String ())
279
- }
280
-
281
- s .all [proto ] = protocolInstance {
282
- client : client ,
283
- plugin : plugin ,
284
- }
285
-
286
- success := false
287
- if tcp , ok := plugin .(TCPPlugin ); ok {
288
- s .tcp [proto ] = tcp
289
- success = true
290
- }
291
- if udp , ok := plugin .(UDPPlugin ); ok {
292
- s .udp [proto ] = udp
293
- success = true
294
- }
295
- if ! success {
296
- logp .Warn ("Protocol (%s) register failed, port: %v" , proto .String (), plugin .GetPorts ())
297
- }
298
- }
0 commit comments