Created
November 5, 2020 19:12
-
-
Save tom-henderson/b65f59018700f4a3526f38bf2760b963 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# Write out prometheus metrics for docker volume size. | |
while read -r container_id; do | |
container=$(docker inspect "$container_id") | |
state=$(jq --raw-output .[].State.Status <(echo "$container")) | |
if [[ $state = 'running' ]]; then | |
name=$(jq --raw-output .[].Name <(echo "$container") | sed 's/\///g') | |
id=$(jq --raw-output .[].Id <(echo "$container") | sed 's/\///g') | |
while read -r volume_path; do | |
volume_name=$(echo "$volume_path" | sed 's/\/var\/lib\/docker\/volumes\///' | sed 's/\/_data//') | |
if [[ -n $volume_name ]]; then | |
size=$(sudo du -sb "$volume_path" | cut -f 1) | |
echo "container_volume_fs_usage_bytes{container_id=\"${id}\",container_name=\"${name}\",volume_name=\"${volume_name}\",volume_path=\"${volume_path}\"} ${size}" | |
fi | |
done <<< "$(jq -r '.[].Mounts[] | select( .Type == "volume" ) | .Source' <(echo "$container"))" # Returns a list of volume paths | |
fi | |
done <<< "$(docker ps -aq)" # Returns a list of container IDs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment