blob: 734c5a6c62b0014307b1b959b64571e73cfe97ca [file] [log] [blame]
yuryna16f29e2018-01-04 13:13:54 +02001#!/bin/bash
2
3NS=
4OUT_NAME=onap_info_$(date +%y.%m.%d_%H.%M.%S.%N)
5OUT_FILE=
6OUT_DIR=$(dirname "$0")
7TMP_DIR=$(dirname $(mktemp -u))
8CONTAINER_LOGS_PATH=/var/log/onap
9CP_CONTAINER_LOGS=false
10
11if [ ! -z "$DEBUG" ]; then
12 set -x
13fi
14
15usage() {
16 cat <<EOF
17Utility script collecting various information about ONAP deployment on kubernetes.
18
19Usage: $0 [PARAMs]
20-u : Display usage
21-n [NAMESPACE] : Kubernetes namespace (required)
22-a [APP] : Specify a specific ONAP component (default: all)
23-d [OUT_DIR] : Specify output folder for the collected info pack file
24 (default: current dir)
25-f [OUT_FILE] : Specify output file for the collected info
26 (default: file name with timestamp)
27-c : Collect log files from containers, from path ${CONTAINER_LOGS_PATH}
28EOF
29}
30
31call_with_log() {
32 local _cmd=$1
33 local _log=$2
34 # Make sure otput dir exists
35 mkdir -p "$(dirname "$_log")"
36 printf "Command: ${_cmd}\n" >> ${_log}
37 printf "================================================================================\n" >> ${_log}
38 eval "${_cmd}" >> ${_log} 2>&1
39 printf "================================================================================\n" >> ${_log}
40}
41
42collect_pod_info() {
43 local _ns=$1
44 local _id=$2
45 local _log_dir=$3
46 local _cp_logs=$4
47 declare -i _i=0
48 kubectl -n $_ns get pods $_id -o=jsonpath='{range .spec.containers[*]}{.name}{"\n"}{end}' | while read c; do
49 call_with_log "kubectl -n $_ns logs $_id -c $c" "$_log_dir/$_id-$c.log"
50 if [ "$_i" -eq "0" ] && [ "$_cp_logs" == "true" ]; then
51 # copy logs from 1st container only as logs dir is shared between the containers
52 local _cmd="kubectl cp $_ns/$_id:${CONTAINER_LOGS_PATH} $_log_dir/$_id-$c -c $c"
53 if [ -z "$DEBUG" ]; then
54 _cmd+=" > /dev/null 2>&1"
55 fi
56 eval "${_cmd}"
57 fi
58 ((_i++))
59 done
60}
61
62collect_ns_info() {
63 local _ns=$1
64 local _log_dir=$2/$_ns
65 call_with_log "kubectl -n $NS-$i get services -o=wide" "$_log_dir/list_services.log"
66 kubectl -n "$_ns" get services | while read i; do
67 local _id=`echo -n $i | tr -s ' ' | cut -d' ' -n -f1`
68 if [ "$_id" == "NAME" ]; then
69 continue
70 fi
71 call_with_log "kubectl -n $_ns describe services $_id" "$_log_dir/describe_services/$_id.log"
72 done
73 call_with_log "kubectl -n $NS-$i get pods -o=wide" "$_log_dir/list_pods.log"
74 kubectl -n "$_ns" get pods | while read i; do
75 local _id=`echo -n $i | tr -s ' ' | cut -d' ' -n -f1`
76 if [ "$_id" == "NAME" ]; then
77 continue
78 fi
79 call_with_log "kubectl -n $_ns describe pods $_id" "$_log_dir/describe_pods/$_id.log"
80 collect_pod_info "$_ns" "$_id" "$_log_dir/logs" "${CP_CONTAINER_LOGS}"
81 done
82}
83
84while getopts ":un:a:d:f:c" PARAM; do
85 case $PARAM in
86 u)
87 usage
88 exit 1
89 ;;
90 n)
91 NS=${OPTARG}
92 ;;
93 a)
94 APP=${OPTARG}
95 if [[ -z $APP ]]; then
96 usage
97 exit 1
98 fi
99 ;;
100 d)
101 OUT_DIR=${OPTARG}
102 if [[ -z $OUT_DIR ]]; then
103 usage
104 exit 1
105 fi
106 ;;
107 f)
108 OUT_FILE=${OPTARG}
109 if [[ -z $OUT_FILE ]]; then
110 usage
111 exit 1
112 fi
113 ;;
114 c)
115 CP_CONTAINER_LOGS=true
116 ;;
117 ?)
118 usage
119 exit
120 ;;
121 esac
122done
123
124if [ -z "$NS" ]; then
125 usage
126 exit 1
127fi
128
129if [[ -z $OUT_FILE ]]; then
130 OUT_FILE=$OUT_NAME.tgz
131fi
132
133if [ ! -z "$APP" ]; then
134 _APPS=($APP)
135else
136 _APPS=(`kubectl get namespaces | grep "^$NS-" | tr -s ' ' | cut -d' ' -n -f1 | sed -e "s/^$NS-//"`)
137fi
138
139printf "Collecting information about ONAP deployment...\n"
140printf "Components: %s\n" "${_APPS[*]}"
141
142# Collect common info
143mkdir -p ${TMP_DIR}/${OUT_NAME}/
144echo "${_APPS[*]}" > ${TMP_DIR}/${OUT_NAME}/component-list.log
145printf "Collecting Helm info\n"
146call_with_log "helm version" "${TMP_DIR}/${OUT_NAME}/helm-version.log"
147call_with_log "helm list" "${TMP_DIR}/${OUT_NAME}/helm-list.log"
148
149printf "Collecting Kubernetes info\n"
150call_with_log "kubectl version" "${TMP_DIR}/${OUT_NAME}/k8s-version.log"
151call_with_log "kubectl get nodes -o=wide" "${TMP_DIR}/${OUT_NAME}/k8s-nodes.log"
152call_with_log "kubectl cluster-info" "${TMP_DIR}/${OUT_NAME}/k8s-cluster-info.log"
153call_with_log "kubectl cluster-info dump" "${TMP_DIR}/${OUT_NAME}/k8s-cluster-info-dump.log"
154call_with_log "kubectl top node" "${TMP_DIR}/${OUT_NAME}/k8s-top-node.log"
155
156# Collect per-component info
157for i in ${_APPS[@]}; do
158 printf "Writing Kubernetes info of component $i\n"
159 collect_ns_info "$NS-$i" "${TMP_DIR}/${OUT_NAME}"
160done
161
162# Pack and cleanup
163mkdir -p ${OUT_DIR}
164_OUT_DIR=`readlink -e ${OUT_DIR}`
165printf "Packing output to ${_OUT_DIR}/${OUT_FILE}...\n"
166cd ${TMP_DIR}
167tar cfz ${_OUT_DIR}/${OUT_FILE} ${OUT_NAME}
168cd -
169printf "Cleaning up...\n"
170rm -rf ${TMP_DIR}/${OUT_NAME}
171printf "Done\n"