Skip to content

Instantly share code, notes, and snippets.

@tom-henderson
Created November 5, 2020 19:12
Show Gist options
  • Select an option

  • Save tom-henderson/b65f59018700f4a3526f38bf2760b963 to your computer and use it in GitHub Desktop.

Select an option

Save tom-henderson/b65f59018700f4a3526f38bf2760b963 to your computer and use it in GitHub Desktop.

Revisions

  1. tom-henderson created this gist Nov 5, 2020.
    23 changes: 23 additions & 0 deletions docker-volume-metrics.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    #!/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