CI / CD Docker container monitoring

Display the resource consumption status of the running container

$ docker stats --no-stream
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
a5ed2facaadd        webcentos           0.00%               2.398MiB / 12.37GiB   0.02%               1.31kB / 0B         0B / 0B             2
e3a96695d263        docker_mysql_1      0.12%               187.5MiB / 12.37GiB   1.48%               1.05kB / 0B         0B / 0B             27
cc761f675b3b        docker_nginx_1      0.00%               4.309MiB / 12.37GiB   0.03%               956B / 0B           0B / 0B             2

Format and display

$ docker stats --no-stream --format "{{.Name}},{{.MemPerc}},{{.CPUPerc}}"   #Container name,Memory usage,CPU usage
webcentos,0.02%,0.00%
docker_mysql_1,1.48%,0.12%
docker_nginx_1,0.03%,0.00%

Monitoring batch

Run every minute and skip notification message when usage threshold (80%) is exceeded

#!/bin/bash

#Threshold(%)
LIMIT=90

#Get STATS for container
CHECK_STATS=$(docker stats --no-stream --format "{{.Name}},{{.MemPerc}},{{.CPUPerc}}") #Container name,Memory usage,CPU usage

while read row; do
  name=`echo ${row} | cut -d , -f 1`     #Container name
  memPer=`echo ${row} | cut -d , -f 2`   #Memory usage
  memPer_i=`echo ${memPer%%.*}`
  cpuPer=`echo ${row} | cut -d , -f 3`   #CPU usage
  cpuPer_i=`echo ${cpuPer%%.*}`
  
  #Memory usage threshold check
  if [ $memPer_i -ge $LIMIT ]; then
     message="Container:${name} Memory alert ${memPer}"
     curl -X POST --data-urlencode 'payload={"channel": "#alert", "username": "admin", "text": "${message}"}' <webhook URL>
  fi

  #CPU usage threshold check
  if [ $cpuPer_i -ge $LIMIT ]; then
     message="Container:${name} CPU alert ${cpuPer}"
     curl -X POST --data-urlencode 'payload={"channel": "#alert", "username": "admin", "text": "${message}"}' <webhook URL>
  fi
done < echo "$CHECK_STATS"

Recommended Posts

CI / CD Docker container monitoring
Small Docker container
Docker container usage scene
Access and debug Circle CI Docker container with ssh
Introducing "" Monitoring Tool "" for monitoring Docker
Athrill's Docker container minimization procedure
Introduction to Linux Container / Docker (Part 1)
Run React on a Docker container
Run GUI application on Docker container
Introduction to Linux Container / Docker (Part 2)
Run PureScript on a Docker container
Pass environment variables to docker container
[Docker] Operation up to container creation # 2
docker single container restart for myself
[Linux] Start Apache container with Docker
Build WebRTC Janus with Docker container
About Docker, disguise server and container