Skip to content

Commit e3b545b

Browse files
authored
fix background mode and update doc (#395)
1 parent 982c0e3 commit e3b545b

File tree

10 files changed

+108
-53
lines changed

10 files changed

+108
-53
lines changed

cmd/display.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ func hearbeat() {
179179
}
180180
}
181181

182+
func backgroundHearbeat() {
183+
for {
184+
if captureEnded {
185+
return
186+
}
187+
188+
if capture != Metric {
189+
updateTableAndSuggestions()
190+
// simply print flow into logs
191+
rows := getTableRows()
192+
for _, row := range rows {
193+
log.Println(row)
194+
}
195+
}
196+
197+
time.Sleep(5 * time.Second)
198+
}
199+
}
200+
182201
func pause(pause bool) {
183202
paused = pause
184203
playPauseButton.SetLabel(getPlayPauseText())

cmd/flow_capture.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ var flowCmd = &cobra.Command{
2424

2525
func runFlowCapture(_ *cobra.Command, _ []string) {
2626
capture = Flow
27-
go startFlowCollector()
28-
createFlowDisplay()
27+
showCount = defaultFlowShowCount
28+
if isBackground {
29+
go backgroundHearbeat() // show table periodically in background
30+
startFlowCollector()
31+
} else {
32+
go startFlowCollector()
33+
createFlowDisplay()
34+
}
2935
}
3036

3137
func startFlowCollector() {

cmd/flow_display.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ var (
5656

5757
func createFlowDisplay() {
5858
focus = "inputField"
59-
showCount = defaultFlowShowCount
6059
app = tview.NewApplication().
6160
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
6261
//nolint:exhaustive
@@ -460,33 +459,28 @@ func AppendFlow(genericMap config.GenericMap) {
460459
return
461460
}
462461

463-
if errAdvancedDisplay != nil {
464-
// simply print flow into logs
465-
log.Printf("%v\n", genericMap)
466-
} else {
467-
// lock since we are updating lastFlows concurrently
468-
mutex.Lock()
469-
470-
// add new flow to the array
471-
genericMap["Index"] = flowIndex
472-
flowIndex++
473-
lastFlows = append(lastFlows, genericMap)
474-
475-
// sort flows according to time
476-
sort.Slice(lastFlows, func(i, j int) bool {
477-
if capture == Flow {
478-
return toFloat64(lastFlows[i], "TimeFlowEndMs") < toFloat64(lastFlows[j], "TimeFlowEndMs")
479-
}
480-
return toFloat64(lastFlows[i], "Time") < toFloat64(lastFlows[j], "Time")
481-
})
462+
// lock since we are updating lastFlows concurrently
463+
mutex.Lock()
482464

483-
// limit flows kept in memory
484-
if len(lastFlows) > keepCount {
485-
lastFlows = lastFlows[len(lastFlows)-keepCount:]
465+
// add new flow to the array
466+
genericMap["Index"] = flowIndex
467+
flowIndex++
468+
lastFlows = append(lastFlows, genericMap)
469+
470+
// sort flows according to time
471+
sort.Slice(lastFlows, func(i, j int) bool {
472+
if capture == Flow {
473+
return toFloat64(lastFlows[i], "TimeFlowEndMs") < toFloat64(lastFlows[j], "TimeFlowEndMs")
486474
}
475+
return toFloat64(lastFlows[i], "Time") < toFloat64(lastFlows[j], "Time")
476+
})
487477

488-
mutex.Unlock()
478+
// limit flows kept in memory
479+
if len(lastFlows) > keepCount {
480+
lastFlows = lastFlows[len(lastFlows)-keepCount:]
489481
}
482+
483+
mutex.Unlock()
490484
}
491485

492486
func updateDisplayEnrichmentTexts() {
@@ -599,6 +593,23 @@ func getFlows() []config.GenericMap {
599593
return flows
600594
}
601595

596+
func getTableRows() []string {
597+
arr := []string{}
598+
if len(tableData.cols) == 0 || len(tableData.flows) == 0 {
599+
return arr
600+
}
601+
602+
for i := range len(tableData.flows) + 1 {
603+
str := ""
604+
for j := range tableData.cols {
605+
str += tableData.GetCell(i, j).Text
606+
}
607+
arr = append(arr, str)
608+
}
609+
610+
return arr
611+
}
612+
602613
func updateTableAndSuggestions() {
603614
// update tableData
604615
tableData.cols = getCols()

cmd/metric_capture.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ var (
3131

3232
func runMetricCapture(c *cobra.Command, _ []string) {
3333
capture = Metric
34-
go startMetricCollector(c.Context())
35-
createMetricDisplay()
34+
35+
updateGraphs(false) // initial update of graphs to have something to display
36+
if isBackground {
37+
startMetricCollector(c.Context())
38+
} else {
39+
go startMetricCollector(c.Context())
40+
createMetricDisplay()
41+
}
3642
}
3743

3844
func startMetricCollector(ctx context.Context) {
@@ -80,15 +86,26 @@ func startMetricCollector(ctx context.Context) {
8086

8187
func queryGraphs(ctx context.Context, client api.Client) {
8288
for index := range graphs {
83-
go queryGraph(ctx, client, index)
89+
if isBackground {
90+
queryGraph(ctx, client, index) // keep logical order for background mode
91+
} else {
92+
go queryGraph(ctx, client, index)
93+
}
8494
}
8595
}
8696

8797
func queryGraph(ctx context.Context, client api.Client, index int) {
8898
query, result := queryProm(ctx, client, graphs[index].Query.PromQL)
89-
if errAdvancedDisplay != nil {
99+
if app == nil || errAdvancedDisplay != nil {
90100
// simply print metrics into logs
91-
log.Printf("%v\n", result)
101+
log.Print(query.PromQL)
102+
if result == nil || len(*result) == 0 {
103+
log.Print(" No result")
104+
} else {
105+
for _, stream := range *result {
106+
log.Printf(" %s", stream.String())
107+
}
108+
}
92109
} else {
93110
appendMetrics(query, result, index)
94111
}

cmd/metric_display.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ var (
6666

6767
func createMetricDisplay() {
6868
updateShowMetricCount()
69-
updateGraphs(false)
7069

7170
app = tview.NewApplication().
7271
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {

cmd/packet_capture.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ var (
3535

3636
func runPacketCapture(_ *cobra.Command, _ []string) {
3737
capture = Packet
38-
go startPacketCollector()
39-
createFlowDisplay()
38+
if isBackground {
39+
go backgroundHearbeat() // show table periodically in background
40+
startPacketCollector()
41+
} else {
42+
go startPacketCollector()
43+
createFlowDisplay()
44+
}
4045
}
4146

4247
//nolint:cyclop

cmd/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var (
5252
captureEnded = false
5353
stopReceived = false
5454
useMocks = false
55+
isBackground = false
5556
)
5657

5758
// Execute executes the root command.
@@ -102,7 +103,10 @@ func onInit() {
102103
printBanner()
103104

104105
log.Infof("Log level: %s\nOption(s): %s", logLevel, options)
105-
106+
if strings.Contains(options, "background") && !strings.Contains(options, "background=false") {
107+
isBackground = true
108+
log.Infof("Running in background mode")
109+
}
106110
showKernelVersion()
107111

108112
if useMocks {
@@ -143,7 +147,7 @@ func onLimitReached() bool {
143147
if app != nil && errAdvancedDisplay == nil {
144148
app.Stop()
145149
}
146-
if strings.Contains(options, "background=true") {
150+
if isBackground {
147151
out, err := exec.Command("/oc-netobserv", "stop").Output()
148152
if err != nil {
149153
log.Fatal(err)

cmd/root_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,6 @@ func setup(t *testing.T) {
9494
assert.Equal(t, nil, err)
9595
}
9696

97-
func getTableRows() []string {
98-
arr := []string{}
99-
if len(tableData.cols) == 0 || len(tableData.flows) == 0 {
100-
return arr
101-
}
102-
103-
for i := range len(tableData.flows) + 1 {
104-
str := ""
105-
for j := range tableData.cols {
106-
str += tableData.GetCell(i, j).Text
107-
}
108-
arr = append(arr, str)
109-
}
110-
111-
return arr
112-
}
113-
11497
func resetTime() {
11598
// set timezone to Paris time for all tests
11699
loc, err := time.LoadLocation("Europe/Paris")

docs/netobserv_cli.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ $ oc netobserv flows [<feature_option>] [<command_options>]
6464
|--enable_rtt| enable RTT tracking | false
6565
|--enable_udn_mapping| enable User Defined Network mapping | false
6666
|--get-subnets| get subnets information | false
67+
|--privileged| force eBPF agent privileged mode | auto
6768
|--sampling| packets sampling interval | 1
6869
|--background| run in background | false
6970
|--copy| copy the output files locally | prompt
@@ -167,7 +168,10 @@ $ oc netobserv metrics [<option>]
167168
|--enable_rtt| enable RTT tracking | false
168169
|--enable_udn_mapping| enable User Defined Network mapping | false
169170
|--get-subnets| get subnets information | false
171+
|--privileged| force eBPF agent privileged mode | auto
170172
|--sampling| packets sampling interval | 1
173+
|--background| run in background | false
174+
|--log-level| components logs | info
171175
|--max-time| maximum capture time | 1h
172176
|--action| filter action | Accept
173177
|--cidr| filter CIDR | 0.0.0.0/0

scripts/help.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ function help {
5757
echo " --drops # including drops"
5858
echo " --include_list=node,namespace # for all metrics matching 'node' or 'namespace' keywords"
5959
echo
60+
echo " Capture metrics in background"
61+
echo " netobserv metrics --background \ # Capture metrics using background mode"
62+
echo " --max-time=24h # for a maximum of 24 hours"
63+
echo " Then open the URL provided by the command to visualize the netobserv-cli dashboard anytime during or after the run."
64+
echo
6065
}
6166

6267
# display version
@@ -90,6 +95,8 @@ function flowsAndPackets_collector_usage {
9095

9196
# fmetrics collector options
9297
function metrics_collector_usage {
98+
echo " --background: run in background (default: false)"
99+
echo " --log-level: components logs (default: info)"
93100
echo " --max-time: maximum capture time (default: 1h)"
94101
}
95102

0 commit comments

Comments
 (0)