Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ func hearbeat() {
}
}

func backgroundHearbeat() {
for {
if captureEnded {
return
}

if capture != Metric {
updateTableAndSuggestions()
// simply print flow into logs
rows := getTableRows()
for _, row := range rows {
log.Println(row)
}
}

time.Sleep(5 * time.Second)
}
}

func pause(pause bool) {
paused = pause
playPauseButton.SetLabel(getPlayPauseText())
Expand Down
10 changes: 8 additions & 2 deletions cmd/flow_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ var flowCmd = &cobra.Command{

func runFlowCapture(_ *cobra.Command, _ []string) {
capture = Flow
go startFlowCollector()
createFlowDisplay()
showCount = defaultFlowShowCount
if isBackground {
go backgroundHearbeat() // show table periodically in background
startFlowCollector()
} else {
go startFlowCollector()
createFlowDisplay()
}
}

func startFlowCollector() {
Expand Down
59 changes: 35 additions & 24 deletions cmd/flow_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ var (

func createFlowDisplay() {
focus = "inputField"
showCount = defaultFlowShowCount
app = tview.NewApplication().
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
//nolint:exhaustive
Expand Down Expand Up @@ -460,33 +459,28 @@ func AppendFlow(genericMap config.GenericMap) {
return
}

if errAdvancedDisplay != nil {
// simply print flow into logs
log.Printf("%v\n", genericMap)
} else {
// lock since we are updating lastFlows concurrently
mutex.Lock()

// add new flow to the array
genericMap["Index"] = flowIndex
flowIndex++
lastFlows = append(lastFlows, genericMap)

// sort flows according to time
sort.Slice(lastFlows, func(i, j int) bool {
if capture == Flow {
return toFloat64(lastFlows[i], "TimeFlowEndMs") < toFloat64(lastFlows[j], "TimeFlowEndMs")
}
return toFloat64(lastFlows[i], "Time") < toFloat64(lastFlows[j], "Time")
})
// lock since we are updating lastFlows concurrently
mutex.Lock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be safer / best practice to have defer Unlock here ?

   defer mutex.Unlock()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can address that in parallel if it's ok for you

That code was already there and not related to that fix


// limit flows kept in memory
if len(lastFlows) > keepCount {
lastFlows = lastFlows[len(lastFlows)-keepCount:]
// add new flow to the array
genericMap["Index"] = flowIndex
flowIndex++
lastFlows = append(lastFlows, genericMap)

// sort flows according to time
sort.Slice(lastFlows, func(i, j int) bool {
if capture == Flow {
return toFloat64(lastFlows[i], "TimeFlowEndMs") < toFloat64(lastFlows[j], "TimeFlowEndMs")
}
return toFloat64(lastFlows[i], "Time") < toFloat64(lastFlows[j], "Time")
})

mutex.Unlock()
// limit flows kept in memory
if len(lastFlows) > keepCount {
lastFlows = lastFlows[len(lastFlows)-keepCount:]
}

mutex.Unlock()
}

func updateDisplayEnrichmentTexts() {
Expand Down Expand Up @@ -599,6 +593,23 @@ func getFlows() []config.GenericMap {
return flows
}

func getTableRows() []string {
arr := []string{}
if len(tableData.cols) == 0 || len(tableData.flows) == 0 {
return arr
}

for i := range len(tableData.flows) + 1 {
str := ""
for j := range tableData.cols {
str += tableData.GetCell(i, j).Text
}
arr = append(arr, str)
}

return arr
}

func updateTableAndSuggestions() {
// update tableData
tableData.cols = getCols()
Expand Down
27 changes: 22 additions & 5 deletions cmd/metric_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ var (

func runMetricCapture(c *cobra.Command, _ []string) {
capture = Metric
go startMetricCollector(c.Context())
createMetricDisplay()

updateGraphs(false) // initial update of graphs to have something to display
if isBackground {
startMetricCollector(c.Context())
} else {
go startMetricCollector(c.Context())
createMetricDisplay()
}
}

func startMetricCollector(ctx context.Context) {
Expand Down Expand Up @@ -80,15 +86,26 @@ func startMetricCollector(ctx context.Context) {

func queryGraphs(ctx context.Context, client api.Client) {
for index := range graphs {
go queryGraph(ctx, client, index)
if isBackground {
queryGraph(ctx, client, index) // keep logical order for background mode
} else {
go queryGraph(ctx, client, index)
}
}
}

func queryGraph(ctx context.Context, client api.Client, index int) {
query, result := queryProm(ctx, client, graphs[index].Query.PromQL)
if errAdvancedDisplay != nil {
if app == nil || errAdvancedDisplay != nil {
// simply print metrics into logs
log.Printf("%v\n", result)
log.Print(query.PromQL)
if result == nil || len(*result) == 0 {
log.Print(" No result")
} else {
for _, stream := range *result {
log.Printf(" %s", stream.String())
}
}
} else {
appendMetrics(query, result, index)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/metric_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ var (

func createMetricDisplay() {
updateShowMetricCount()
updateGraphs(false)

app = tview.NewApplication().
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
Expand Down
9 changes: 7 additions & 2 deletions cmd/packet_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ var (

func runPacketCapture(_ *cobra.Command, _ []string) {
capture = Packet
go startPacketCollector()
createFlowDisplay()
if isBackground {
go backgroundHearbeat() // show table periodically in background
startPacketCollector()
} else {
go startPacketCollector()
createFlowDisplay()
}
}

//nolint:cyclop
Expand Down
8 changes: 6 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
captureEnded = false
stopReceived = false
useMocks = false
isBackground = false
)

// Execute executes the root command.
Expand Down Expand Up @@ -102,7 +103,10 @@ func onInit() {
printBanner()

log.Infof("Log level: %s\nOption(s): %s", logLevel, options)

if strings.Contains(options, "background") && !strings.Contains(options, "background=false") {
isBackground = true
log.Infof("Running in background mode")
}
showKernelVersion()

if useMocks {
Expand Down Expand Up @@ -143,7 +147,7 @@ func onLimitReached() bool {
if app != nil && errAdvancedDisplay == nil {
app.Stop()
}
if strings.Contains(options, "background=true") {
if isBackground {
out, err := exec.Command("/oc-netobserv", "stop").Output()
if err != nil {
log.Fatal(err)
Expand Down
17 changes: 0 additions & 17 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,6 @@ func setup(t *testing.T) {
assert.Equal(t, nil, err)
}

func getTableRows() []string {
arr := []string{}
if len(tableData.cols) == 0 || len(tableData.flows) == 0 {
return arr
}

for i := range len(tableData.flows) + 1 {
str := ""
for j := range tableData.cols {
str += tableData.GetCell(i, j).Text
}
arr = append(arr, str)
}

return arr
}

func resetTime() {
// set timezone to Paris time for all tests
loc, err := time.LoadLocation("Europe/Paris")
Expand Down
4 changes: 4 additions & 0 deletions docs/netobserv_cli.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ $ oc netobserv flows [<feature_option>] [<command_options>]
|--enable_rtt| enable RTT tracking | false
|--enable_udn_mapping| enable User Defined Network mapping | false
|--get-subnets| get subnets information | false
|--privileged| force eBPF agent privileged mode | auto
|--sampling| packets sampling interval | 1
|--background| run in background | false
|--copy| copy the output files locally | prompt
Expand Down Expand Up @@ -167,7 +168,10 @@ $ oc netobserv metrics [<option>]
|--enable_rtt| enable RTT tracking | false
|--enable_udn_mapping| enable User Defined Network mapping | false
|--get-subnets| get subnets information | false
|--privileged| force eBPF agent privileged mode | auto
|--sampling| packets sampling interval | 1
|--background| run in background | false
|--log-level| components logs | info
|--max-time| maximum capture time | 1h
|--action| filter action | Accept
|--cidr| filter CIDR | 0.0.0.0/0
Expand Down
7 changes: 7 additions & 0 deletions scripts/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function help {
echo " --drops # including drops"
echo " --include_list=node,namespace # for all metrics matching 'node' or 'namespace' keywords"
echo
echo " Capture metrics in background"
echo " netobserv metrics --background \ # Capture metrics using background mode"
echo " --max-time=24h # for a maximum of 24 hours"
echo " Then open the URL provided by the command to visualize the netobserv-cli dashboard anytime during or after the run."
echo
}

# display version
Expand Down Expand Up @@ -90,6 +95,8 @@ function flowsAndPackets_collector_usage {

# fmetrics collector options
function metrics_collector_usage {
echo " --background: run in background (default: false)"
echo " --log-level: components logs (default: info)"
echo " --max-time: maximum capture time (default: 1h)"
}

Expand Down