[APACHE] Add Apache CNF use case files
[demo.git] / tutorials / ApacheCNF / templates / tools / tail_all.sh
1 #!/bin/bash
2 #   COPYRIGHT NOTICE STARTS HERE
3
4 #   Copyright 2020 . Samsung Electronics Co., Ltd.
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17
18 #   COPYRIGHT NOTICE ENDS HERE
19
20 set -e
21
22 get_pod_name() {
23     kubectl -n ${NAMESPACE} get pod -l app="$1" -o jsonpath='{ .items[0].metadata.name }'
24 }
25
26 exec_in_pod() {
27     local pod="$1"
28     local container="$2"
29     shift 2
30     local cmd="$@"
31     kubectl --namespace ${NAMESPACE} exec ${pod} --container ${container} -- sh -c "${cmd}"
32 }
33
34 pod_logs() {
35     local pod="$1"
36     local container="$2"
37     local options="$3"
38     shift 3
39     kubectl --namespace ${NAMESPACE} logs ${pod} --container ${container} ${options:+"${options}"}
40 }
41
42 k8s_tail() {
43     local operation="$1"
44     local file_to_tail="$2"
45     local pod_label="$3"
46     local container="$4"
47     if [ -z ${container} ]; then
48         container=${pod_label}
49     fi
50     mkdir -p ${CACHE_FILE_DIR}
51     local pod=$(get_pod_name ${pod_label})
52     file_path_savable_form="$(sed 's#/#__#g' <<<"$file_to_tail")"
53     LINECOUNT_CACHE_FILE=${CACHE_FILE_DIR}/${pod}-${container}-${file_path_savable_form}
54     local line_count=0
55     if [ "${operation}" == "start" ]; then
56         if [ "${file_to_tail}" == "POD_LOG" ]; then
57             line_count=$(pod_logs ${pod} ${container} "" | wc -l)
58         else
59             if ! line_count=$(exec_in_pod ${pod} ${container} "wc -l ${file_to_tail}"); then
60                 echo "Failed to get file ${file_to_tail} line count, maybe it does not exist. Using linecount 0." >&2
61                 line_count=0
62             else
63                 # parse out linecount from wc -l output
64                 line_count=$(echo -e "${line_count}" | tail -1 | cut -d' ' -f1)
65             fi
66         fi
67         echo "${line_count}" > ${LINECOUNT_CACHE_FILE}
68         echo "Saved file ${file_to_tail} of POD ${pod_label} linecount ${line_count} into file ${LINECOUNT_CACHE_FILE}"
69     fi
70     if [ "${operation}" == "collect" ]; then
71         if [ ! -f ${LINECOUNT_CACHE_FILE} ]; then
72             echo "Linecount cache file ${LINECOUNT_CACHE_FILE} not found. Either tailing was not started or POD has restarted. Colected from log beginning." >&2
73             start_line=0
74         else
75             start_line=$(cat ${LINECOUNT_CACHE_FILE})
76         fi
77         echo "Tail pod's ${pod_label} container ${container} file ${file_to_tail} starting from line ${start_line} onwards" >&2
78         if [ "${file_to_tail}" == "POD_LOG" ]; then
79             current_line_count=$(pod_logs ${pod} ${container} "" | wc -l)
80             lines_from_end=$(( ${current_line_count}-${start_line} ))
81             echo "Execute kubectl logs for ${pod} POD's ${container} container. Log ${lines_from_end} lines from end"
82             pod_logs ${pod} ${container} --tail=${lines_from_end}
83         else
84             echo "Execute cmd "tail -n +${start_line} ${file_to_tail}" inside ${pod} POD's ${container} container"
85             exec_in_pod ${pod} ${container} "tail -n +${start_line} ${file_to_tail}"
86         fi
87     fi
88 }
89
90 tail_it() {
91     local operation=$1
92     for tail in "${TAILS[@]}"
93     do
94         tailarray=(${tail})
95         result=$(k8s_tail ${operation} ${tailarray[@]})
96         if [ "${operation}" == "collect" ]; then
97             mkdir -p ${RESULT_DIR}
98             log_file=$(basename ${tailarray[0]})
99             if [ "${log_file}" == "POD_LOG" ]; then
100                log_file=${log_file}.log
101             fi
102             pod=${tailarray[1]}
103             container="${pod:-tailarray[2]}"
104             out_file=${RESULT_DIR}/${pod}_${container}_${log_file}
105             echo -e "${result}" > ${out_file}
106             echo "Saved tail content to log file ${out_file}"
107         else
108             echo -e "${result}"
109         fi
110     done
111 }
112
113
114 ##### MAIN #####
115 if [ -f ./tail_config ]; then
116 . ./tail_config
117 fi
118 NAMESPACE=${NAMESPACE:-onap}
119 CACHE_FILE_DIR=${CACHE_FILE_DIR:-.k8s_tail}
120 RESULT_DIR=${RESULT_DIR:-./tail_results}
121 DEFAULT_TAILS=()
122 TAILS=("${TAILS[@]:-${DEFAULT_TAILS[@]}}")
123
124 case "$1" in
125     -h|--help|help|?|"")
126         echo "Script usage:"
127         echo "$0 start   - Start pods' log tailing"
128         echo "$0 collect - Collect all logs currently tailed or get all logs as whole"
129         echo ""
130         echo "If start is not called before collect is called, collect gathers all logs completely. "
131         echo "Otherwise logs are collected only from the linecount of the time of start call."
132         echo ""
133         echo "Log files collected is configured with 'tail_config' file in the same directory with the script."
134         echo "Config file needs to fontain bash array variable named TAILS=() and format of each entry in array is"
135         echo "    <file path> <POD's app label name> [<container name>]"
136         echo "where <file path> is actual file path inside the pod or special string 'POD_LOG' that means kubectl logs"
137         echo "where optional <container name> is to specify POD's container if many containers in the pod. By default same name as pod is used."
138         echo ""
139         echo 'Example: TAILS=('
140         echo '  "/app/logs/apih/metrics.log so"'
141         echo '  "/app/logs/bpmn/debug.log so-bpmn-infra"'
142         echo '  "/app/logs/vnfm-adapter/debug.log so-vnfm-adapter"'
143         echo '  "/var/log/onap/sdnc/karaf.log sdnc"'
144         echo '  "/tmp/app.out network-name-gen"'
145         echo '  "POD_LOG cds-blueprints-processor"'
146         echo '  "POD_LOG multicloud multicloud"'
147         echo '  "POD_LOG multicloud-k8s multicloud-k8s"'
148         echo '  "POD_LOG multicloud-k8s framework-artifactbroker"'
149         echo '  "/app/logs/openstack/error.log so-openstack-adapter"'
150         echo '  "/app/logs/openstack/debug.log so-openstack-adapter"'
151         echo ' )'
152         echo ""
153         ;;
154     start|collect)
155         tail_it $1
156         ;;
157     *)
158         echo "Wrong usage, check '$0 -h'" >&2
159         exit 1
160         ;;
161 esac