Skip to content

Instantly share code, notes, and snippets.

@dmonllao
Created July 26, 2019 14:43
Show Gist options
  • Save dmonllao/9293b98f9ef3b16a118f12d3858c55d3 to your computer and use it in GitHub Desktop.
Save dmonllao/9293b98f9ef3b16a118f12d3858c55d3 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# Interval in seconds.
INTERVAL="1"
TIMEFORMAT="%d-%m-%y,%H:%M:%S"
echo "Monitor the memory usage of a running docker container (memory in MBs)"
example="./monitor_docker_memory_stat.sh ccec200a349403170ba088650a6a664f35ef895b6bc734de89eacb887a1ffde6 rss"
if [ -z $1 ];
then
echo "Error: No container id provided. E.g. $example"
exit 1
fi
if [ -z $2 ];
then
echo "Error: No memory variable name provided. E.g. $example"
exit 1
fi
containerid=$1
statname=$2
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [ ! -d "$dir/data" ];
then
mkdir "$dir/data"
fi
filename=$statname.data
file="$dir/data/$filename"
plotfile=$dir'/'$statname'plot.gn'
cat > "$plotfile" <<EOL
set title "$statname value"
set xdata time
set timefmt "$TIMEFORMAT"
set xtics format "$TIMEFORMAT"
plot [:][:] 'data/$filename' using 1:2 with line
while (1) {
replot
pause 1
}
EOL
if [ -f $file ];
then
rm "$file"
fi
touch "$file"
i=0
while true; do
sudo cat /sys/fs/cgroup/memory/docker/$containerid/memory.stat | grep "^$statname " | { date +$TIMEFORMAT & grep "[0-9]*$" --only-matching | awk '{print $1/1024/1024}'; } | xargs echo >> $file
if [ $i -eq 10 ]; then
echo 'gets in'
gnuplot $plotfile &
fi
i=$((i+1))
sleep $INTERVAL
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment