This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NETOBSERV-44] Consumming goflow2 as a library.
Using goflow2 as a library to directly receive netflow event with the kube-enricher
- Loading branch information
1 parent
4921807
commit bc26bad
Showing
6 changed files
with
126 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package netflow | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
goflow2Format "github.com/netsampler/goflow2/format" | ||
_ "github.com/netsampler/goflow2/format/protobuf" | ||
"github.com/netsampler/goflow2/utils" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type GoflowDriver struct { | ||
in chan map[string]interface{} | ||
} | ||
|
||
func NewDriver(hostname string, port int) *GoflowDriver { | ||
gf := GoflowDriver{} | ||
gf.in = make(chan map[string]interface{}) | ||
|
||
go func(in chan map[string]interface{}) { | ||
ctx := context.Background() | ||
|
||
transporter := NewWrapper(in) | ||
|
||
formatter, err := goflow2Format.FindFormat(ctx, "pb") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
sNF := &utils.StateNetFlow{ | ||
Format: formatter, | ||
Transport: transporter, | ||
Logger: logrus.StandardLogger(), | ||
} | ||
err = sNF.FlowRoutine(1, hostname, port, false) | ||
log.Fatal(err) | ||
|
||
}(gf.in) | ||
|
||
return &gf | ||
} | ||
|
||
func (gf *GoflowDriver) Next() (map[string]interface{}, error) { | ||
msg := <-gf.in | ||
return msg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package netflow | ||
|
||
import ( | ||
"github.com/golang/protobuf/proto" | ||
pbFormat "github.com/netobserv/goflow2-kube-enricher/pkg/format/pb" | ||
goflowpb "github.com/netsampler/goflow2/pb" | ||
) | ||
|
||
// This is en implementation of the goflow2 transport interface | ||
type TransportWrapper struct { | ||
c chan map[string]interface{} | ||
} | ||
|
||
func NewWrapper(c chan map[string]interface{}) *TransportWrapper { | ||
tw := TransportWrapper{c: c} | ||
return &tw | ||
} | ||
|
||
func (w *TransportWrapper) Send(key, data []byte) error { | ||
message := goflowpb.FlowMessage{} | ||
err := proto.Unmarshal(data, &message) | ||
if err != nil { | ||
return err | ||
} | ||
renderedMsg, err := pbFormat.RenderMessage(message) | ||
if err == nil { | ||
w.c <- renderedMsg | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters