Test FTC100 fails since A1-SIM update
[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 STARTTIME=-1
35
36 if [ $# -lt 4 ]; then
37   print_usage
38   exit 1
39 fi
40 if [ $1 == "DOCKER" ]; then
41   STAT_TYPE=$1
42   shift
43   STARTTIME=$1
44   shift
45   LOGFILE=$1
46   shift
47   if [ $(($#%2)) -ne 0 ]; then
48     print_usage
49     exit 1
50   fi
51 elif [ $1 == "KUBE" ]; then
52   STAT_TYPE=$1
53   shift
54   STARTTIME=$1
55   shift
56   LOGFILE=$1
57   shift
58   if [ $(($#%3)) -ne 0 ]; then
59     print_usage
60     exit 1
61   fi
62 else
63   print_usage
64   exit 1
65 fi
66
67
68 echo "Name;Time;PIDS;CPU perc;Mem perc" > $LOGFILE
69
70 if [ "$STARTTIME" -ne -1 ]; then
71     STARTTIME=$(($SECONDS-$STARTTIME))
72 fi
73
74 while [ true ]; do
75   docker stats --no-stream --format "table {{.Name}};{{.PIDs}};{{.CPUPerc}};{{.MemPerc}}" > tmp/.tmp_stat_out.txt
76   if [ "$STARTTIME" -eq -1 ]; then
77     STARTTIME=$SECONDS
78   fi
79   CTIME=$(($SECONDS-$STARTTIME))
80
81   TMP_APPS=""
82
83   while read -r line; do
84     APP_LIST=(${@})
85     if [ $STAT_TYPE == "DOCKER" ]; then
86       for ((i=0; i<$#; i=i+2)); do
87         SAPP=${APP_LIST[$i]}
88         APP=${APP_LIST[$i+1]}
89         d=$(echo $line | grep -v "k8s" | grep $APP)
90         if [ ! -z $d ]; then
91           d=$(echo $d | cut -d';' -f 2- | sed -e 's/%//g' | sed 's/\./,/g')
92           echo "$SAPP;$CTIME;$d" >> $LOGFILE
93           TMP_APPS=$TMP_APPS" $SAPP "
94         fi
95       done
96     else
97       for ((i=0; i<$#; i=i+3)); do
98         SAPP=${APP_LIST[$i]}
99         APP=${APP_LIST[$i+1]}
100         NS=${APP_LIST[$i+2]}
101         d=$(echo "$line" | grep -v "k8s_POD" | grep "k8s" | grep $APP | grep $NS)
102         if [ ! -z "$d" ]; then
103           d=$(echo "$d" | cut -d';' -f 2- | sed -e 's/%//g' | sed 's/\./,/g')
104           data="$SAPP-$NS;$CTIME;$d"
105           echo $data >> $LOGFILE
106           TMP_APPS=$TMP_APPS" $SAPP-$NS "
107         fi
108       done
109     fi
110   done < tmp/.tmp_stat_out.txt
111
112   APP_LIST=(${@})
113   if [ $STAT_TYPE == "DOCKER" ]; then
114     for ((i=0; i<$#; i=i+2)); do
115       SAPP=${APP_LIST[$i]}
116       APP=${APP_LIST[$i+1]}
117       if [[ $TMP_APPS != *" $SAPP "* ]]; then
118         data="$SAPP;$CTIME;0;0,00;0,00"
119         echo $data >> $LOGFILE
120       fi
121     done
122   else
123     for ((i=0; i<$#; i=i+3)); do
124       SAPP=${APP_LIST[$i]}
125       APP=${APP_LIST[$i+1]}
126       NS=${APP_LIST[$i+2]}
127       if [[ $TMP_APPS != *" $SAPP-$NS "* ]]; then
128         data="$SAPP-$NS;$CTIME;0;0,00;0,00"
129         echo $data >> $LOGFILE
130       fi
131     done
132   fi
133   sleep 1
134 done