Kafka now works in kube for calls outside its namespace
[nonrtric.git] / test / common / genstat.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020 Nordix Foundation. All rights reserved.
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 #  ============LICENSE_END=================================================
18 #
19
20 # This script collects container statistics to a file. Data is separated with semicolon.
21 # Works for both docker container and kubernetes pods.
22 # Relies on 'docker stats' so will not work for other container runtimes.
23 # Used by the test env.
24
25 # args: docker <start-time-seconds> <log-file> <app-short-name> <app-name> [ <app-short-name> <app-name> ]*
26 # or
27 # args: kube <start-time-seconds> <log-file> <app-short-name> <app-name> <namespace> [ <app-short-name> <app-name> <namespace> ]*
28
29 print_usage() {
30   echo "Usage: genstat.sh DOCKER <start-time-seconds> <log-file> <app-short-name> <app-name> [ <app-short-name> <app-name> ]*"
31   echo "or"
32   echo "Usage: genstat.sh KUBE <start-time-seconds> <log-file> <app-short-name> <app-name> <namespace> [ <app-short-name> <app-name> <namespace> ]*"
33 }
34 DD=$@
35 STARTTIME=-1
36
37 if [ $# -lt 4 ]; then
38   print_usage
39   exit 1
40 fi
41 if [ $1 == "DOCKER" ]; then
42   STAT_TYPE=$1
43   shift
44   STARTTIME=$1
45   shift
46   LOGFILE=$1
47   shift
48   if [ $(($#%2)) -ne 0 ]; then
49     print_usage
50     exit 1
51   fi
52 elif [ $1 == "KUBE" ]; then
53   STAT_TYPE=$1
54   shift
55   STARTTIME=$1
56   shift
57   LOGFILE=$1
58   shift
59   if [ $(($#%3)) -ne 0 ]; then
60     print_usage
61     exit 1
62   fi
63 else
64   print_usage
65   exit 1
66 fi
67
68
69 echo "Name;Time;PIDS;CPU perc;Mem perc" > $LOGFILE
70 echo $DD >> $LOGFILE
71
72 if [ "$STARTTIME" -ne -1 ]; then
73     STARTTIME=$(($SECONDS-$STARTTIME))
74 fi
75
76 while [ true ]; do
77   docker stats --no-stream --format "table {{.Name}};{{.PIDs}};{{.CPUPerc}};{{.MemPerc}}" > tmp/.tmp_stat_out.txt
78   if [ "$STARTTIME" -eq -1 ]; then
79     STARTTIME=$SECONDS
80   fi
81   CTIME=$(($SECONDS-$STARTTIME))
82
83   TMP_APPS=""
84
85   while read -r line; do
86     APP_LIST=(${@})
87     if [ $STAT_TYPE == "DOCKER" ]; then
88       for ((i=0; i<$#; i=i+2)); do
89         SAPP=${APP_LIST[$i]}
90         APP=${APP_LIST[$i+1]}
91         d=$(echo $line | grep -v "k8s" | grep $APP)
92         if [ ! -z $d ]; then
93           d=$(echo $d | cut -d';' -f 2- | sed -e 's/%//g' | sed 's/\./,/g')
94           echo "$SAPP;$CTIME;$d" >> $LOGFILE
95           TMP_APPS=$TMP_APPS" $SAPP "
96         fi
97       done
98     else
99       for ((i=0; i<$#; i=i+3)); do
100         SAPP=${APP_LIST[$i]}
101         APP=${APP_LIST[$i+1]}
102         NS=${APP_LIST[$i+2]}
103         d=$(echo "$line" | grep -v "k8s_POD" | grep "k8s" | grep $APP | grep $NS)
104         if [ ! -z "$d" ]; then
105           d=$(echo "$d" | cut -d';' -f 2- | sed -e 's/%//g' | sed 's/\./,/g')
106           data="$SAPP-$NS;$CTIME;$d"
107           echo $data >> $LOGFILE
108           TMP_APPS=$TMP_APPS" $SAPP-$NS "
109         fi
110       done
111     fi
112   done < tmp/.tmp_stat_out.txt
113
114   APP_LIST=(${@})
115   if [ $STAT_TYPE == "DOCKER" ]; then
116     for ((i=0; i<$#; i=i+2)); do
117       SAPP=${APP_LIST[$i]}
118       APP=${APP_LIST[$i+1]}
119       if [[ $TMP_APPS != *" $SAPP "* ]]; then
120         data="$SAPP;$CTIME;0;0,00;0,00"
121         echo $data >> $LOGFILE
122       fi
123     done
124   else
125     for ((i=0; i<$#; i=i+3)); do
126       SAPP=${APP_LIST[$i]}
127       APP=${APP_LIST[$i+1]}
128       NS=${APP_LIST[$i+2]}
129       if [[ $TMP_APPS != *" $SAPP-$NS "* ]]; then
130         data="$SAPP-$NS;$CTIME;0;0,00;0,00"
131         echo $data >> $LOGFILE
132       fi
133     done
134   fi
135   sleep 1
136 done