Skip to content

Instantly share code, notes, and snippets.

@vikalpj
Last active February 23, 2018 09:23
Show Gist options
  • Save vikalpj/d2d0f5fefc3f7d7d9da1fbda3b6634b5 to your computer and use it in GitHub Desktop.
Save vikalpj/d2d0f5fefc3f7d7d9da1fbda3b6634b5 to your computer and use it in GitHub Desktop.
Clean up script for ecs containers when container using High memory
#!/bin/bash -e
##
# Use this annotated script as base for killing container misbehaving on reaching memory limit
#
# Requirements:
# - `jq` must be installed on both the client and server
##
# don't kill containers using these images even if they're misbehaving
EXCLUDES_PATTERN=$(cat <<'EOF' | xargs | sed 's/ /|/g'
amazon/amazon-ecs-agent
EOF
)
# list all the candidate containers
targets=$(docker ps --no-trunc --format '{{.ID}} {{.Image}}' | grep -Ev "$EXCLUDES_PATTERN" | awk '{ print $1; }' | xargs)
for target in $targets; do
# get taskid and dockerid from ecs
task=$(curl -s http://localhost:51678/v1/tasks?dockerid=$target)
taskId=$(echo $task | jq -r ".Arn" | cut -d "/" -f 2)
dockerId=$(echo $task | jq -r ".Containers[0] .DockerId")
memoryStatsFile="/cgroup/memory/ecs/$taskId/$dockerId/memory.stat"
# skip current target if cannot find memory stats file, might not be managed by ecs
if ! [ -f $memoryStatsFile ]
then echo "Memory stats not found for taskId=$taskid dockerId=$target" && continue
fi
info="id=$target $(docker inspect --format 'image={{.Config.Image}} StartedAt="{{.State.StartedAt}}"' "$target") pgmajfault=$(grep total_pgmajfault $memoryStatsFile | awk '{print $2;}')"
majorPageFaults=$(echo "$info" | awk '{ print $4;}' | sed 's/pgmajfault=//g')
startedAt=$(echo "$info" | awk '{ print $3;}' | sed 's/StartedAt=//g' | sed 's/"//g')
startedSecs=$(date -d $startedAt +%s)
stoppingSecs=$(date +%s)
duration=$(($stoppingSecs - $stoppingSecs))
duration="$(($duration / 60))m$(($duration % 60))s"
info=$(echo "duration=$duration $info")
if [ "$majorPageFaults" -gt 5000 ]; then
echo "Stopping Container: timestamp=$(date -Iseconds) [$info]"
docker stop "$target"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment