File: flow_profiler.go

package info (click to toggle)
fever 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 920 kB
  • sloc: sh: 41; makefile: 18
file content (155 lines) | stat: -rw-r--r-- 3,962 bytes parent folder | download | duplicates (5)
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
package processing

// DCSO FEVER
// Copyright (c) 2020, DCSO GmbH

import (
	"fmt"
	"sync"
	"time"

	"github.com/DCSO/fever/types"
	"github.com/DCSO/fever/util"
	log "github.com/sirupsen/logrus"
)

// ProtoProfile contains flow statistics for a give app layer protocol.
type ProtoProfile struct {
	PacketsToSrv uint64
	PacketsToClt uint64
	BytesToSrv   uint64
	BytesToClt   uint64
}

// FlowProfiler counts EVE event type statistics, such as number and size
// of JSON data received from the input.
type FlowProfiler struct {
	SensorID          string
	Host              string
	Profile           map[string]ProtoProfile
	FlushPeriod       time.Duration
	ProfileMutex      sync.Mutex
	CloseChan         chan bool
	ClosedChan        chan bool
	Logger            *log.Entry
	Submitter         util.StatsSubmitter
	SubmitChannel     chan []byte
	SubmitChannelFull bool
}

// MakeFlowProfiler creates a new FlowProfiler.
func MakeFlowProfiler(flushPeriod time.Duration, submitter util.StatsSubmitter) (*FlowProfiler, error) {
	a := &FlowProfiler{
		FlushPeriod: flushPeriod,
		Logger: log.WithFields(log.Fields{
			"domain": "flowprofiler",
		}),
		Profile:       make(map[string]ProtoProfile),
		CloseChan:     make(chan bool),
		ClosedChan:    make(chan bool),
		SubmitChannel: make(chan []byte, 60),
		Submitter:     submitter,
	}
	a.Host = getFQDN()
	return a, nil
}

func (a *FlowProfiler) formatLineProtocol() []string {
	out := make([]string, 0)
	a.ProfileMutex.Lock()
	myProfile := a.Profile
	for proto, protoVals := range myProfile {
		out = append(out, fmt.Sprintf("%s,host=%s,proto=%s flowbytestoclient=%d,flowbytestoserver=%d,flowpktstoclient=%d,flowpktstoserver=%d %d",
			util.ToolName, a.Host, proto,
			protoVals.BytesToClt, protoVals.BytesToSrv,
			protoVals.PacketsToClt, protoVals.PacketsToSrv,
			uint64(time.Now().UnixNano())))
		a.Profile[proto] = ProtoProfile{}
	}
	a.ProfileMutex.Unlock()
	return out
}

func (a *FlowProfiler) flush() {
	lineStrings := a.formatLineProtocol()
	for _, lineString := range lineStrings {
		select {
		case a.SubmitChannel <- []byte(lineString):
			if a.SubmitChannelFull {
				log.Warning("channel was free to submit again")
				a.SubmitChannelFull = false
			}
		default:
			if !a.SubmitChannelFull {
				log.Warning("channel is full, cannot submit message...")
				a.SubmitChannelFull = true
			}
		}
	}
}

// Consume processes an Entry, adding the data within to the internal
// aggregated state
func (a *FlowProfiler) Consume(e *types.Entry) error {
	aproto := e.AppProto
	if aproto == "" {
		aproto = "unknown"
	}
	a.ProfileMutex.Lock()
	profile := a.Profile[aproto]
	profile.BytesToClt += uint64(e.BytesToClient)
	profile.BytesToSrv += uint64(e.BytesToServer)
	profile.PacketsToClt += uint64(e.PktsToClient)
	profile.PacketsToSrv += uint64(e.PktsToServer)
	a.Profile[aproto] = profile
	a.ProfileMutex.Unlock()
	return nil
}

// Run starts the background aggregation service for this handler
func (a *FlowProfiler) Run() {
	go func() {
		for message := range a.SubmitChannel {
			a.Submitter.SubmitWithHeaders(message, "", "text/plain", map[string]string{
				"database":         "telegraf",
				"retention_policy": "default",
			})
		}
	}()
	go func() {
		i := 0 * time.Second
		for {
			select {
			case <-a.CloseChan:
				close(a.SubmitChannel)
				close(a.ClosedChan)
				return
			default:
				if i >= a.FlushPeriod {
					a.flush()
					i = 0 * time.Second
				}
				time.Sleep(1 * time.Second)
				i += 1 * time.Second
			}
		}
	}()
}

// Stop causes the aggregator to cease aggregating and submitting data
func (a *FlowProfiler) Stop(stopChan chan bool) {
	close(a.CloseChan)
	<-a.ClosedChan
	close(stopChan)
}

// GetName returns the name of the handler
func (a *FlowProfiler) GetName() string {
	return "Flow profiler"
}

// GetEventTypes returns a slice of event type strings that this handler
// should be applied to
func (a *FlowProfiler) GetEventTypes() []string {
	return []string{"flow"}
}