2 # COPYRIGHT NOTICE STARTS HERE
4 # Copyright 2020 . Samsung Electronics Co., Ltd.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # COPYRIGHT NOTICE ENDS HERE
23 kubectl -n ${NAMESPACE} get pod -l app="$1" -o jsonpath='{ .items[0].metadata.name }'
31 kubectl --namespace ${NAMESPACE} exec ${pod} --container ${container} -- sh -c "${cmd}"
39 kubectl --namespace ${NAMESPACE} logs ${pod} --container ${container} ${options:+"${options}"}
44 local file_to_tail="$2"
47 if [ -z ${container} ]; then
48 container=${pod_label}
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}
55 if [ "${operation}" == "start" ]; then
56 if [ "${file_to_tail}" == "POD_LOG" ]; then
57 line_count=$(pod_logs ${pod} ${container} "" | wc -l)
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
63 # parse out linecount from wc -l output
64 line_count=$(echo -e "${line_count}" | tail -1 | cut -d' ' -f1)
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}"
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
75 start_line=$(cat ${LINECOUNT_CACHE_FILE})
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}
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}"
92 for tail in "${TAILS[@]}"
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
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}"
115 if [ -f ./tail_config ]; then
118 NAMESPACE=${NAMESPACE:-onap}
119 CACHE_FILE_DIR=${CACHE_FILE_DIR:-.k8s_tail}
120 RESULT_DIR=${RESULT_DIR:-./tail_results}
122 TAILS=("${TAILS[@]:-${DEFAULT_TAILS[@]}}")
127 echo "$0 start - Start pods' log tailing"
128 echo "$0 collect - Collect all logs currently tailed or get all logs as whole"
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."
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."
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"'
158 echo "Wrong usage, check '$0 -h'" >&2