forked from netobserv/netobserv-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflow_capture.go
More file actions
388 lines (350 loc) · 9.71 KB
/
flow_capture.go
File metadata and controls
388 lines (350 loc) · 9.71 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package cmd
import (
"encoding/json"
"fmt"
"os"
"regexp"
"slices"
"sort"
"strings"
"sync"
"time"
"github.com/jpillora/sizestr"
"github.com/netobserv/flowlogs-pipeline/pkg/config"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/utils"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/write/grpc"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/write/grpc/genericmap"
"github.com/eiannone/keyboard"
"github.com/fatih/color"
"github.com/rodaine/table"
"github.com/spf13/cobra"
)
var flowCmd = &cobra.Command{
Use: "get-flows",
Short: "",
Long: "",
Run: runFlowCapture,
}
var (
flowsToShow = 35
regexes = []string{}
lastFlows = []config.GenericMap{}
)
func runFlowCapture(_ *cobra.Command, _ []string) {
go func() {
if !scanner() {
return
}
// scanner returns on exit request
os.Exit(0)
}()
captureType = "Flow"
wg := sync.WaitGroup{}
wg.Add(len(ports))
for i := range ports {
go func(idx int) {
defer wg.Done()
err := runFlowCaptureOnAddr(ports[idx], nodes[idx])
if err != nil {
// Only fatal errors are returned here
log.Fatal(err)
}
}(i)
}
wg.Wait()
}
func runFlowCaptureOnAddr(port int, filename string) error {
if len(filename) > 0 {
log.Infof("Starting Flow Capture for %s...", filename)
} else {
log.Infof("Starting Flow Capture...")
filename = strings.ReplaceAll(
currentTime().UTC().Format(time.RFC3339),
":", "") // get rid of offensive colons
}
var f *os.File
err := os.MkdirAll("./output/flow/", 0700)
if err != nil {
log.Errorf("Create directory failed: %v", err.Error())
log.Fatal(err)
}
log.Trace("Created flow folder")
f, err = os.Create("./output/flow/" + filename + ".json")
if err != nil {
log.Errorf("Create file %s failed: %v", filename, err.Error())
log.Fatal(err)
}
defer f.Close()
log.Trace("Created json file")
// Initialize sqlite DB
db := initFlowDB(filename)
log.Trace("Initialized database")
flowPackets := make(chan *genericmap.Flow, 100)
collector, err := grpc.StartCollector(port, flowPackets)
if err != nil {
return fmt.Errorf("StartCollector failed: %w", err)
}
log.Trace("Started collector")
collectorStarted = true
go func() {
<-utils.ExitChannel()
log.Trace("Ending collector")
close(flowPackets)
collector.Close()
db.Close()
log.Trace("Done")
}()
log.Trace("Ready ! Waiting for flows...")
for fp := range flowPackets {
if !captureStarted {
log.Tracef("Received first %d flows", len(flowPackets))
}
if stopReceived {
log.Trace("Stop received")
return nil
}
// parse and display flow async
go parseGenericMapAndDisplay(fp.GenericMap.Value)
// Write flows to sqlite DB
err = queryFlowDB(fp.GenericMap.Value, db)
if err != nil {
log.Error("Error while writing to DB:", err.Error())
}
if !captureStarted {
log.Trace("Wrote flows to DB")
}
// append new line between each record to read file easilly
bytes, err := f.Write(append(fp.GenericMap.Value, []byte(",\n")...))
if err != nil {
return err
}
if !captureStarted {
log.Trace("Wrote flows to json")
}
// terminate capture if max bytes reached
totalBytes += int64(bytes)
if totalBytes > maxBytes {
if exit := onLimitReached(); exit {
log.Infof("Capture reached %s, exiting now...", sizestr.ToString(maxBytes))
return nil
}
}
// terminate capture if max time reached
now := currentTime()
duration := now.Sub(startupTime)
if int(duration) > int(maxTime) {
if exit := onLimitReached(); exit {
log.Infof("Capture reached %s, exiting now...", maxTime)
return nil
}
}
captureStarted = true
}
return nil
}
func parseGenericMapAndDisplay(bytes []byte) {
genericMap := config.GenericMap{}
err := json.Unmarshal(bytes, &genericMap)
if err != nil {
log.Error("Error while parsing json", err)
return
}
if !captureStarted {
log.Tracef("Parsed genericMap %v", genericMap)
}
manageFlowsDisplay(genericMap)
}
func manageFlowsDisplay(genericMap config.GenericMap) {
// lock since we are updating lastFlows concurrently
mutex.Lock()
lastFlows = append(lastFlows, genericMap)
sort.Slice(lastFlows, func(i, j int) bool {
if captureType == "Flow" {
return toFloat64(lastFlows[i], "TimeFlowEndMs") < toFloat64(lastFlows[j], "TimeFlowEndMs")
}
return toFloat64(lastFlows[i], "Time") < toFloat64(lastFlows[j], "Time")
})
if len(regexes) > 0 {
// regexes may change during the render so we make a copy first
rCopy := make([]string, len(regexes))
copy(rCopy, regexes)
filtered := []config.GenericMap{}
for _, flow := range lastFlows {
match := true
for i := range rCopy {
ok, _ := regexp.MatchString(rCopy[i], fmt.Sprintf("%v", flow))
match = match && ok
if !match {
break
}
}
if match {
filtered = append(filtered, flow)
}
}
lastFlows = filtered
}
if len(lastFlows) > flowsToShow {
lastFlows = lastFlows[len(lastFlows)-flowsToShow:]
}
updateTable()
// unlock
mutex.Unlock()
}
func updateTable() {
// don't refresh terminal too often to avoid blinking
now := currentTime()
if !captureEnded && int(now.Sub(lastRefresh)) > int(maxRefreshRate) {
lastRefresh = now
resetTerminal()
duration := now.Sub(startupTime)
if outputBuffer == nil {
fmt.Printf("Running network-observability-cli as %s Capture\n", captureType)
fmt.Printf("Log level: %s ", logLevel)
fmt.Printf("Duration: %s ", duration.Round(time.Second))
fmt.Printf("Capture size: %s\n", sizestr.ToString(totalBytes))
if len(strings.TrimSpace(options)) > 0 {
fmt.Printf("Options: %s\n", options)
}
if strings.Contains(options, "background=true") {
fmt.Printf("Showing last: %d\n", flowsToShow)
fmt.Printf("Display: %s\n", display.getCurrentItem().name)
fmt.Printf("Enrichment: %s\n", enrichment.getCurrentItem().name)
} else {
fmt.Printf("Showing last: %d Use Up / Down keyboard arrows to increase / decrease limit\n", flowsToShow)
fmt.Printf("Display: %s Use Left / Right keyboard arrows to cycle views\n", display.getCurrentItem().name)
fmt.Printf("Enrichment: %s Use Page Up / Page Down keyboard keys to cycle enrichment scopes\n", enrichment.getCurrentItem().name)
}
}
if display.getCurrentItem().name == rawDisplay {
fmt.Print("Raw flow logs:\n")
for _, flow := range lastFlows {
fmt.Printf("%v\n", flow)
}
fmt.Printf("%s\n", strings.Repeat("-", 500))
} else {
// recreate table from scratch
headerFmt := color.New(color.BgHiBlue, color.Bold).SprintfFunc()
columnFmt := color.New(color.FgHiYellow).SprintfFunc()
// main field, always show the end time
colIDs := []string{
"EndTime",
}
// enrichment fields
if enrichment.getCurrentItem().name != noOptions {
colIDs = append(colIDs, enrichment.getCurrentItem().ids...)
} else {
// TODO: add a new flag in the config to identify these as default non enriched fields
colIDs = append(colIDs,
"SrcAddr",
"SrcPort",
"DstAddr",
"DstPort",
)
}
// standard / feature fields
if display.getCurrentItem().name != standardDisplay {
for _, col := range cfg.Columns {
if slices.Contains(display.getCurrentItem().ids, col.Feature) {
colIDs = append(colIDs, col.ID)
}
}
} else {
// TODO: add a new flag in the config to identify these as default feature fields
colIDs = append(colIDs,
"Interfaces",
"Proto",
"Dscp",
"Bytes",
"Packets",
)
}
colInterfaces := make([]interface{}, len(colIDs))
for i, id := range colIDs {
colInterfaces[i] = ToTableColName(id)
}
tbl := table.New(colInterfaces...)
if outputBuffer != nil {
tbl.WithWriter(outputBuffer)
}
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
// append most recent rows
for _, flow := range lastFlows {
tbl.AddRow(ToTableRow(flow, colIDs)...)
}
// inserting empty row ensure minimum column sizes
emptyRow := []interface{}{}
for _, id := range colIDs {
emptyRow = append(emptyRow, strings.Repeat("-", ToTableColWidth(id)))
}
tbl.AddRow(emptyRow...)
// print table
tbl.Print()
}
if len(keyboardError) > 0 {
fmt.Println(keyboardError)
} else if outputBuffer == nil {
if len(regexes) > 0 {
fmt.Printf("Live table filter: %s Press enter to match multiple regexes at once\n", regexes)
} else {
fmt.Printf("Type anything to filter incoming flows in view\n")
}
}
}
}
// scanner returns true in case of normal exit (end of program execution) or false in case of error
func scanner() bool {
if err := keyboard.Open(); err != nil {
keyboardError = fmt.Sprintf("Keyboard not supported %v", err)
return false
}
defer func() {
_ = keyboard.Close()
}()
for {
char, key, err := keyboard.GetKey()
if err != nil {
panic(err)
}
switch {
case key == keyboard.KeyCtrlC, stopReceived:
log.Info("Ctrl-C pressed, exiting program.")
// exit program
return true
case key == keyboard.KeyArrowUp:
flowsToShow++
case key == keyboard.KeyArrowDown:
if flowsToShow > 10 {
flowsToShow--
}
case key == keyboard.KeyArrowRight:
display.next()
case key == keyboard.KeyArrowLeft:
display.prev()
case key == keyboard.KeyPgup:
enrichment.next()
case key == keyboard.KeyPgdn:
enrichment.prev()
case key == keyboard.KeyBackspace || key == keyboard.KeyBackspace2:
if len(regexes) > 0 {
lastIndex := len(regexes) - 1
if len(regexes[lastIndex]) > 0 {
regexes[lastIndex] = regexes[lastIndex][:len(regexes[lastIndex])-1]
} else {
regexes = regexes[:lastIndex]
}
}
case key == keyboard.KeyEnter:
regexes = append(regexes, "")
default:
if len(regexes) == 0 {
regexes = []string{string(char)}
} else {
lastIndex := len(regexes) - 1
regexes[lastIndex] += string(char)
}
}
lastRefresh = startupTime
updateTable()
}
}