Skip to content

Commit 57d04b5

Browse files
committed
fix background mode and update doc
1 parent 982c0e3 commit 57d04b5

File tree

8 files changed

+52
-13
lines changed

8 files changed

+52
-13
lines changed

cmd/flow_capture.go

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

2525
func runFlowCapture(_ *cobra.Command, _ []string) {
2626
capture = Flow
27-
go startFlowCollector()
28-
createFlowDisplay()
27+
if isBackground {
28+
startFlowCollector()
29+
} else {
30+
go startFlowCollector()
31+
createFlowDisplay()
32+
}
2933
}
3034

3135
func startFlowCollector() {

cmd/flow_display.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ func AppendFlow(genericMap config.GenericMap) {
460460
return
461461
}
462462

463-
if errAdvancedDisplay != nil {
463+
if app == nil || errAdvancedDisplay != nil {
464464
// simply print flow into logs
465465
log.Printf("%v\n", genericMap)
466466
} else {

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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ var (
3535

3636
func runPacketCapture(_ *cobra.Command, _ []string) {
3737
capture = Packet
38-
go startPacketCollector()
39-
createFlowDisplay()
38+
if isBackground {
39+
startPacketCollector()
40+
} else {
41+
go startPacketCollector()
42+
createFlowDisplay()
43+
}
4044
}
4145

4246
//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)

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)