forked from ibm-apiconnect/v10-postmortem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_postmortem.sh
1374 lines (1175 loc) · 60.9 KB
/
generate_postmortem.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Purpose: Collects API Connect logs and packages into an archive.
#
# Authors: Charles Johnson, [email protected]
# Franck Delporte, [email protected]
# Nagarjun Nama Balaji, [email protected]
# Kenny Nguyen, [email protected]
#
#parse passed arguments
for switch in $@; do
case $switch in
*"-h"*|*"--help"*)
echo -e 'Usage: generate_postmortem.sh {optional: LOG LIMIT}'
echo -e ""
echo -e "Available switches:"
echo -e "--specific-namespaces: Target only the listed namespaces for the data collection."
echo -e "--extra-namespaces: Extra namespaces separated with commas. Example: --extra-namespaces=dev1,dev2,dev3"
echo -e "--log-limit: Set the number of lines to collect from each pod logs."
echo -e "--ova: Only set if running inside an OVA deployment."
echo -e "--no-prompt: Do not prompt to report auto-detected namespaces."
echo -e "--pull-appliance-logs: Call [apic logs] command then package into archive file."
echo -e "--pull-portal-backup: Pulls specified backup from portal cluster."
echo -e "--performance-check: Set to run performance checks."
echo -e "--no-history: Do not collect user history."
echo -e "--collect-secrets: Collect secrets from targeted namespaces. Due to sensitivity of data, do not use unless requested by support."
echo -e "--collect-crunchy: Collect Crunchy mustgather."
echo -e ""
echo -e "--diagnostic-all: Set to enable all diagnostic data."
echo -e "--diagnostic-manager: Set to include additional manager specific data."
echo -e "--diagnostic-gateway: Set to include additional gateway specific data."
echo -e "--diagnostic-portal: Set to include additional portal specific data."
echo -e "--diagnostic-analytics: Set to include additional portal specific data."
echo -e ""
echo -e "--debug: Set to enable verbose logging."
echo -e ""
exit 0
;;
*"--debug"*)
set -x
DEBUG_SET=1
;;
*"--ova"*)
IS_OVA=1
NO_PROMPT=1
NAMESPACE_LIST="kube-system default"
;;
*"--diagnostic-all"*)
DIAG_MANAGER=1
DIAG_GATEWAY=1
DIAG_PORTAL=1
DIAG_ANALYTICS=1
;;
*"--diagnostic-manager"*)
DIAG_MANAGER=1
;;
*"--diagnostic-gateway"*)
DIAG_GATEWAY=1
;;
*"--diagnostic-portal"*)
DIAG_PORTAL=1
;;
*"--diagnostic-analytics"*)
DIAG_ANALYTICS=1
;;
*"--log-limit"*)
limit=`echo "${switch}" | cut -d'=' -f2`
if [[ "$limit" =~ ^[0-9]+$ ]]; then
LOG_LIMIT="--tail=${limit}"
fi
;;
*"--specific-namespaces"*)
NO_PROMPT=1
SPECIFIC_NAMESPACES=1
specific_namespaces=`echo "${switch}" | cut -d'=' -f2 | tr ',' ' '`
NAMESPACE_LIST="${specific_namespaces}"
;;
*"--extra-namespaces"*)
NO_PROMPT=1
extra_namespaces=`echo "${switch}" | cut -d'=' -f2 | tr ',' ' '`
NAMESPACE_LIST="kube-system ${extra_namespaces}"
;;
*"--pull-appliance-logs"*)
PULL_APPLIANCE_LOGS=1
;;
*"--performance-check"*)
PERFORMANCE_CHECK=1
;;
*"--no-history"*)
NO_HISTORY=1
;;
*"--no-prompt"*)
NO_PROMPT=1
;;
*"--collect-secrets"*)
COLLECT_SECRETS=1
;;
*"--collect-crunchy"*)
COLLECT_CRUNCHY=1
SCRIPT_LOCATION="`pwd`/crunchy_gather.py"
if [[ ! -f $SCRIPT_LOCATION ]]; then
echo -e "Unable to locate script [crunchy_gather.py] in current directory. Download from GitHub repository. Exiting..."
exit 1
fi
chmod +x $SCRIPT_LOCATION
;;
*)
if [[ -z "$DEBUG_SET" ]]; then
set +e
fi
;;
esac
done
if [[ -z "$LOG_LIMIT" ]]; then
LOG_LIMIT=""
fi
if [[ -z "$SPECIFIC_NAMESPACES" ]]; then
SPECIFIC_NAMESPACES=0
fi
#====================================== Confirm pre-reqs and init variables ======================================
#------------------------------- Make sure all necessary commands exists ------------------------------
which kubectl &> /dev/null
if [[ $? -ne 0 ]]; then
echo "Unable to locate the command [kubectl] in the path. Either install or add it to the path. EXITING..."
exit 1
fi
ARCHIVE_UTILITY=`which zip 2>/dev/null`
if [[ $? -ne 0 ]]; then
ARCHIVE_UTILITY=`which tar 2>/dev/null`
if [[ $? -ne 0 ]]; then
echo "Unable to locate either command [tar] / [zip] in the path. Either install or add it to the path. EXITING..."
exit 1
fi
fi
#------------------------------------------------------------------------------------------------------
#------------------------------------------ custom functions ------------------------------------------
#compare versions
function version_gte() { test "$1" == "$2" || test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
#XML to generate error report
function generateXmlForErrorReport()
{
cat << EOF > $1
<?xml version="1.0" encoding="UTF-8"?>
<!-- ErrorReport Request -->
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<dp:request xmlns:dp="http://www.datapower.com/schemas/management" domain="default">
<dp:do-action>
<ErrorReport/>
</dp:do-action>
</dp:request>
</env:Body>
</env:Envelope>
EOF
}
#------------------------------------------------------------------------------------------------------
#------------------------------------------- Set variables --------------------------------------------
LOG_PATH="/tmp"
CURRENT_PATH=`pwd`
TIMESTAMP=`date +%Y%m%dT%H%M%S%Z`
TEMP_NAME="postmortem-$TIMESTAMP"
TEMP_PATH="${LOG_PATH}/${TEMP_NAME}"
if [[ -z "$NAMESPACE_LIST" ]]; then
NAMESPACE_LIST="kube-system"
fi
if [[ -z "$AUTO_DETECT" ]]; then
AUTO_DETECT=1
fi
ARCHIVE_FILE=""
ERROR_REPORT_SLEEP_TIMEOUT=30
MIN_DOCKER_VERSION="17.03"
MIN_KUBELET_VERSION="1.17"
COLOR_YELLOW=`tput setaf 3`
COLOR_WHITE=`tput setaf 7`
COLOR_RESET=`tput sgr0`
#------------------------------------------------------------------------------------------------------
#------------------------------------------- Clean up area --------------------------------------------
function cleanup {
echo "Cleaning up. Removing directory [$TEMP_PATH]."
rm -fr $TEMP_PATH
}
trap cleanup EXIT
#------------------------------------------------------------------------------------------------------
#=================================================================================================================
echo -e "Generating postmortem, please wait..."
mkdir -p $TEMP_PATH
#determine if metrics is installed
kubectl get pods --all-namespaces 2>/dev/null | egrep -q "metrics-server|openshift-monitoring"
OUTPUT_METRICS=$?
kubectl get ns 2>/dev/null | grep -q "rook-ceph"
if [[ $? -eq 0 && $SPECIFIC_NAMESPACES -ne 1 ]]; then
NAMESPACE_LIST+=" rook-ceph"
fi
kubectl get ns 2>/dev/null | grep -q "rook-ceph-system"
if [[ $? -eq 0 && $SPECIFIC_NAMESPACES -ne 1 ]]; then
NAMESPACE_LIST+=" rook-ceph-system"
fi
#================================================= pull ova data =================================================
if [[ $IS_OVA -eq 1 ]]; then
OVA_DATA="${TEMP_PATH}/ova"
mkdir -p $OVA_DATA
#grab version
sudo apic version 1>"${OVA_DATA}/version.out" 2>/dev/null
#grab status
sudo apic status 1>"${OVA_DATA}/status.out" 2>/dev/null
#grab bash history
if [[ $NO_HISTORY -ne 1 ]]; then
cp "/home/apicadm/.bash_history" "${OVA_DATA}/apicadm-bash_history.out" &>/dev/null
cp "/root/.bash_history" "${OVA_DATA}/root-bash_history.out" &>/dev/null
fi
if [[ $PULL_APPLIANCE_LOGS -eq 1 ]]; then
cd $OVA_DATA
sudo apic logs &>/dev/null
fi
fi
#=================================================================================================================
#============================================== autodetect namespaces ============================================
if [[ $AUTO_DETECT -eq 1 ]]; then
NS_LISTING=`kubectl get ns 2>/dev/null | sed -e '1d' | egrep -v "kube-system|cert-manager|rook"`
SUBSYS_MANAGER="ISNOTSET"
SUBSYS_ANALYTICS="ISNOTSET"
SUBSYS_PORTAL="ISNOTSET"
SUBSYS_GATEWAY_V5="ISNOTSET"
SUBSYS_GATEWAY_V6="ISNOTSET"
SUBSYS_MANAGER_COUNT=0
SUBSYS_ANALYTICS_COUNT=0
SUBSYS_PORTAL_COUNT=0
SUBSYS_GATEWAY_V5_COUNT=0
SUBSYS_GATEWAY_V6_COUNT=0
CLUSTER_LIST=(ManagementCluster AnalyticsCluster PortalCluster GatewayCluster)
ns_matches=""
while read line; do
ns=`echo "${line}" | awk '{print $1}'`
for cluster in ${CLUSTER_LIST[@]}; do
OUTPUT=`kubectl get -n $ns $cluster 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
OUTPUT=`echo "${OUTPUT}" | grep -v NAME`
while read line; do
name=`echo ${line} | awk '{print $1}'`
case $cluster in
"ManagementCluster")
if [[ ${SUBSYS_MANAGER} == "ISNOTSET" ]]; then
SUBSYS_MANAGER=$name
else
SUBSYS_MANAGER+=" ${name}"
fi
((SUBSYS_MANAGER_COUNT=SUBSYS_MANAGER_COUNT+1))
;;
"AnalyticsCluster")
if [[ ${SUBSYS_ANALYTICS} == "ISNOTSET" ]]; then
SUBSYS_ANALYTICS=$name
else
SUBSYS_ANALYTICS+=" ${name}"
fi
((SUBSYS_ANALYTICS_COUNT=SUBSYS_ANALYTICS_COUNT+1))
;;
"PortalCluster")
if [[ ${SUBSYS_PORTAL} == "ISNOTSET" ]]; then
SUBSYS_PORTAL=$name
else
SUBSYS_PORTAL+=" ${name}"
fi
((SUBSYS_PORTAL_COUNT=SUBSYS_PORTAL_COUNT+1))
;;
"GatewayCluster")
if [[ "$name" == *"v5"* ]]; then
if [[ ${SUBSYS_GATEWAY_V5} == "ISNOTSET" ]]; then
SUBSYS_GATEWAY_V5=$name
else
SUBSYS_GATEWAY_V5+=" ${name}"
fi
((SUBSYS_GATEWAY_V5_COUNT=SUBSYS_GATEWAY_V5_COUNT+1))
else
if [[ ${SUBSYS_GATEWAY_V6} == "ISNOTSET" ]]; then
SUBSYS_GATEWAY_V6=$name
else
SUBSYS_GATEWAY_V6+=" ${name}"
fi
((SUBSYS_GATEWAY_V6_COUNT=SUBSYS_GATEWAY_V6_COUNT+1))
fi
;;
esac
if [[ "${ns_matches}" != *"${ns}"* ]]; then
if [[ ${#ns_matches} -eq 0 ]]; then
ns_matches=$ns
else
ns_matches+=" ${ns}"
fi
fi
done <<< "$OUTPUT"
fi
done
done <<< "$NS_LISTING"
[ $SPECIFIC_NAMESPACES -eq 1 ] || echo -e "Auto-detected namespaces [${ns_matches}]."
if [[ $NO_PROMPT -ne 1 ]]; then
read -p "Proceed with data collection (y/n)? " yn
case $yn in
[Yy]* )
echo -e "Proceeding..."
;;
[Nn]* )
echo -e "Exiting..."
exit 1
;;
esac
fi
[ $SPECIFIC_NAMESPACES -eq 1 ] || NAMESPACE_LIST+=" ${ns_matches}"
fi
#=================================================================================================================
#============================================= pull kubernetes data ==============================================
#----------------------------------------- create directories -----------------------------------------
K8S_DATA="${TEMP_PATH}/kubernetes"
K8S_CLUSTER="${K8S_DATA}/cluster"
K8S_NAMESPACES="${K8S_DATA}/namespaces"
K8S_VERSION="${K8S_DATA}/versions"
K8S_CLUSTER_NODE_DATA="${K8S_CLUSTER}/nodes"
K8S_CLUSTER_LIST_DATA="${K8S_CLUSTER}/lists"
K8S_CLUSTER_ROLE_DATA="${K8S_CLUSTER}/clusterroles"
K8S_CLUSTER_ROLEBINDING_DATA="${K8S_CLUSTER}/clusterrolebindings"
K8S_CLUSTER_CRD_DATA="${K8S_CLUSTER}/crd"
K8S_CLUSTER_CRD_DESCRIBE_DATA="${K8S_CLUSTER_CRD_DATA}/describe"
K8S_CLUSTER_PV_DATA="${K8S_CLUSTER}/pv"
K8S_CLUSTER_PV_DESCRIBE_DATA="${K8S_CLUSTER_PV_DATA}/describe"
K8S_CLUSTER_STORAGECLASS_DATA="${K8S_CLUSTER}/storageclasses"
K8S_CLUSTER_STORAGECLASS_DESCRIBE_DATA="${K8S_CLUSTER_STORAGECLASS_DATA}/describe"
K8S_CLUSTER_PERFORMANCE="${K8S_CLUSTER}/performance"
mkdir -p $K8S_VERSION
mkdir -p $K8S_CLUSTER_NODE_DATA
mkdir -p $K8S_CLUSTER_LIST_DATA
mkdir -p $K8S_CLUSTER_ROLE_DATA
mkdir -p $K8S_CLUSTER_ROLEBINDING_DATA
mkdir -p $K8S_CLUSTER_CRD_DESCRIBE_DATA
mkdir -p $K8S_CLUSTER_PV_DESCRIBE_DATA
mkdir -p $K8S_CLUSTER_STORAGECLASS_DESCRIBE_DATA
mkdir -p $K8S_CLUSTER_PERFORMANCE
#------------------------------------------------------------------------------------------------------
#grab kubernetes version
kubectl version 1>"${K8S_VERSION}/kubectl.version" 2>/dev/null
#----------------------------------- collect cluster specific data ------------------------------------
#node
OUTPUT=`kubectl get nodes 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" &> "${K8S_CLUSTER_NODE_DATA}/nodes.out"
while read line; do
name=`echo "$line" | awk -F ' ' '{print $1}'`
role=`echo "$line" | awk -F ' ' '{print $3}'`
describe_stdout=`kubectl describe node $name 2>/dev/null`
if [[ $? -eq 0 && ${#describe_stdout} -gt 0 ]]; then
if [[ -z "$role" ]]; then
echo "$describe_stdout" > "${K8S_CLUSTER_NODE_DATA}/describe-${name}.out"
else
echo "$describe_stdout" > "${K8S_CLUSTER_NODE_DATA}/describe-${name}_${role}.out"
fi
if [[ -z "$ARCHIVE_FILE" && "$role" == *"master"* ]]; then
host=`echo $name | cut -d'.' -f1`
if [[ -z "$host" ]]; then
ARCHIVE_FILE="${LOG_PATH}/apiconnect-logs-${TIMESTAMP}"
else
ARCHIVE_FILE="${LOG_PATH}/apiconnect-logs-${host}-${TIMESTAMP}"
fi
fi
fi
done <<< "$OUTPUT"
if [[ $OUTPUT_METRICS -eq 0 ]]; then
kubectl top nodes &> "${K8S_CLUSTER_NODE_DATA}/top.out"
[ $? -eq 0 ] || rm -f "${K8S_CLUSTER_NODE_DATA}/top.out"
fi
else
rm -fr $K8S_CLUSTER_NODE_DATA
fi
if [[ -z "$ARCHIVE_FILE" ]]; then
ARCHIVE_FILE="${LOG_PATH}/apiconnect-logs-${TIMESTAMP}"
fi
#cluster roles
OUTPUT=`kubectl get clusterroles 2>/dev/null | cut -d' ' -f1`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
while read line; do
kubectl describe clusterrole $line &> "${K8S_CLUSTER_ROLE_DATA}/${line}.out"
[ $? -eq 0 ] || rm -f "${K8S_CLUSTER_ROLE_DATA}/${line}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_CLUSTER_ROLE_DATA
fi
#cluster rolebindings
OUTPUT=`kubectl get clusterrolebindings 2>/dev/null | cut -d' ' -f1`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
while read line; do
kubectl describe clusterrolebinding $line &> "${K8S_CLUSTER_ROLEBINDING_DATA}/${line}.out"
[ $? -eq 0 ] || rm -f "${K8S_CLUSTER_ROLEBINDING_DATA}/${line}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_CLUSTER_ROLEBINDING_DATA
fi
#crds
OUTPUT=`kubectl get crds 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_CLUSTER_CRD_DATA}/crd.out"
while read line; do
crd=`echo "$line" | cut -d' ' -f1`
kubectl describe crd $crd &>"${K8S_CLUSTER_CRD_DESCRIBE_DATA}/${crd}.out"
[ $? -eq 0 ] || rm -f "${K8S_CLUSTER_CRD_DESCRIBE_DATA}/${crd}.out"
done <<< "$OUTPUT"
fi
#pv
OUTPUT=`kubectl get pv 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_CLUSTER_PV_DATA}/pv.out"
while read line; do
pv=`echo "$line" | cut -d' ' -f1`
kubectl describe pv $pv &>"${K8S_CLUSTER_PV_DESCRIBE_DATA}/${pv}.out"
[ $? -eq 0 ] || rm -f "${K8S_CLUSTER_PV_DESCRIBE_DATA}/${pv}.out"
done <<< "$OUTPUT"
fi
#storageclasses
OUTPUT=`kubectl get storageclasses 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_CLUSTER_STORAGECLASS_DATA}/storageclasses.out"
while read $line; do
sc=`echo "$line" | cut -d' ' -f1`
kubectl describe storageclasses $sc &>"${K8S_CLUSTER_STORAGECLASS_DESCRIBE_DATA}/${sc}.out"
[ $? -eq 0 ] || rm -f "${K8S_CLUSTER_STORAGECLASS_DESCRIBE_DATA}/${sc}.out"
done <<< "$OUTPUT"
fi
#check etcd cluster performance
if [[ $PERFORMANCE_CHECK -eq 1 ]]; then
if [[ $IS_OVA -eq 1 ]]; then
apic stage etcd-check-perf -l debug &> ${K8S_CLUSTER_PERFORMANCE}/etcd-performance.out # ova has special `apic stage` command that will run the etcd performance check and a defrag after
else
ETCD_POD=`kubectl get pod -n kube-system --selector component=etcd -o=jsonpath={.items[0].metadata.name} 2>/dev/null` # retrieve name of etcd pod to exec
# parse out etcd certs from pod describe
ETCD_CA_FILE=`kubectl describe pod -n kube-system ${ETCD_POD} | grep "\--trusted-ca-file" | cut -f2 -d"=" 2>/dev/null`
ETCD_CERT_FILE=`kubectl describe pod -n kube-system ${ETCD_POD} | grep "\--cert-file" | cut -f2 -d"=" 2>/dev/null`
ETCD_KEY_FILE=`kubectl describe pod -n kube-system ${ETCD_POD} | grep "\--key-file" | cut -f2 -d"=" 2>/dev/null`
OUTPUT=`kubectl exec -n kube-system ${ETCD_POD} -- sh -c "export ETCDCTL_API=3; etcdctl member list --cacert=${ETCD_CA_FILE} --cert=${ETCD_CERT_FILE} --key=${ETCD_KEY_FILE} 2>/dev/null"`
# parsing endpoints from etcd member list
ENDPOINTS=''
while read line; do
endpoint=`echo "$line" | awk '{print $5}'` # output formatting will change in etcd v3.4 so will need to update this
if [[ ${#ENDPOINTS} -eq 0 ]]; then
ENDPOINTS=$endpoint
else
ENDPOINTS="${ENDPOINTS},${endpoint}"
fi
done <<< "$OUTPUT"
ENDPOINTS=${ENDPOINTS%,} # strip trailing comma
# run etcd performance check
OUTPUT=`kubectl exec -n kube-system ${ETCD_POD} -- sh -c "export ETCDCTL_API=3; etcdctl check perf --endpoints="${ENDPOINTS}" --cacert=${ETCD_CA_FILE} --cert=${ETCD_CERT_FILE} --key=${ETCD_KEY_FILE}"`
echo "${OUTPUT}" > ${K8S_CLUSTER_PERFORMANCE}/etcd-performance.out
# run recommeneded `etcdctl defrag` to free up storage space
OUTPUT=`kubectl exec -n kube-system ${ETCD_POD} -- sh -c "export ETCDCTL_API=3; etcdctl defrag --endpoints="${ENDPOINTS}" --cacert=${ETCD_CA_FILE} --cert=${ETCD_CERT_FILE} --key=${ETCD_KEY_FILE}"`
echo "${OUTPUT}" > ${K8S_CLUSTER_PERFORMANCE}/etcd-defrag.out
fi
fi
#------------------------------------------------------------------------------------------------------
#---------------------------------- collect namespace specific data -----------------------------------
for NAMESPACE in $NAMESPACE_LIST; do
K8S_NAMESPACES_SPECIFIC="${K8S_NAMESPACES}/${NAMESPACE}"
K8S_NAMESPACES_LIST_DATA="${K8S_NAMESPACES_SPECIFIC}/lists"
K8S_NAMESPACES_CLUSTER_DATA="${K8S_NAMESPACES_SPECIFIC}/clusters"
K8S_NAMESPACES_CLUSTER_YAML_OUTPUT="${K8S_NAMESPACES_CLUSTER_DATA}/yaml"
K8S_NAMESPACES_CLUSTER_DESCRIBE_DATA="${K8S_NAMESPACES_CLUSTER_DATA}/describe"
K8S_NAMESPACES_CONFIGMAP_DATA="${K8S_NAMESPACES_SPECIFIC}/configmaps"
K8S_NAMESPACES_CONFIGMAP_YAML_OUTPUT="${K8S_NAMESPACES_CONFIGMAP_DATA}/yaml"
K8S_NAMESPACES_CONFIGMAP_DESCRIBE_DATA="${K8S_NAMESPACES_CONFIGMAP_DATA}/describe"
K8S_NAMESPACES_CRONJOB_DATA="${K8S_NAMESPACES_SPECIFIC}/cronjobs"
K8S_NAMESPACES_CRONJOB_DESCRIBE_DATA="${K8S_NAMESPACES_CRONJOB_DATA}/describe"
K8S_NAMESPACES_CRUNCHY_DATA="${K8S_NAMESPACES_SPECIFIC}/crunchy"
K8S_NAMESPACES_DAEMONSET_DATA="${K8S_NAMESPACES_SPECIFIC}/daemonsets"
K8S_NAMESPACES_DAEMONSET_YAML_OUTPUT="${K8S_NAMESPACES_DAEMONSET_DATA}/yaml"
K8S_NAMESPACES_DAEMONSET_DESCRIBE_DATA="${K8S_NAMESPACES_DAEMONSET_DATA}/describe"
K8S_NAMESPACES_DEPLOYMENT_DATA="${K8S_NAMESPACES_SPECIFIC}/deployments"
K8S_NAMESPACES_DEPLOYMENT_YAML_OUTPUT="${K8S_NAMESPACES_DEPLOYMENT_DATA}/yaml"
K8S_NAMESPACES_DEPLOYMENT_DESCRIBE_DATA="${K8S_NAMESPACES_DEPLOYMENT_DATA}/describe"
K8S_NAMESPACES_ENDPOINT_DATA="${K8S_NAMESPACES_SPECIFIC}/endpoints"
K8S_NAMESPACES_ENDPOINT_DESCRIBE_DATA="${K8S_NAMESPACES_ENDPOINT_DATA}/describe"
K8S_NAMESPACES_ENDPOINT_YAML_OUTPUT="${K8S_NAMESPACES_ENDPOINT_DATA}/yaml"
K8S_NAMESPACES_JOB_DATA="${K8S_NAMESPACES_SPECIFIC}/jobs"
K8S_NAMESPACES_JOB_DESCRIBE_DATA="${K8S_NAMESPACES_JOB_DATA}/describe"
K8S_NAMESPACES_JOB_YAML_OUTPUT="${K8S_NAMESPACES_JOB_DATA}/yaml"
K8S_NAMESPACES_POD_DATA="${K8S_NAMESPACES_SPECIFIC}/pods"
K8S_NAMESPACES_POD_DESCRIBE_DATA="${K8S_NAMESPACES_POD_DATA}/describe"
K8S_NAMESPACES_POD_DIAGNOSTIC_DATA="${K8S_NAMESPACES_POD_DATA}/diagnostic"
K8S_NAMESPACES_POD_LOG_DATA="${K8S_NAMESPACES_POD_DATA}/logs"
K8S_NAMESPACES_PVC_DATA="${K8S_NAMESPACES_SPECIFIC}/pvc"
K8S_NAMESPACES_PVC_DESCRIBE_DATA="${K8S_NAMESPACES_PVC_DATA}/describe"
K8S_NAMESPACES_PGTASKS_DATA="${K8S_NAMESPACES_SPECIFIC}/pgtasks"
K8S_NAMESPACES_PGTASKS_DESCRIBE_DATA="${K8S_NAMESPACES_PGTASKS_DATA}/describe"
K8S_NAMESPACES_REPLICASET_DATA="${K8S_NAMESPACES_SPECIFIC}/replicasets"
K8S_NAMESPACES_REPLICASET_YAML_OUTPUT="${K8S_NAMESPACES_REPLICASET_DATA}/yaml"
K8S_NAMESPACES_REPLICASET_DESCRIBE_DATA="${K8S_NAMESPACES_REPLICASET_DATA}/describe"
K8S_NAMESPACES_ROLE_DATA="${K8S_NAMESPACES_SPECIFIC}/roles"
K8S_NAMESPACES_ROLE_DESCRIBE_DATA="${K8S_NAMESPACES_ROLE_DATA}/describe"
K8S_NAMESPACES_ROLEBINDING_DATA="${K8S_NAMESPACES_SPECIFIC}/rolebindings"
K8S_NAMESPACES_ROLEBINDING_DESCRIBE_DATA="${K8S_NAMESPACES_ROLEBINDING_DATA}/describe"
K8S_NAMESPACES_SA_DATA="${K8S_NAMESPACES_SPECIFIC}/service_accounts"
K8S_NAMESPACES_SA_DESCRIBE_DATA="${K8S_NAMESPACES_SA_DATA}/describe"
K8S_NAMESPACES_SECRET_DATA="${K8S_NAMESPACES_SPECIFIC}/secrets"
K8S_NAMESPACES_SECRET_DESCRIBE_DATA="${K8S_NAMESPACES_SECRET_DATA}/describe"
K8S_NAMESPACES_SECRET_YAML_OUTPUT="${K8S_NAMESPACES_SECRET_DATA}/yaml"
K8S_NAMESPACES_SERVICE_DATA="${K8S_NAMESPACES_SPECIFIC}/services"
K8S_NAMESPACES_SERVICE_DESCRIBE_DATA="${K8S_NAMESPACES_SERVICE_DATA}/describe"
K8S_NAMESPACES_SERVICE_YAML_OUTPUT="${K8S_NAMESPACES_SERVICE_DATA}/yaml"
K8S_NAMESPACES_STS_DATA="${K8S_NAMESPACES_SPECIFIC}/statefulset"
K8S_NAMESPACES_STS_DESCRIBE_DATA="${K8S_NAMESPACES_STS_DATA}/describe"
mkdir -p $K8S_NAMESPACES_LIST_DATA
mkdir -p $K8S_NAMESPACES_CLUSTER_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_CLUSTER_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_CONFIGMAP_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_CONFIGMAP_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_CRONJOB_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_CRUNCHY_DATA
mkdir -p $K8S_NAMESPACES_DAEMONSET_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_DAEMONSET_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_DEPLOYMENT_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_DEPLOYMENT_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_ENDPOINT_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_ENDPOINT_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_JOB_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_JOB_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_POD_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_POD_LOG_DATA
mkdir -p $K8S_NAMESPACES_PVC_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_PGTASKS_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_REPLICASET_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_REPLICASET_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_ROLE_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_ROLEBINDING_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_SA_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_SECRET_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_SECRET_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_SERVICE_DESCRIBE_DATA
mkdir -p $K8S_NAMESPACES_SERVICE_YAML_OUTPUT
mkdir -p $K8S_NAMESPACES_STS_DESCRIBE_DATA
#grab cluster configuration, equivalent to "apiconnect-up.yml" which now resides in cluster
CLUSTER_LIST=(apic ManagementCluster ManagementBackup ManagementRestore ManagementDBUpgrade AnalyticsCluster PortalCluster GatewayCluster)
for cluster in ${CLUSTER_LIST[@]}; do
OUTPUT=`kubectl get -n $NAMESPACE $cluster 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "${OUTPUT}" > "${K8S_NAMESPACES_CLUSTER_DATA}/${cluster}.out"
kubectl describe $cluster -n $NAMESPACE &>"${K8S_NAMESPACES_CLUSTER_DESCRIBE_DATA}/${cluster}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_CLUSTER_DESCRIBE_DATA}/${cluster}.out"
kubectl get $cluster -n $NAMESPACE -o yaml &>"${K8S_NAMESPACES_CLUSTER_YAML_OUTPUT}/${cluster}.yaml"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_CLUSTER_YAML_OUTPUT}/${cluster}.yaml"
fi
done
#grab lists
OUTPUT=`kubectl get events -n $NAMESPACE 2>/dev/null`
[[ $? -ne 0 || ${#OUTPUT} -eq 0 ]] || echo "$OUTPUT" > "${K8S_NAMESPACES_LIST_DATA}/events.out"
OUTPUT=`kubectl get hpa -n $NAMESPACE 2>/dev/null`
[[ $? -ne 0 || ${#OUTPUT} -eq 0 ]] || echo "$OUTPUT" > "${K8S_NAMESPACES_LIST_DATA}/hpa.out"
OUTPUT=`kubectl get validatingwebhookconfiguration -n $NAMESPACE 2>/dev/null`
[[ $? -ne 0 || ${#OUTPUT} -eq 0 ]] || echo "$OUTPUT" > "${K8S_NAMESPACES_LIST_DATA}/validatingwebhookconfiguration.out"
OUTPUT=`kubectl get mutatingwebhookconfiguration -n $NAMESPACE 2>/dev/null`
[[ $? -ne 0 || ${#OUTPUT} -eq 0 ]] || echo "$OUTPUT" > "${K8S_NAMESPACES_LIST_DATA}/mutatingwebhookconfiguration.out"
#grab ingress/routes then check each
OUTPUT=`kubectl get ingress -n $NAMESPACE 2>/dev/null`
if [[ ${#OUTPUT} -gt 0 ]]; then
ir_outfile="ingress.out"
ir_checks_outfile="ingress-checks.out"
IS_OCP=0
else
OUTPUT=`kubectl get routes -n $NAMESPACE 2>/dev/null`
ir_outfile="routes.out"
ir_checks_outfile="routes-checks.out"
IS_OCP=1
fi
IR_OUTFILE="${K8S_NAMESPACES_LIST_DATA}/${ir_outfile}"
IR_CHECKS_OUTFILE="${K8S_NAMESPACES_LIST_DATA}/${ir_checks_outfile}"
if [[ ${#OUTPUT} -gt 0 && ! -f $IR_OUTFILE && ! -f $IR_CHECKS_OUTFILE ]]; then
echo "$OUTPUT" > $IR_OUTFILE
#check if portal pods are available to use nslookup
OUTPUT1=`kubectl get pods -n $NAMESPACE 2>/dev/null | egrep -v "up|downloads" | egrep "portal.*www|-apim-|-client-|-ui-" | head -n1`
if [[ ${#OUTPUT} -gt 0 ]]; then
nslookup_pod=`echo "${OUTPUT1}" | awk '{print $1}'`
fi
#determine host column
title_column=`echo "${OUTPUT}" | head -n1`
column_count=`echo "${title_column}" | awk '{print NF}'`
pos=1
while [ $pos -lt $column_count ]; do
token=`echo "${title_column}" | awk -v p=$pos '{print $p}'`
if [[ "${token}" == *"HOST"* ]]; then
break
fi
pos=$(( $pos + 1 ))
done
#check hosts
if [[ ${#nslookup_pod} -gt 0 && $pos -lt $column_count ]]; then
ingress_list=`echo "${OUTPUT}" | grep -v NAME | awk -v p=$pos '{print $p}' | uniq`
at_start=1
while read ingress; do
nslookup_output=`kubectl exec -n $NAMESPACE $nslookup_pod -- nslookup $ingress 2>&1`
if [[ $at_start -eq 1 ]]; then
echo -e "${nslookup_output}" > $IR_CHECKS_OUTFILE
else
echo -e "\n\n===============\n\n${nslookup_output}" >> $IR_CHECKS_OUTFILE
fi
at_start=0
done <<< "$ingress_list"
fi
fi
#grab configmap data
OUTPUT=`kubectl get configmaps -n $NAMESPACE 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_NAMESPACES_CONFIGMAP_DATA}/configmaps.out"
while read line; do
cm=`echo "$line" | cut -d' ' -f1`
kubectl get configmap $cm -n $NAMESPACE -o yaml &>"${K8S_NAMESPACES_CONFIGMAP_YAML_OUTPUT}/${cm}.yaml"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_CONFIGMAP_YAML_OUTPUTA}/${cm}.yaml"
kubectl describe configmap $cm -n $NAMESPACE &> "${K8S_NAMESPACES_CONFIGMAP_DESCRIBE_DATA}/${cm}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_CONFIGMAP_DESCRIBE_DATA}/${cm}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_NAMESPACES_CONFIGMAP_DATA
fi
#grab cronjob data
OUTPUT=`kubectl get cronjobs -n $NAMESPACE 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_NAMESPACES_CRONJOB_DATA}/cronjobs.out"
while read line; do
cronjob=`echo "$line" | cut -d' ' -f1`
kubectl describe cronjob $cronjob -n $NAMESPACE &> "${K8S_NAMESPACES_CRONJOB_DESCRIBE_DATA}/${cronjob}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_CRONJOB_DESCRIBE_DATA}/${cronjob}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_NAMESPACES_CRONJOB_DATA
fi
#grab crunchy mustgather
if [[ $COLLECT_CRUNCHY -eq 1 && "$NAMESPACE" != "kube-system" ]]; then
$CURRENT_PATH/crunchy_gather.py -n $NAMESPACE -o $K8S_NAMESPACES_CRUNCHY_DATA &> "${K8S_NAMESPACES_CRUNCHY_DATA}/output.log"
fi
#grab daemonset data
OUTPUT=`kubectl get daemonset -n $NAMESPACE 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_NAMESPACES_DAEMONSET_DATA}/daemonset.out"
while read line; do
ds=`echo "$line" | cut -d' ' -f1`
kubectl describe daemonset $ds -n $NAMESPACE &>"${K8S_NAMESPACES_DAEMONSET_DESCRIBE_DATA}/${ds}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_DAEMONSET_DESCRIBE_DATA}/${ds}.out"
kubectl get daemonset $ds -o yaml -n $NAMESPACE &>"${K8S_NAMESPACES_DAEMONSET_YAML_OUTPUT}/${ds}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_DAEMONSET_YAML_OUTPUT}/${ds}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_NAMESPACES_DAEMONSET_DATA
fi
#grab deployment data
OUTPUT=`kubectl get deployments -n $NAMESPACE 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_NAMESPACES_DEPLOYMENT_DATA}/deployments.out"
while read line; do
deployment=`echo "$line" | cut -d' ' -f1`
kubectl describe deployment $deployment -n $NAMESPACE &>"${K8S_NAMESPACES_DEPLOYMENT_DESCRIBE_DATA}/${deployment}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_DEPLOYMENT_DESCRIBE_DATA}/${deployment}.out"
kubectl get deployment $deployment -o yaml -n $NAMESPACE &>"${K8S_NAMESPACES_DEPLOYMENT_DESCRIBE_DATA}/${deployment}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_DEPLOYMENT_DESCRIBE_DATA}/${deployment}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_NAMESPACES_DEPLOYMENT_DATA
fi
#grab endpoint data
OUTPUT=`kubectl get endpoints -n $NAMESPACE 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_NAMESPACES_ENDPOINT_DATA}/endpoints.out"
while read line; do
endpoint=`echo "$line" | cut -d' ' -f1`
kubectl describe endpoints $endpoint -n $NAMESPACE &>"${K8S_NAMESPACES_ENDPOINT_DESCRIBE_DATA}/${endpoint}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_ENDPOINT_DESCRIBE_DATA}/${endpoint}.out"
kubectl get endpoints $endpoint -o yaml -n $NAMESPACE &>"${K8S_NAMESPACES_ENDPOINT_YAML_OUTPUT}/${endpoint}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_ENDPOINT_YAML_OUTPUT}/${endpoint}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_NAMESPACES_ENDPOINT_DATA
fi
#grab job data
OUTPUT=`kubectl get jobs -n $NAMESPACE 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_NAMESPACES_JOB_DATA}/jobs.out"
while read line; do
job=`echo "$line" | cut -d' ' -f1`
kubectl describe job $job -n $NAMESPACE &> "${K8S_NAMESPACES_JOB_DESCRIBE_DATA}/${job}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_JOB_DESCRIBE_DATA}/${job}.out"
kubectl get job $job -o yaml -n $NAMESPACE &>"${K8S_NAMESPACES_JOB_YAML_OUTPUT}/${job}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_JOB_YAML_OUTPUT}/${job}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_NAMESPACES_JOB_DATA
fi
#grab pgtasks data
OUTPUT=`kubectl get pgtasks -n $NAMESPACE 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_NAMESPACES_PGTASKS_DATA}/pgtasks.out"
while read line; do
pgtask=`echo "$line" | cut -d' ' -f1`
kubectl describe pgtask $pgtask -n $NAMESPACE &>"${K8S_NAMESPACES_PGTASKS_DESCRIBE_DATA}/${pgtask}.out"
[ $? -eq 0 ] || rm -f "${K8S_NAMESPACES_PGTASKS_DESCRIBE_DATA}/${pgtask}.out"
done <<< "$OUTPUT"
else
rm -fr $K8S_NAMESPACES_PGTASKS_DATA
fi
#grab pod data
OUTPUT=`kubectl get pods -n $NAMESPACE -o wide 2>/dev/null`
if [[ $? -eq 0 && ${#OUTPUT} -gt 0 ]]; then
echo "$OUTPUT" > "${K8S_NAMESPACES_POD_DATA}/pods.out"
while read line; do
pod=`echo "$line" | awk -F ' ' '{print $1}'`
ready=`echo "$line" | awk -F ' ' '{print $2}'`
status=`echo "$line" | awk -F ' ' '{print $3}'`
node=`echo "$line" | awk -F ' ' '{print $7}'`
IS_INGRESS=0
IS_GATEWAY=0
IS_PORTAL=0
IS_ANALYTICS=0
subManager=""
subAnalytics=""
subPortal=""
subGateway=""
CHECK_INGRESS=0
case $NAMESPACE in
"kube-system")
case "$pod" in
*"calico"*|*"flannel"*) SUBFOLDER="networking";;
*"coredns"*) SUBFOLDER="coredns";;
*"etcd"*) SUBFOLDER="etcd";;
*"ingress"*)
IS_INGRESS=1
SUBFOLDER="ingress"
;;
*"kube"*) SUBFOLDER="kube";;
*"metrics"*) SUBFOLDER="metrics";;
*"tiller"*) SUBFOLDER="tiller";;
*) SUBFOLDER="other";;
esac
DESCRIBE_TARGET_PATH="${K8S_NAMESPACES_POD_DESCRIBE_DATA}/${SUBFOLDER}"
LOG_TARGET_PATH="${K8S_NAMESPACES_POD_LOG_DATA}/${SUBFOLDER}";;
*"rook"*)
DESCRIBE_TARGET_PATH="${K8S_NAMESPACES_POD_DESCRIBE_DATA}"
LOG_TARGET_PATH="${K8S_NAMESPACES_POD_LOG_DATA}";;
*)
CHECK_INGRESS=1
case $pod in
*"${SUBSYS_MANAGER}"*|*"postgres"*)
SUBFOLDER="manager"
subManager=$SUBSYS_MANAGER
;;
*"${SUBSYS_ANALYTICS}"*)
SUBFOLDER="analytics"
subAnalytics=$SUBSYS_ANALYTICS
IS_ANALYTICS=1
;;
*"${SUBSYS_PORTAL}"*)
SUBFOLDER="portal"
subPortal=$SUBSYS_PORTAL
IS_PORTAL=1
;;
*"${SUBSYS_GATEWAY_V5}"*)
SUBFOLDER="gateway"
subGateway=$SUBSYS_GATEWAY_V5
IS_GATEWAY=1
;;
"${SUBSYS_GATEWAY_V6}"*)
SUBFOLDER="gateway"
subGateway=$SUBSYS_GATEWAY_V6
IS_GATEWAY=1
;;
*"datapower"*)
SUBFOLDER="gateway"
IS_GATEWAY=1
;;
"ingress-nginx"*)
IS_INGRESS=1
SUBFOLDER="ingress"
;;
*"ibm-apiconnect"*)
SUBFOLDER="operator"
;;
*)
#check for multiple subsystems
if [[ SUBSYS_MANAGER_COUNT -gt 1 ]]; then
for s in $SUBSYS_MANAGER; do
if [[ "${pod}" == "${s}-"* ]]; then
SUBFOLDER="manager"
subManager=$s
fi
done
elif [[ SUBSYS_ANALYTICS_COUNT -gt 1 ]]; then
for s in $SUBSYS_ANALYTICS; do
if [[ "${pod}" == "${s}-"* ]]; then
SUBFOLDER="analytics"
subAnalytics=$s
IS_ANALYTICS=1
fi
done
elif [[ SUBSYS_PORTAL_COUNT -gt 1 ]]; then
for s in $SUBSYS_PORTAL; do
if [[ "${pod}" == "${s}-"* ]]; then
SUBFOLDER="portal"
subPortal=$s
IS_PORTAL=1
fi
done
elif [[ SUBSYS_GATEWAY_V5_COUNT -gt 1 ]]; then
for s in $SUBSYS_GATEWAY_V5; do
if [[ "${pod}" == "${s}-"* ]]; then
SUBFOLDER="gateway"
subGateway=$s
IS_GATEWAY=1
fi
done
elif [[ SUBSYS_GATEWAY_V6_COUNT -gt 1 ]]; then
for s in $SUBSYS_GATEWAY_V6; do
if [[ "${pod}" == "${s}-"* ]]; then
SUBFOLDER="gateway"
subGateway=$s
IS_GATEWAY=1
fi
done
else
SUBFOLDER="other"
fi
esac
DESCRIBE_TARGET_PATH="${K8S_NAMESPACES_POD_DESCRIBE_DATA}/${SUBFOLDER}"
LOG_TARGET_PATH="${K8S_NAMESPACES_POD_LOG_DATA}/${SUBFOLDER}";;
esac
#make sure directories exist
if [[ ! -d "$DESCRIBE_TARGET_PATH" ]]; then
mkdir -p $DESCRIBE_TARGET_PATH
fi
if [[ ! -d "$LOG_TARGET_PATH" ]]; then
mkdir -p $LOG_TARGET_PATH
fi
#grab ingress configuration
if [[ $IS_INGRESS -eq 1 ]]; then
kubectl cp -n $NAMESPACE "${pod}:/etc/nginx/nginx.conf" "${LOG_TARGET_PATH}/${pod}_nginx-ingress-configuration.out" &>/dev/null
[[ $? -eq 0 && -s "${LOG_TARGET_PATH}/${pod}_nginx-ingress-configuration.out" ]] || rm -f "${LOG_TARGET_PATH}/${pod}_nginx-ingress-configuration.out"
#reset variable
IS_INGRESS=0
fi
#grab postgres data
if [[ $DIAG_MANAGER -eq 1 && "$status" == "Running" && "$pod" == *"postgres"* && ! "$pod" =~ (backrest|pgbouncer|stanza|operator) ]]; then
target_dir="${K8S_NAMESPACES_POD_DIAGNOSTIC_DATA}/postgres/${pod}-pglogs"
health_dir="${K8S_NAMESPACES_POD_DIAGNOSTIC_DATA}/postgres/${pod}-health-stats"
mkdir -p $target_dir
mkdir -p $health_dir
POSTGRES_PGLOGS_NAME=`kubectl exec -n $NAMESPACE ${pod} -- ls -1 /pgdata | grep -v lost 2>"/dev/null"`
POSTGRES_PGWAL_NAME=`kubectl exec -n $NAMESPACE ${pod} -- ls -1 /pgwal | grep -v lost 2>"/dev/null"`
#pglogs
kubectl cp -n $NAMESPACE "${pod}:/pgdata/${POSTGRES_PGLOGS_NAME}/pglogs" $target_dir &>/dev/null
#df
DB_DF_OUTPUT=`kubectl exec -n $NAMESPACE ${pod} -c database -- df -h 2>"/dev/null"`
echo "$DB_DF_OUTPUT" > $health_dir/df.out
#pg wal dir count
PG_WAL_DIR_COUNT=`kubectl exec -n $NAMESPACE ${pod} -c database -- ls -lrt /pgwal/${POSTGRES_PGWAL_NAME}/ | wc -l 2>"/dev/null"`
echo "$PG_WAL_DIR_COUNT" > $health_dir/pgwal-dir-count.out
#pg wal dir history data