aboutsummaryrefslogtreecommitdiffstats
path: root/kubernetes/oneclick/tools/collectInfo.bash
blob: 734c5a6c62b0014307b1b959b64571e73cfe97ca (plain)
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
#!/bin/bash

NS=
OUT_NAME=onap_info_$(date +%y.%m.%d_%H.%M.%S.%N)
OUT_FILE=
OUT_DIR=$(dirname "$0")
TMP_DIR=$(dirname $(mktemp -u))
CONTAINER_LOGS_PATH=/var/log/onap
CP_CONTAINER_LOGS=false

if [ ! -z "$DEBUG" ]; then
  set -x
fi

usage() {
  cat <<EOF
Utility script collecting various information about ONAP deployment on kubernetes.

Usage: $0 [PARAMs]
-u                  : Display usage
-n [NAMESPACE]      : Kubernetes namespace (required)
-a [APP]            : Specify a specific ONAP component (default: all)
-d [OUT_DIR]        : Specify output folder for the collected info pack file
                      (default: current dir)
-f [OUT_FILE]       : Specify output file for the collected info
                      (default: file name with timestamp)
-c                  : Collect log files from containers, from path ${CONTAINER_LOGS_PATH}
EOF
}

call_with_log() {
  local _cmd=$1
  local _log=$2
  # Make sure otput dir exists
  mkdir -p "$(dirname "$_log")"
  printf "Command: ${_cmd}\n" >> ${_log}
  printf "================================================================================\n" >> ${_log}
  eval "${_cmd}" >> ${_log} 2>&1
  printf "================================================================================\n" >> ${_log}
}

collect_pod_info() {
  local _ns=$1
  local _id=$2
  local _log_dir=$3
  local _cp_logs=$4
  declare -i _i=0
  kubectl -n $_ns get pods $_id -o=jsonpath='{range .spec.containers[*]}{.name}{"\n"}{end}' | while read c; do
    call_with_log "kubectl -n $_ns logs $_id -c $c" "$_log_dir/$_id-$c.log"
    if [ "$_i" -eq "0" ] && [ "$_cp_logs" == "true" ]; then
      # copy logs from 1st container only as logs dir is shared between the containers
      local _cmd="kubectl cp $_ns/$_id:${CONTAINER_LOGS_PATH} $_log_dir/$_id-$c -c $c"
      if [ -z "$DEBUG" ]; then
        _cmd+=" > /dev/null 2>&1"
      fi
      eval "${_cmd}"
    fi
    ((_i++))
  done
}

collect_ns_info() {
  local _ns=$1
  local _log_dir=$2/$_ns
  call_with_log "kubectl -n $NS-$i get services -o=wide" "$_log_dir/list_services.log"
  kubectl -n "$_ns" get services | while read i; do
    local _id=`echo -n $i | tr -s ' ' | cut -d' ' -n -f1`
    if [ "$_id" == "NAME" ]; then
      continue
    fi
    call_with_log "kubectl -n $_ns describe services $_id" "$_log_dir/describe_services/$_id.log"
  done
  call_with_log "kubectl -n $NS-$i get pods -o=wide" "$_log_dir/list_pods.log"
  kubectl -n "$_ns" get pods | while read i; do
    local _id=`echo -n $i | tr -s ' ' | cut -d' ' -n -f1`
    if [ "$_id" == "NAME" ]; then
      continue
    fi
    call_with_log "kubectl -n $_ns describe pods $_id" "$_log_dir/describe_pods/$_id.log"
    collect_pod_info "$_ns" "$_id" "$_log_dir/logs" "${CP_CONTAINER_LOGS}"
  done
}

while getopts ":un:a:d:f:c" PARAM; do
  case $PARAM in
    u)
      usage
      exit 1
      ;;
    n)
      NS=${OPTARG}
      ;;
    a)
      APP=${OPTARG}
      if [[ -z $APP ]]; then
        usage
        exit 1
      fi
      ;;
    d)
      OUT_DIR=${OPTARG}
      if [[ -z $OUT_DIR ]]; then
        usage
        exit 1
      fi
      ;;
    f)
      OUT_FILE=${OPTARG}
      if [[ -z $OUT_FILE ]]; then
        usage
        exit 1
      fi
      ;;
    c)
      CP_CONTAINER_LOGS=true
      ;;
    ?)
      usage
      exit
      ;;
  esac
done

if [ -z "$NS" ]; then
  usage
  exit 1
fi

if [[ -z $OUT_FILE ]]; then
  OUT_FILE=$OUT_NAME.tgz
fi

if [ ! -z "$APP" ]; then
  _APPS=($APP)
else
  _APPS=(`kubectl get namespaces | grep "^$NS-" | tr -s ' ' | cut -d' ' -n -f1 | sed -e "s/^$NS-//"`)
fi

printf "Collecting information about ONAP deployment...\n"
printf "Components: %s\n" "${_APPS[*]}"

# Collect common info
mkdir -p ${TMP_DIR}/${OUT_NAME}/
echo "${_APPS[*]}" > ${TMP_DIR}/${OUT_NAME}/component-list.log
printf "Collecting Helm info\n"
call_with_log "helm version" "${TMP_DIR}/${OUT_NAME}/helm-version.log"
call_with_log "helm list" "${TMP_DIR}/${OUT_NAME}/helm-list.log"

printf "Collecting Kubernetes info\n"
call_with_log "kubectl version" "${TMP_DIR}/${OUT_NAME}/k8s-version.log"
call_with_log "kubectl get nodes -o=wide" "${TMP_DIR}/${OUT_NAME}/k8s-nodes.log"
call_with_log "kubectl cluster-info" "${TMP_DIR}/${OUT_NAME}/k8s-cluster-info.log"
call_with_log "kubectl cluster-info dump" "${TMP_DIR}/${OUT_NAME}/k8s-cluster-info-dump.log"
call_with_log "kubectl top node" "${TMP_DIR}/${OUT_NAME}/k8s-top-node.log"

# Collect per-component info
for i in ${_APPS[@]}; do
  printf "Writing Kubernetes info of component $i\n"
  collect_ns_info "$NS-$i" "${TMP_DIR}/${OUT_NAME}"
done

# Pack and cleanup
mkdir -p ${OUT_DIR}
_OUT_DIR=`readlink -e ${OUT_DIR}`
printf "Packing output to ${_OUT_DIR}/${OUT_FILE}...\n"
cd ${TMP_DIR}
tar cfz ${_OUT_DIR}/${OUT_FILE} ${OUT_NAME}
cd -
printf "Cleaning up...\n"
rm -rf ${TMP_DIR}/${OUT_NAME}
printf "Done\n"