@@ -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,28 @@ func (s ProtocolsStruct) configureProtocol(
176
171
return nil
177
172
}
178
173
174
+ // This could happen earlier, but let the errors be found first.
175
+ var protocol struct {
176
+ Device string `config:"interface"`
177
+ }
178
+ err := config .Unpack (& protocol )
179
+ if err != nil {
180
+ return err
181
+ }
182
+ if protocol .Device != "" && protocol .Device != device {
183
+ return nil
184
+ }
185
+
179
186
var client beat.Client
180
187
results := func (beat.Event ) {}
181
- if ! testMode {
182
- var err error
188
+ if ! test {
183
189
results , err = pub .CreateReporter (config )
184
190
if err != nil {
185
191
return err
186
192
}
187
193
}
188
194
189
- inst , err := plugin (testMode , results , watcher , config )
195
+ inst , err := plugin (test , results , watch , config )
190
196
if err != nil {
191
197
logp .Err ("Failed to register protocol plugin: %v" , err )
192
198
return err
@@ -196,6 +202,30 @@ func (s ProtocolsStruct) configureProtocol(
196
202
return nil
197
203
}
198
204
205
+ func (s ProtocolsStruct ) register (proto Protocol , client beat.Client , plugin Plugin ) {
206
+ if _ , exists := s .all [proto ]; exists {
207
+ logp .Warn ("Protocol (%s) plugin will overwritten by another plugin" , proto .String ())
208
+ }
209
+
210
+ s .all [proto ] = protocolInstance {
211
+ client : client ,
212
+ plugin : plugin ,
213
+ }
214
+
215
+ success := false
216
+ if tcp , ok := plugin .(TCPPlugin ); ok {
217
+ s .tcp [proto ] = tcp
218
+ success = true
219
+ }
220
+ if udp , ok := plugin .(UDPPlugin ); ok {
221
+ s .udp [proto ] = udp
222
+ success = true
223
+ }
224
+ if ! success {
225
+ logp .Warn ("Protocol (%s) register failed, port: %v" , proto .String (), plugin .GetPorts ())
226
+ }
227
+ }
228
+
199
229
func (s ProtocolsStruct ) GetTCP (proto Protocol ) TCPPlugin {
200
230
plugin , exists := s .tcp [proto ]
201
231
if ! exists {
@@ -272,27 +302,3 @@ func (s ProtocolsStruct) BpfFilter(withVlans bool, withICMP bool) string {
272
302
}
273
303
return filter
274
304
}
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