$ 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
$ 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%
#!/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