-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpcap-pod.sh
executable file
·193 lines (138 loc) · 4.24 KB
/
pcap-pod.sh
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
#!/usr/bin/env bash
set -u -e -o pipefail
declare -r SCRIPT_PATH=$(readlink -f "$0")
declare -r SCRIPT_DIR=$(cd $(dirname "$SCRIPT_PATH") && pwd)
declare -r DNS_PCAP_NS=dns-pcap
declare -r LOCAL_PCAP_DIR="dns-pcaps/$(date +%s)"
declare -a TCPDUMP_PIDS=()
declare -a DEBUG_PODS=()
declare -a NODE_NAMES=()
print_with_timestamp() {
local level=$1; shift
echo $(date -u +"%Y-%m-%dT%H:%M:%S%Z [%s]") "$level: $@"
}
log() {
print_with_timestamp INFO "$@"
}
err() {
print_with_timestamp ERROR "$@" >&2
}
sub_header() {
echo -e "\n* $@"
echo "-------------------------------------------------------"
}
CLEANUP_IN_PROGRESS=false
cleanup() {
sub_header "Gathering all pcaps and cleaning up ..."
$CLEANUP_IN_PROGRESS && return 0
CLEANUP_IN_PROGRESS=true
log "Shutting down tcpdump and copying pcaps"
log "Please wait until all the pcaps are copied to $LOCAL_PCAP_DIR"
### DO NOT EXIT on ERRORS
#set +e
stop_listening
sleep 3 ### requires time to flush
cp_pcaps
delete_debug_pods
sub_header "All pcaps have been copied to $LOCAL_PCAP_DIR"
echo -e "\n====================xxxXxxx=========================="
}
stop_listening() {
sub_header "Stopping tcpdump on all pods"
for pod in ${DEBUG_PODS[@]}; do
oc exec -t -n $DNS_PCAP_NS $pod -- pkill tcpdump &
done
wait
for pid in ${TCPDUMP_PIDS[@]}; do
log "sending kill to $pid"
{kill -TERM $pid || true} &
done
wait
}
cp_pcaps() {
sub_header "Copying all pcaps to $LOCAL_PCAP_DIR"
mkdir -p "$LOCAL_PCAP_DIR"
local idx=0
for pod in ${DEBUG_PODS[@]}; do
local pcap_file="${NODE_NAMES[$idx]}.pcap"
log "$pod -> $pcap_file"
#set -x
oc cp -n $DNS_PCAP_NS $pod:tmp/$pcap_file "$LOCAL_PCAP_DIR/$pcap_file"
(( idx++ )) || true ### something is wrong; why does this error ?
done
}
delete_debug_pods() {
sub_header "Deleting all debug pods "
for pod in ${DEBUG_PODS[@]}; do
log "Deleting pod $pod"
oc delete -n $DNS_PCAP_NS pod $pod &
done
wait
}
start_debug_node() {
local node=$1; shift
local node_name=$(basename $node)
log "Starting a debug pod for node: $node_name"
local pod_manifest=$( oc debug $node --to-namespace="$DNS_PCAP_NS" -o json -- sleep infinity )
local pod_name=$(echo $pod_manifest | jq -r '.metadata.name')
log "Starting pod $pod_name for $node"
DEBUG_PODS+=( $pod_name )
NODE_NAMES+=( $node_name ) ### maintain same index
echo $pod_manifest | oc apply -f-
oc wait --for=condition=Ready -n $DNS_PCAP_NS "pod/$pod_name"
return 0
}
tcpdump_node() {
local node_name=$1; shift
local pod_name=$1; shift
local debug_script
debug_script=$(cat <<-EOF
pod_id=\$(chroot /host crictl ps -o json | jq -r '.containers[] | select(.labels["io.kubernetes.container.name"] == "dns") | .id')
pod_pid=\$(chroot /host crictl inspect --output json \$pod_id | jq '.info.pid')
nsenter -n -t \$pod_pid -- tcpdump -i any -nn -w /tmp/$node_name.pcap
EOF
)
log "starting tcpdump on pod: $pod_name"
oc exec -t -n $DNS_PCAP_NS $pod_name -- bash -c "$debug_script" &
TCPDUMP_PIDS+=( $! )
return 0
}
start_tcpdump_all_nodes() {
sub_header "Starting tcpdump on all debug pods ... "
local idx=0
for pod in ${DEBUG_PODS[@]}; do
local node=${NODE_NAMES[$idx]}
tcpdump_node $node $pod
(( idx++ )) || true ### something is wrong; why does this error ?
done
}
start_debug_pods() {
sub_header "Starting Debug Pods on all nodes ... "
local nodes=($(oc get nodes -o name))
for n in ${nodes[@]}; do
start_debug_node "$n"
done
}
main() {
## todo: usage
echo Starting packet capture of all dns pods
echo All pcaps will be copied to $LOCAL_PCAP_DIR
echo ==========================================================
### ensure the ns is created before proceeding
oc get ns $DNS_PCAP_NS -o name >/dev/null || oc create ns $DNS_PCAP_NS || {
err "Failed to create $DNS_PCAP_NS namespace"
return 1
}
### cleanup only if the ns can be created
trap cleanup EXIT
trap "exit 0" SIGINT
start_debug_pods
start_tcpdump_all_nodes
sleep 3
echo ==========================================================
log Capturing of all DNS pods have started. Press Ctrl+C to stop
log All pcap files will be copied to $LOCAL_PCAP_DIR
sleep infinity
return $?
}
main "$@"