How to find MAX memory from docker statistics?

With the help docker statsyou can see the memory usage of the container over time.

Is there a way to find what was the highest memory usage at startup docker stats?

+16
source share
4 answers

I took a sample script from here and aggregated @pl_rock data. But be careful - the command sortonly compares string values, so the results are usually wrong (but good for me). Also remember that docker sometimes reports incorrect numbers (i.e., more distributed memory than physical memory).

Here is the script:

#!/bin/bash

"$@" & # Run the given command line in the background.
pid=$!

echo "" > stats

while true; do
  sleep 1
  sample="$(ps -o rss= $pid 2> /dev/null)" || break

  docker stats --no-stream --format "{{.MemUsage}} {{.Name}} {{.Container}}" | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }' >> stats
done

for containerid in `awk '/.+/ { print $7 }' stats | sort | uniq`
do
    grep "$containerid" stats | sort -r -k3 | tail -n 1
done
+3
source

, .MemPerc ( , ). .MemUsage , , .

docker stats --format 'CPU: {{.CPUPerc}}\tMEM: {{.MemPerc}}'

( ).

:

(timeout 120 docker stats --format '{{.MemPerc}}' <CONTAINER_ID> \
  | sed 's/\x1b\[[0-9;]*[a-zA-Z]//g' ; echo) \
  | tr -d '%' | sort -k1,1n | tail -n 1

( , , ) :

awk '/MemTotal/ {print $2}' /proc/meminfo

, -, , docker stats , , , .

...

/:

(timeout 20 docker stats --format \
  'CPU: {{.CPUPerc}}\tMEM: {{.MemPerc}}' <CONTAINER_ID> \
  | sed 's/\x1b\[[0-9;]*[a-zA-Z]//g' ; echo) \
  | gzip -c > monitor.log.gz

, pipe gzip. ~ 2 , , .

,

+2

:

docker stats --no-stream | awk '{ print $3 }' | sed '1d'|sort | tail -1

.

:

 --no-stream :          Disable streaming stats and only pull the first result
 awk '{ print $3 }' :   will print MEM USAGE
 sed '1d' :             will delete first entry that is %
 sort :                 it will sort the result
 tail -1 :              it will give last entry that is highest. 
0

-, -. , javascript , .

, .

:

CONTAINER=$(docker ps -q -f name=CONTAINER_NAME)
FORMAT='{{.MemPerc}}\t{{.MemUsage}}\t{{.Name}}'

docker stats --format $FORMAT $CONTAINER | sed -u 's/\x1b\[[0-9;]*[a-zA-Z]//g' | tee stats

:

  • CONTAINER=$(docker ps -q -f name=NAME) # ,
  • FORMAT='{{.MemPerc}} ...}} # MemPerc ( );
  • sed -u # -u ,
  • | sed -u 's/\x1b\[[0-9;]*[a-zA-Z]//g' # escape- ANSI
  • | tee stats # ,
  • Ctrl-C , - ,
  • - sort -n stats | tail
0

Source: https://habr.com/ru/post/1015753/


All Articles