-
Notifications
You must be signed in to change notification settings - Fork 2
Implement Strobes Analysis/ Filtering For RITA v2 #91
base: master
Are you sure you want to change the base?
Changes from all commits
e586da9
7faccea
0e9db64
fdbb10e
d104c4e
e3e8f44
76703d8
7ef6ac2
db42a65
cf027de
f0a75f5
fcbfca0
73ffd8d
e41b231
f7e2f39
4ec5b38
0fedf6f
3c39f84
b11114a
5aa48e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,12 @@ package commands | |
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/activecm/ipfix-rita/converter/config" | ||
| "github.com/activecm/ipfix-rita/converter/config/yaml" | ||
| "github.com/activecm/ipfix-rita/converter/input/logstash/mongodb" | ||
| "github.com/activecm/ipfix-rita/converter/output/rita" | ||
| "github.com/urfave/cli" | ||
| "time" | ||
| ) | ||
|
|
||
| func init() { | ||
|
|
@@ -53,7 +53,7 @@ func init() { | |
| fmt.Printf("Found %d Flow Records Ready For Processing\n", count) | ||
| coll.Database.Session.Close() | ||
|
|
||
| outDB, err := rita.NewOutputDB(conf.GetOutputConfig().GetRITAConfig()) | ||
| outDB, err := rita.NewDBManager(conf.GetOutputConfig().GetRITAConfig(), 100, 1*time.Second) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to add some dummy values to make the constructor happy. |
||
| if err != nil { | ||
| return cli.NewExitError(fmt.Sprintf("%+v\n", err), 1) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,13 @@ type RITA interface { | |
| GetConnectionConfig() MongoDBConnection | ||
| GetDBRoot() string | ||
| GetMetaDB() string | ||
| GetStrobe() Strobe | ||
| } | ||
|
|
||
| //Strobe contains configuration for populating the | ||
| //freqConn collection / Strobes analysis in RITA | ||
| type Strobe interface { | ||
| GetConnectionLimit() int | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally had to touch a few files to implement the ConnectionLimit config option for strobes as in RITA. Changed: |
||
|
|
||
| //Filtering contains information on local subnets and other networks/hosts | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ import ( | |
| "github.com/activecm/ipfix-rita/converter/logging" | ||
| "github.com/activecm/ipfix-rita/converter/output" | ||
| "github.com/activecm/ipfix-rita/converter/output/rita" | ||
| "github.com/activecm/ipfix-rita/converter/output/rita/buffered" | ||
| "github.com/activecm/ipfix-rita/converter/stitching/session" | ||
| "github.com/activecm/rita/parser/parsetypes" | ||
| "github.com/pkg/errors" | ||
|
|
@@ -23,14 +22,12 @@ import ( | |
| //before being sent to MongoDB. The buffers are flushed when | ||
| //they are full or after a deadline passes for the individual buffer. | ||
| type batchRITAConnDateWriter struct { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The batch writer has to be activated with a cli flag. Largely references to the AutoFlushCollection directly tied to the conn collection have been replaced with calls to rita.DB |
||
| db rita.OutputDB | ||
| localNets []net.IPNet | ||
| outputCollections map[string]*buffered.AutoFlushCollection | ||
| bufferSize int64 | ||
| autoFlushTime time.Duration | ||
| autoFlushContext context.Context | ||
| autoFlushOnFatal func() | ||
| log logging.Logger | ||
| db rita.DBManager | ||
| localNets []net.IPNet | ||
| outputDBs map[string]rita.DB | ||
| autoFlushContext context.Context | ||
| autoFlushOnFatal func() | ||
| log logging.Logger | ||
| } | ||
|
|
||
| //NewBatchRITAConnDateWriter creates an buffered RITA compatible writer | ||
|
|
@@ -40,22 +37,20 @@ type batchRITAConnDateWriter struct { | |
| //when the buffer is full or after a deadline passes. | ||
| func NewBatchRITAConnDateWriter(ritaConf config.RITA, localNets []net.IPNet, | ||
| bufferSize int64, autoFlushTime time.Duration, log logging.Logger) (output.SessionWriter, error) { | ||
| db, err := rita.NewOutputDB(ritaConf) | ||
| db, err := rita.NewDBManager(ritaConf, bufferSize, autoFlushTime) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "could not connect to RITA MongoDB") | ||
| } | ||
|
|
||
| autoFlushContext, autoFlushOnFail := context.WithCancel(context.Background()) | ||
| //return the new writer | ||
| return &batchRITAConnDateWriter{ | ||
| db: db, | ||
| localNets: localNets, | ||
| outputCollections: make(map[string]*buffered.AutoFlushCollection), | ||
| bufferSize: bufferSize, | ||
| autoFlushTime: autoFlushTime, | ||
| autoFlushContext: autoFlushContext, | ||
| autoFlushOnFatal: autoFlushOnFail, | ||
| log: log, | ||
| db: db, | ||
| localNets: localNets, | ||
| outputDBs: make(map[string]rita.DB), | ||
| autoFlushContext: autoFlushContext, | ||
| autoFlushOnFatal: autoFlushOnFail, | ||
| log: log, | ||
| }, nil | ||
| } | ||
|
|
||
|
|
@@ -90,14 +85,14 @@ func (r *batchRITAConnDateWriter) Write(sessions <-chan *session.Aggregate) <-ch | |
| sess.ToRITAConn(&connRecord, r.isIPLocal) | ||
|
|
||
| //create/ get the buffered output collection | ||
| outColl, err := r.getConnCollectionForSession(sess, errs, r.autoFlushOnFatal) | ||
| outDB, err := r.getDBForSession(sess, errs, r.autoFlushOnFatal) | ||
| if err != nil { | ||
| errs <- err | ||
| break WriteLoop | ||
| } | ||
|
|
||
| //insert the record | ||
| err = outColl.Insert(connRecord) | ||
| err = outDB.InsertConnRecord(&connRecord) | ||
| if err != nil { | ||
| errs <- err | ||
| break WriteLoop | ||
|
|
@@ -109,11 +104,13 @@ func (r *batchRITAConnDateWriter) Write(sessions <-chan *session.Aggregate) <-ch | |
| } | ||
|
|
||
| func (r *batchRITAConnDateWriter) closeDBSessions(errs chan<- error) { | ||
| for i := range r.outputCollections { | ||
| r.outputCollections[i].Close() | ||
| for i := range r.outputDBs { | ||
| err := r.outputDBs[i].Close() | ||
| if err != nil { | ||
| errs <- err | ||
| } | ||
|
|
||
| err := r.db.MarkImportFinishedInMetaDB(r.outputCollections[i].Database()) | ||
| //stops outputCollections from sending on errs | ||
| err = r.outputDBs[i].MarkFinished() | ||
| if err != nil { | ||
| errs <- err | ||
| } | ||
|
|
@@ -132,39 +129,28 @@ func (r *batchRITAConnDateWriter) isIPLocal(ipAddrStr string) bool { | |
| return false | ||
| } | ||
|
|
||
| func (r *batchRITAConnDateWriter) getConnCollectionForSession(sess *session.Aggregate, | ||
| autoFlushAsyncErrChan chan<- error, autoFlushOnFatal func()) (*buffered.AutoFlushCollection, error) { | ||
| func (r *batchRITAConnDateWriter) getDBForSession(sess *session.Aggregate, | ||
| autoFlushAsyncErrChan chan<- error, autoFlushOnFatal func()) (rita.DB, error) { | ||
|
|
||
| //get the latest flowEnd time | ||
| endTimeMilliseconds := sess.FlowEndMilliseconds() | ||
| //time.Unix(seconds, nanoseconds) | ||
| //1000 milliseconds per second, 1000 nanosecodns to a microsecond. 1000 microseconds to a millisecond | ||
| //1000 milliseconds per second, 1000 nanoseconds to a microsecond. 1000 microseconds to a millisecond | ||
| endTime := time.Unix(endTimeMilliseconds/1000, (endTimeMilliseconds%1000)*1000*1000) | ||
| endTimeStr := endTime.Format("2006-01-02") | ||
|
|
||
| //cache the database connection | ||
| outBufferedColl, ok := r.outputCollections[endTimeStr] | ||
| outDB, ok := r.outputDBs[endTimeStr] | ||
| if !ok { | ||
| //connect to the db | ||
| var err error | ||
| outColl, err := r.db.NewRITAOutputConnection(endTimeStr) | ||
| outDB, err := r.db.NewRitaDB(endTimeStr, autoFlushAsyncErrChan, autoFlushOnFatal) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NewRITADB encapsulates the removed logic. |
||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "could not connect to output database for suffix: %s", endTimeStr) | ||
| return outDB, errors.Wrapf(err, "could not create output database for suffix: %s", endTimeStr) | ||
| } | ||
|
|
||
| //create the meta db record | ||
| err = r.db.EnsureMetaDBRecordExists(outColl.Database.Name) | ||
| if err != nil { | ||
| outColl.Database.Session.Close() | ||
| return nil, err | ||
| } | ||
|
|
||
| //create the output buffer | ||
| outBufferedColl = buffered.NewAutoFlushCollection(outColl, r.bufferSize, r.autoFlushTime) | ||
| outBufferedColl.StartAutoFlush(autoFlushAsyncErrChan, autoFlushOnFatal) | ||
|
|
||
| //cache the result | ||
| r.outputCollections[endTimeStr] = outBufferedColl | ||
| r.outputDBs[endTimeStr] = outDB | ||
| } | ||
| return outBufferedColl, nil | ||
| return outDB, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package constants | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the strobes code is separate from the rita db manager code, and both sets of code needed access to these constants, a new package was created to hold them. This prevents import cycles. |
||
|
|
||
| //MetaDBDatabasesCollection is the name of the RITA collection | ||
| //in the RITA MetaDB that keeps track of RITA managed databases | ||
| const MetaDBDatabasesCollection = "databases" | ||
|
|
||
| //StrobesCollection contains the name for the RITA freqConn MongoDB collection | ||
| const StrobesCollection = "freqConn" | ||
|
|
||
| //ConnCollection contains the name for the RITA conn MongoDB collection | ||
| const ConnCollection = "conn" | ||
|
|
||
| // Version specifies which RITA DB schema the resulting data matches | ||
| var Version = "v2.0.0+ActiveCM-IPFIX" | ||
|
|
||
| // TODO: Use version in RITA as dep | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have been updated earlier, but it the change was needed now as we have to get the parsetypes.Freq object out of RITA to get the needed MongoDB indexes for the freqConn collection