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