yuryn | a16f29e | 2018-01-04 13:13:54 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | NS= |
| 4 | OUT_NAME=onap_info_$(date +%y.%m.%d_%H.%M.%S.%N) |
| 5 | OUT_FILE= |
| 6 | OUT_DIR=$(dirname "$0") |
| 7 | TMP_DIR=$(dirname $(mktemp -u)) |
| 8 | CONTAINER_LOGS_PATH=/var/log/onap |
| 9 | CP_CONTAINER_LOGS=false |
| 10 | |
| 11 | if [ ! -z "$DEBUG" ]; then |
| 12 | set -x |
| 13 | fi |
| 14 | |
| 15 | usage() { |
| 16 | cat <<EOF |
| 17 | Utility script collecting various information about ONAP deployment on kubernetes. |
| 18 | |
| 19 | Usage: $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} |
| 28 | EOF |
| 29 | } |
| 30 | |
| 31 | call_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 | |
| 42 | collect_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 | |
| 62 | collect_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 | |
| 84 | while 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 |
| 122 | done |
| 123 | |
| 124 | if [ -z "$NS" ]; then |
| 125 | usage |
| 126 | exit 1 |
| 127 | fi |
| 128 | |
| 129 | if [[ -z $OUT_FILE ]]; then |
| 130 | OUT_FILE=$OUT_NAME.tgz |
| 131 | fi |
| 132 | |
| 133 | if [ ! -z "$APP" ]; then |
| 134 | _APPS=($APP) |
| 135 | else |
| 136 | _APPS=(`kubectl get namespaces | grep "^$NS-" | tr -s ' ' | cut -d' ' -n -f1 | sed -e "s/^$NS-//"`) |
| 137 | fi |
| 138 | |
| 139 | printf "Collecting information about ONAP deployment...\n" |
| 140 | printf "Components: %s\n" "${_APPS[*]}" |
| 141 | |
| 142 | # Collect common info |
| 143 | mkdir -p ${TMP_DIR}/${OUT_NAME}/ |
| 144 | echo "${_APPS[*]}" > ${TMP_DIR}/${OUT_NAME}/component-list.log |
| 145 | printf "Collecting Helm info\n" |
| 146 | call_with_log "helm version" "${TMP_DIR}/${OUT_NAME}/helm-version.log" |
| 147 | call_with_log "helm list" "${TMP_DIR}/${OUT_NAME}/helm-list.log" |
| 148 | |
| 149 | printf "Collecting Kubernetes info\n" |
| 150 | call_with_log "kubectl version" "${TMP_DIR}/${OUT_NAME}/k8s-version.log" |
| 151 | call_with_log "kubectl get nodes -o=wide" "${TMP_DIR}/${OUT_NAME}/k8s-nodes.log" |
| 152 | call_with_log "kubectl cluster-info" "${TMP_DIR}/${OUT_NAME}/k8s-cluster-info.log" |
| 153 | call_with_log "kubectl cluster-info dump" "${TMP_DIR}/${OUT_NAME}/k8s-cluster-info-dump.log" |
| 154 | call_with_log "kubectl top node" "${TMP_DIR}/${OUT_NAME}/k8s-top-node.log" |
| 155 | |
| 156 | # Collect per-component info |
| 157 | for i in ${_APPS[@]}; do |
| 158 | printf "Writing Kubernetes info of component $i\n" |
| 159 | collect_ns_info "$NS-$i" "${TMP_DIR}/${OUT_NAME}" |
| 160 | done |
| 161 | |
| 162 | # Pack and cleanup |
| 163 | mkdir -p ${OUT_DIR} |
| 164 | _OUT_DIR=`readlink -e ${OUT_DIR}` |
| 165 | printf "Packing output to ${_OUT_DIR}/${OUT_FILE}...\n" |
| 166 | cd ${TMP_DIR} |
| 167 | tar cfz ${_OUT_DIR}/${OUT_FILE} ${OUT_NAME} |
| 168 | cd - |
| 169 | printf "Cleaning up...\n" |
| 170 | rm -rf ${TMP_DIR}/${OUT_NAME} |
| 171 | printf "Done\n" |