Skip to content

Commit 2a8b1da

Browse files
jpinsonneaujotak
andauthored
NETOBSERV-2253 CLI: enhance metrics safety option (#320)
* deploy collector for metrics cleanup * Update commands/netobserv Co-authored-by: Joel Takvorian <joel.takvorian@homeblocks.net> * Update commands/netobserv * Update commands/netobserv Co-authored-by: Joel Takvorian <joel.takvorian@homeblocks.net> --------- Co-authored-by: Joel Takvorian <joel.takvorian@homeblocks.net>
1 parent 673fc2c commit 2a8b1da

File tree

7 files changed

+119
-41
lines changed

7 files changed

+119
-41
lines changed

cmd/metric_capture.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"time"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var metricCmd = &cobra.Command{
12+
Use: "get-metrics",
13+
Short: "",
14+
Long: "",
15+
Run: runMetricCapture,
16+
}
17+
18+
func runMetricCapture(_ *cobra.Command, _ []string) {
19+
captureType = "Metric"
20+
21+
// TODO: implement a UI for metrics using tview
22+
// https://github.com/netobserv/network-observability-cli/pull/215
23+
24+
ticker := time.NewTicker(time.Second)
25+
for range ticker.C {
26+
// terminate capture if max time reached
27+
now := currentTime()
28+
duration := now.Sub(startupTime)
29+
if int(duration) > int(maxTime) {
30+
log.Infof("Capture reached %s, exiting now...", maxTime)
31+
32+
if allowClear {
33+
resetTerminal()
34+
}
35+
out, err := exec.Command("/oc-netobserv", "stop").Output()
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
fmt.Printf("%s", out)
40+
fmt.Print(`Thank you for using...`)
41+
printBanner()
42+
fmt.Print(`
43+
44+
- Open NetObserv / On Demand dashboard to see generated metrics
45+
46+
- Once finished, remove everything using 'oc netobserv cleanup'
47+
48+
See you soon !
49+
50+
51+
`)
52+
return
53+
}
54+
}
55+
}

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ func init() {
7474
os.Exit(0)
7575
}()
7676

77-
// IPFIX flow
77+
// flow
7878
rootCmd.AddCommand(flowCmd)
7979

8080
// packet
8181
rootCmd.AddCommand(pktCmd)
82+
83+
// metrics
84+
rootCmd.AddCommand(metricCmd)
8285
}
8386

8487
func onInit() {

commands/netobserv

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ case "$1" in
8585
;;
8686
*)
8787
shift # remove first argument
88-
options=( "$@" )
88+
options=("$@")
8989
# run flows command
9090
command="flows"
9191
;;
@@ -99,7 +99,7 @@ case "$1" in
9999
;;
100100
*)
101101
shift # remove first argument
102-
options=( "$@" )
102+
options=("$@")
103103
# run packets command
104104
command="packets"
105105
;;
@@ -113,9 +113,11 @@ case "$1" in
113113
;;
114114
*)
115115
shift # remove first argument
116-
options=( "$@" )
116+
options=("$@")
117117
# run metrics command
118118
command="metrics"
119+
# override maxTime default to 1h for metrics only
120+
maxTime="1h"
119121
;;
120122
esac
121123
;;
@@ -181,15 +183,15 @@ trap cleanup EXIT
181183

182184
setup
183185

184-
if [[ "$command" == "flows" || "$command" == "packets" ]]; then
186+
if [[ "$command" == "flows" || "$command" == "packets" || "$command" == "metrics" ]]; then
185187
# convert options to string
186188
optionStr="${options//--/}"
187189
optionStr="${optionStr// /|}"
188190

189191
# prepare commands & args
190192
runCommand="sleep infinity"
191193
execCommand="/network-observability-cli get-$command ${optionStr:+"--options" "${optionStr}"} --loglevel $logLevel --maxtime $maxTime --maxbytes $maxBytes"
192-
if [[ "$runBackground" == "true" || "$outputYAML" == "true" ]]; then
194+
if [[ "$runBackground" == "true" || "$outputYAML" == "true" || "$command" == "metrics" ]]; then
193195
runCommand="bash -c \"$execCommand && $runCommand\""
194196
execCommand=""
195197
fi
@@ -199,18 +201,29 @@ if [[ "$command" == "flows" || "$command" == "packets" ]]; then
199201
--command -- $runCommand"
200202

201203
if [[ "$outputYAML" == "true" ]]; then
202-
echo "Check the generated YAML file in output folder."
203-
echo
204-
echo "You can create $command agents by executing:"
205-
echo " ${K8S_CLI_BIN} apply -f ./output/${command}_capture_${dateName}.yml"
206-
echo
207-
echo "Then create the collector using:"
208-
echo -e " $cmd"
209-
echo
210-
echo "And follow its progression with:"
211-
echo " ${K8S_CLI_BIN} logs collector -n $namespace -f"
212-
echo
213-
exit 0
204+
if [[ "$command" == "flows" || "$command" == "packets" ]]; then
205+
echo "Check the generated YAML file in output folder."
206+
echo
207+
echo "You can create $command agents by executing:"
208+
echo " ${K8S_CLI_BIN} apply -f ./output/${command}_capture_${dateName}.yml"
209+
echo
210+
echo "Then create the collector using:"
211+
echo -e " $cmd"
212+
echo
213+
echo "And follow its progression with:"
214+
echo " ${K8S_CLI_BIN} logs collector -n $namespace -f"
215+
echo
216+
exit 0
217+
else
218+
echo "Check the generated YAML file in output folder."
219+
echo
220+
echo "You can create metrics agents by executing:"
221+
echo " ${K8S_CLI_BIN} apply -f ./output/${command}_capture_${dateName}.yml"
222+
echo
223+
echo "Then open your OCP Console and search for netobserv-cli dashboard"
224+
echo
225+
exit 0
226+
fi
214227
fi
215228

216229
echo "Running network-observability-cli get-$cmd... "
@@ -228,28 +241,22 @@ if [[ "$command" == "flows" || "$command" == "packets" ]]; then
228241
-n $namespace \
229242
collector \
230243
-- $execCommand
231-
else
244+
elif [[ "$command" == "flows" || "$command" == "packets" ]]; then
232245
echo "Background capture started. Use:"
233246
echo " - '${K8S_CLI_BIN} netobserv follow' to see the capture progress"
234247
echo " - '${K8S_CLI_BIN} netobserv copy' to copy the generated files locally"
235248
echo " - '${K8S_CLI_BIN} netobserv cleanup' to remove the netobserv components"
236-
fi
237-
elif [ "$command" = "metrics" ]; then
238-
if [[ "$outputYAML" == "true" ]]; then
239-
echo "Check the generated YAML file in output folder."
240249
echo
241-
echo "You can create metrics agents by executing:"
242-
echo " ${K8S_CLI_BIN} apply -f ./output/${command}_capture_${dateName}.yml"
243-
echo
244-
echo "Then open your OCP Console and search for netobserv-cli dashboard"
250+
echo "Capture ends automatically in $maxTime or reaching $maxBytes bytes"
251+
else
252+
runBackground="true"
253+
echo "Metrics capture started."
254+
consoleURL="$(oc whoami --show-console)"
255+
echo "Open ${consoleURL}/monitoring/dashboards/netobserv-cli to see generated metrics."
256+
echo "Use '${K8S_CLI_BIN} netobserv stop' to stop the collection and 'oc netobserv cleanup' to remove everything."
245257
echo
246-
exit 0
258+
echo "Capture ends automatically in $maxTime"
247259
fi
248-
runBackground="true"
249-
echo "Metrics capture started."
250-
consoleURL="$(oc whoami --show-console)"
251-
echo "Open ${consoleURL}/monitoring/dashboards/netobserv-cli to see generated metrics."
252-
echo "Use 'oc netobserv stop' to stop the collection and 'oc netobserv cleanup' to remove everything."
253260
else
254261
echo "Unexpected exception occured on $command"
255262
exit 1

docs/netobserv_cli.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ $ oc netobserv metrics [<option>]
168168
|--enable_udn_mapping| enable User Defined Network mapping | false
169169
|--get-subnets| get subnets information | false
170170
|--sampling| value that defines the ratio of packets being sampled | 1
171+
|--max-time| maximum capture time | 1h
171172
|--action| filter action | Accept
172173
|--cidr| filter CIDR | 0.0.0.0/0
173174
|--direction| filter direction | -

scripts/functions.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,13 @@ function check_args_and_apply() {
970970
filter=${filter/$key=$maxTime/}
971971
;;
972972
*max-bytes) # Max bytes
973-
maxBytes=$value
974-
filter=${filter/$key=$maxBytes/}
973+
if [[ "$command" == "flows" || "$command" == "packets" ]]; then
974+
maxBytes=$value
975+
filter=${filter/$key=$maxBytes/}
976+
else
977+
echo "--max-bytes is invalid option for metrics"
978+
exit 1
979+
fi
975980
;;
976981
*node-selector) # Node selector
977982
if [[ $value == *":"* ]]; then

scripts/generate-doc.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ $ oc netobserv flows [<feature_option>] [<command_options>]
6666
|===
6767
| Option | Description | Default"
6868
features_usage
69-
collector_usage
69+
flowsAndPackets_collector_usage
7070
filters_usage
7171
flowsAndMetrics_filters_usage
7272
echo -e "|==="
@@ -92,7 +92,7 @@ $ oc netobserv packets [<option>]
9292
[cols=\"1,1,1\",options=\"header\"]
9393
|===
9494
| Option | Description | Default"
95-
collector_usage
95+
flowsAndPackets_collector_usage
9696
filters_usage
9797
echo -e "|==="
9898
# packets example
@@ -117,6 +117,7 @@ $ oc netobserv metrics [<option>]
117117
|===
118118
| Option | Description | Default"
119119
features_usage
120+
metrics_collector_usage
120121
filters_usage
121122
metrics_options
122123
flowsAndMetrics_filters_usage

scripts/help.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,20 @@ function features_usage {
7878
echo " --sampling: value that defines the ratio of packets being sampled (default: 1)"
7979
}
8080

81-
# collector options
82-
function collector_usage {
81+
# flow and packets collector options
82+
function flowsAndPackets_collector_usage {
8383
echo " --background: run in background (default: false)"
8484
echo " --copy: copy the output files locally (default: prompt)"
8585
echo " --log-level: components logs (default: info)"
8686
echo " --max-time: maximum capture time (default: 5m)"
8787
echo " --max-bytes: maximum capture bytes (default: 50000000 = 50MB)"
8888
}
8989

90+
# fmetrics collector options
91+
function metrics_collector_usage {
92+
echo " --max-time: maximum capture time (default: 1h)"
93+
}
94+
9095
# script options
9196
function script_usage {
9297
echo " --yaml: generate YAML without applying it (default: false)"
@@ -143,7 +148,7 @@ function flows_usage {
143148
flowsAndMetrics_filters_usage
144149
echo
145150
echo "options:"
146-
collector_usage
151+
flowsAndPackets_collector_usage
147152
script_usage
148153
}
149154

@@ -158,7 +163,7 @@ function packets_usage {
158163
filters_usage
159164
echo
160165
echo "options:"
161-
collector_usage
166+
flowsAndPackets_collector_usage
162167
script_usage
163168
}
164169

@@ -176,6 +181,7 @@ function metrics_usage {
176181
filters_usage
177182
flowsAndMetrics_filters_usage
178183
echo "options:"
184+
metrics_collector_usage
179185
script_usage
180186
metrics_options
181187
echo

0 commit comments

Comments
 (0)