-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogContainerStats.sh
More file actions
executable file
·115 lines (98 loc) · 3.1 KB
/
Copy pathlogContainerStats.sh
File metadata and controls
executable file
·115 lines (98 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
# check arguments
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <container name>" >&2
exit 1
fi
# check if container exists
if [ ! "$(docker ps -a | grep $1)" ]; then
echo "Container not found, please check if name $1 is correct"
exit 1
fi
if [ -t 0 ]; then
SAVED_STTY="`stty --save`"
stty -echo -icanon -icrnl time 0 min 0
fi
# variables
count=0
keypress=''
peakMem=0
peakCPU=0
peakMemU=0
meanMem=0
meanCPU=0
meanMemU=0
curMem=0
curCPU=0
curMemU=0
prevMemU=0
# create log file
mkdir -p logs
rm -f logs/statscontainer_$1.log
touch logs/statscontainer_$1.log
echo "Starting loop, press any key to stop"
while [ "x$keypress" = "x" ]; do
# loop every second
sleep 1
# get stats
if [ "$( docker container inspect -f '{{.State.Status}}' $1 )" == "running" ]; then
stats=$(docker stats --no-stream --format "{{ json . }}" $1)
curMem=$( echo "$stats" | jq .MemPerc)
curCPU=$( echo "$stats" | jq .CPUPerc)
curMemU=$( echo "$stats" | jq .MemUsage)
else
# if container not running, break while loop
echo "container not found running, quitting"
break
fi
# get peak and mean mem perc
tmp=${curMem#*\"}
curMem=${tmp%\%*}
if [ $(bc <<< "$curMem > $peakMem") -gt 0 ]; then
peakMem=$curMem
#echo "new peak mem: $curMem"
fi
# get peak and mean mem usage
tmp=${curMemU#*\"}
curMemU=${tmp%%G*}
curMemUMB=${curMemU%%M*} # in case of Mib memory (should probably also check for Kib)
if [[ ${#curMemU} != ${#curMemUMB} ]]; then
curMemU=$(bc -l <<< "$curMemUMB/1000")
fi
if [ $(bc <<< "$curMemU > $peakMemU") -gt 0 ]; then
peakMemU=$curMemU
#echo "new peak mem usage: $curMemU"
fi
# also update log file with time and number of iterations if mem usage changed
if [ "$curMemU" != "$prevMemu" ]; then
# write time and iteration
echo "$(date +[%Y.%m.%d\|%H:%M:%S]) - iteration ${count}" >> logs/statscontainer_$1.log
# also write full stats to log file
docker stats --no-stream $1 >> logs/statscontainer_$1.log
prevMemU=$curMemU
fi
# get peak and mean cpu perc
tmp=${curCPU#*\"}
curCPU=${tmp%\%*}
if [ $(bc <<< "$curCPU > $peakCPU") -gt 0 ]; then
peakCPU=$curCPU
#echo "new peak cpu: $curCPU"
fi
#update means (sum up)
meanMem=$(bc<<<"$meanMem + $curMem")
meanCPU=$(bc<<<"$meanCPU + $curCPU")
meanMemU=$(bc<<<"$meanMemU + $curMemU")
let count+=1
keypress="`cat -v`"
done
# print out stats and log after keypress
echo "STATS:" | tee -a logs/statscontainer_$1.log
echo "Mean memory percentage: $(bc -l <<< "$meanMem/$count") %" | tee -a logs/statscontainer_$1.log
echo "Peak memory percentage: $peakMem %" | tee -a logs/statscontainer_$1.log
echo "Mean memory usage: $(bc -l <<< "$meanMemU/$count") GiB" | tee -a logs/statscontainer_$1.log
echo "Peak memory usage: $peakMemU GiB" | tee -a logs/statscontainer_$1.log
echo "Mean CPU percentage: $(bc -l <<< "$meanCPU/$count") %" | tee -a logs/statscontainer_$1.log
echo "Peak CPU percentage: $peakCPU %" | tee -a logs/statscontainer_$1.log
echo "Full log file with time more stats can be found at logs/statscontainer_$1.log"
if [ -t 0 ]; then stty "$SAVED_STTY"; fi
exit 0