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
|
package processing
// DCSO FEVER
// Copyright (c) 2017, DCSO GmbH
import (
"bytes"
"os"
"strings"
"sync"
"time"
"github.com/DCSO/fever/types"
"github.com/DCSO/fever/util"
"github.com/DCSO/bloom"
log "github.com/sirupsen/logrus"
)
// FlowExtractor is an aggregator that extracts the flows from
// "hosts of interest" and sends them to the backend.
type FlowExtractor struct {
SensorID string
BloomPath string
BloomFilter *bloom.BloomFilter
FlowsMutex sync.RWMutex
flowCount int
Flows *bytes.Buffer
SubmitChannel chan []byte
Submitter util.StatsSubmitter
FlushPeriod time.Duration
FlushCount int
CloseChan chan bool
ClosedChan chan bool
Logger *log.Entry
}
// MakeFlowExtractor creates a new empty FlowExtractor.
func MakeFlowExtractor(flushPeriod time.Duration, flushCount int, bloomPath string, submitter util.StatsSubmitter) (*FlowExtractor, error) {
var bloomFilter *bloom.BloomFilter
if bloomPath != "" {
compressed := false
if strings.HasSuffix(bloomPath, ".gz") {
compressed = true
}
var err error
bloomFilter, err = bloom.LoadFilter(bloomPath, compressed)
if err != nil {
return nil, err
}
}
fe := &FlowExtractor{
FlushPeriod: flushPeriod,
Submitter: submitter,
BloomPath: bloomPath,
Logger: log.WithFields(log.Fields{
"domain": "flow_extractor",
}),
Flows: new(bytes.Buffer),
SubmitChannel: make(chan []byte, 60),
BloomFilter: bloomFilter,
CloseChan: make(chan bool),
ClosedChan: make(chan bool),
FlushCount: flushCount,
flowCount: 0,
}
fe.SensorID, _ = os.Hostname()
return fe, nil
}
func (fe *FlowExtractor) flush() {
fe.FlowsMutex.Lock()
myFlows := fe.Flows
fe.Flows = new(bytes.Buffer)
fe.flowCount = 0
fe.FlowsMutex.Unlock()
select {
case fe.SubmitChannel <- myFlows.Bytes():
break
default:
log.Warning("Flow channel is full, cannot submit message...")
}
}
// Consume processes an Entry, adding the data within to the flows
func (fe *FlowExtractor) Consume(e *types.Entry) error {
fe.FlowsMutex.Lock()
defer fe.FlowsMutex.Unlock()
if fe.BloomFilter != nil {
if !fe.BloomFilter.Check([]byte(e.SrcIP)) && !fe.BloomFilter.Check([]byte(e.DestIP)) {
return nil
}
}
var fev types.FlowEvent
err := fev.FromEntry(e)
if err != nil {
return err
}
err = fev.Marshal(fe.Flows)
fe.flowCount++
return err
}
// Run starts the background aggregation service for this handler
func (fe *FlowExtractor) Run() {
//this goroutine asynchronously submit flow messages
go func() {
for message := range fe.SubmitChannel {
fe.Submitter.Submit(message, "", "application/binary-flows")
}
}()
//this go routine takes care of flushing the flows
go func() {
i := 0 * time.Second
interval := 100 * time.Millisecond
for {
select {
case <-fe.CloseChan:
close(fe.SubmitChannel)
close(fe.ClosedChan)
return
default:
//we flush if the flush period has passed, or if the count
//of events is larger then the flush count
fe.FlowsMutex.Lock()
flowCount := fe.flowCount
fe.FlowsMutex.Unlock()
if i >= fe.FlushPeriod || flowCount > fe.FlushCount {
fe.flush()
i = 0 * time.Second
}
time.Sleep(interval)
i += interval
}
}
}()
}
// Stop causes the aggregator to cease aggregating and submitting data
func (fe *FlowExtractor) Stop(stopChan chan bool) {
close(fe.CloseChan)
<-fe.ClosedChan
close(stopChan)
}
// GetName returns the name of the handler
func (fe *FlowExtractor) GetName() string {
return "Flow extractor"
}
// GetEventTypes returns a slice of event type strings that this handler
// should be applied to
func (fe *FlowExtractor) GetEventTypes() []string {
return []string{"flow"}
}
|