forked from netobserv/netobserv-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetobserv
More file actions
executable file
·220 lines (196 loc) · 5.14 KB
/
netobserv
File metadata and controls
executable file
·220 lines (196 loc) · 5.14 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
#!/usr/bin/env bash
source "./scripts/functions.sh"
source "./scripts/dependencies_check.sh"
set +u
# e2e skips inputs
if [ -z "${isE2E+x}" ]; then isE2E=false; fi
# keep capture state
if [ -z "${captureStarted+x}" ]; then captureStarted=false; fi
# prompt copy by default
if [ -z "${copy+x}" ]; then copy="prompt"; fi
# run foreground by default
if [ -z "${runBackground+x}" ]; then runBackground="false"; fi
# options such as filters, background etc
options=""
# namespace for this run
namespace="netobserv-cli"
if [ -n "$NETOBSERV_NAMESPACE" ]; then
echo "using custom namespace $NETOBSERV_NAMESPACE"
namespace="$NETOBSERV_NAMESPACE"
fi
# CLI image to use
img="quay.io/netobserv/network-observability-cli:main"
if [ -n "$NETOBSERV_COLLECTOR_IMAGE" ]; then
echo "using custom collector image $NETOBSERV_COLLECTOR_IMAGE"
img="$NETOBSERV_COLLECTOR_IMAGE"
fi
# version to display
version="0.0.1"
# command to run
command=""
# log level (default: info)
logLevel="info"
# max time (default: 5min)
maxTime="5m"
# max bytes (default: 50MB)
maxBytes=50000000
function flows() {
case "$2" in
"help")
flows_usage
exit 0
;;
*)
shift # remove first argument
options="$*"
# run flows command
command="flows"
;;
esac
}
function packets() {
case "$2" in
"help")
packets_usage
exit 0
;;
*)
shift # remove first argument
options="$*"
# run packets command
command="packets"
;;
esac
}
function metrics() {
case "$2" in
"help")
metrics_usage
exit 0
;;
*)
shift # remove first argument
options="$*"
# run metrics command
command="metrics"
;;
esac
}
if [[ ! "$*" =~ ^(.*)help|version(.*) ]]; then
required_yq_version="v0.0.0"
supported_archs=""
required_bash_version="v0.0.0"
check_dependencies "$required_yq_version" "$supported_archs" "$required_bash_version"
fi
case "$1" in
"help")
# display Help
echo
echo "Netobserv allows you to capture flow, packets and metrics from your cluster."
echo "Find more information at: https://github.com/netobserv/network-observability-cli/"
echo
echo "Syntax: netobserv [flows|packets|metrics|follow|stop|copy|cleanup|version] [options]"
echo
echo "commands:"
echo " flows Capture flows information in JSON format using collector pod."
echo " Options:"
flows_usage
echo " packets Capture packets information in pcap format using collector pod."
echo " Options:"
packets_usage
echo " metrics Capture metrics information in Prometheus using a ServiceMonitor (OCP cluster only)."
echo " Options:"
metrics_usage
echo " follow Follow collector logs when running in background."
echo " stop Stop collection by removing agent daemonset."
echo " copy Copy collector generated files locally."
echo " cleanup Remove netobserv components and configurations."
echo " version Print software version."
echo
exit 0
;;
"version")
# display version
echo "Netobserv CLI version $version"
exit 0
;;
"flows")
flows $*
;;
"packets")
packets $*
;;
"metrics")
metrics $*
;;
"follow")
# run follow command
follow
exit 0
;;
"stop")
# run deleteDaemonset command
deleteDaemonset
exit 0
;;
"copy")
# run copy output command
copyOutput
exit 0
;;
"cleanup")
# run cleanup command
cleanup
exit 0
;;
*)
echo "Unknown command $1. Use 'netobserv help' to display options"
exit 1
;;
esac
trap cleanup EXIT
setup $command $options
if [[ "$command" == "flows" || "$command" == "packets" ]]; then
# convert options to string
optionStr="${options//--/}"
optionStr="${optionStr// /|}"
# prepare commands & args
runCommand="sleep infinity"
execCommand="/network-observability-cli get-$command ${optionStr:+"--options" "${optionStr}"} --loglevel $logLevel --maxtime $maxTime --maxbytes $maxBytes"
if [[ "$runBackground" == "true" ]]; then
runCommand="$execCommand & $runCommand"
execCommand=""
fi
echo "Running network-observability-cli get-$command... "
${K8S_CLI_BIN} run \
-n $namespace \
collector \
--image=$img --image-pull-policy='Always' \
--overrides='{ "spec": { "serviceAccount": "netobserv-cli" } }' \
--restart='Never' \
--command -- $runCommand
${K8S_CLI_BIN} wait \
-n $namespace \
--for=condition=Ready pod/collector || exit 1
captureStarted=true
if [ -n "${execCommand}" ]; then
${K8S_CLI_BIN} exec -i --tty \
-n $namespace \
collector \
-- $execCommand
else
echo "Background capture started. Use:"
echo " - '${K8S_CLI_BIN} netobserv follow' to see the capture progress"
echo " - '${K8S_CLI_BIN} netobserv copy' to copy the generated files locally"
echo " - '${K8S_CLI_BIN} netobserv cleanup' to remove the netobserv components"
fi
elif [ "$command" = "metrics" ]; then
runBackground="true"
echo "Metrics capture started."
consoleURL="$(oc whoami --show-console)"
echo "Open ${consoleURL}/monitoring/dashboards/netobserv-cli to see generated metrics."
echo "Use 'oc netobserv stop' to stop the collection and 'oc netobserv cleanup' to remove everything."
else
echo "Unexpected exception occured on $command"
exit 1
fi