Skip to content

Instantly share code, notes, and snippets.

@laudai
Forked from robsonke/checkDockerDisks.sh
Created May 1, 2018 12:59
Show Gist options
  • Save laudai/7eff83774beae893c21c04152699c5b3 to your computer and use it in GitHub Desktop.
Save laudai/7eff83774beae893c21c04152699c5b3 to your computer and use it in GitHub Desktop.
This Bash script will loop through all running docker containers on a host and list the disk usage per mount. In case it's breaching the 65%, it will email you.
#!/bin/bash
# get all running docker container names
containers=$(sudo docker ps | awk '{if(NR>1) print $NF}')
host=$(hostname)
# loop through all containers
for container in $containers
do
echo "Container: $container"
percentages=($(sudo docker exec $container /bin/sh -c "df -h | grep -vE '^Filesystem|shm|boot' | awk '{ print +\$5 }'"))
mounts=($(sudo docker exec $container /bin/sh -c "df -h | grep -vE '^Filesystem|shm|boot' | awk '{ print \$6 }'"))
for index in ${!mounts[*]}; do
echo "Mount ${mounts[index]}: ${percentages[index]}%"
if (( ${percentages[index]} > 70 )); then
message="[ERROR] At $host and Docker container $container the mount ${mounts[index]} is at ${percentages[index]}% of its disk space. Please check this."
echo $message
echo $message | mail -s "Docker container $container at $host is out of disk space" "r.sonke@maxxton.com"
fi
done
echo ================================
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment