-
-
Save arbal/a58c0d63118dff11c52a42ed930eea5c to your computer and use it in GitHub Desktop.
Docker stats in CSV with separated values in bytes
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 | |
function cleanLine() { | |
local line key field | |
line="$1" | |
key="$2" | |
field="$3" | |
echo "$line" | jq -r "$key" | tr -d 'BbIi' | awk "{print toupper(\$$field)}" | numfmt --from=auto | |
} | |
function getFullCSV() { | |
local line out headersOut | |
headersOut=0 | |
lines=$(docker stats --no-stream --format "{{json .}}") | |
if [[ ${#lines} -gt 3 ]] ; then | |
while read -r line; do | |
out=$(echo "$line" | jq -r \ | |
--arg date "$(date +%s)" \ | |
--arg BlockIOIn "$(cleanLine "$line" '.BlockIO' 1)" \ | |
--arg BlockIOOut "$(cleanLine "$line" '.BlockIO' 3)" \ | |
--arg MemUsage "$(cleanLine "$line" '.MemUsage' 1)" \ | |
--arg MemLimit "$(cleanLine "$line" '.MemUsage' 3)" \ | |
--arg NetIOIn "$(cleanLine "$line" '.NetIO' 1)" \ | |
--arg NetIOOut "$(cleanLine "$line" '.NetIO' 3)" \ | |
'. + { BlockIOIn: $BlockIOIn, | |
BlockIOOut: $BlockIOOut, | |
MemUsage: $MemUsage, | |
MemLimit: $MemLimit, | |
NetIOIn: $NetIOIn, | |
NetIOOut: $NetIOOut, | |
date: $date } | |
| del(.BlockIO) | del(.NetIO) | |
| . as $input | keys as $cols | $cols, ($cols | map($input[.])) | @csv | |
') | |
if [[ "$headersOut" -eq 0 ]]; then | |
echo "$out" | |
headersOut=1 | |
else | |
echo "$out" | tail -n +2 | |
fi | |
done <<< "$lines" | |
else | |
return 1 | |
fi | |
} | |
function getValueLine() { | |
getFullCSV | tail -n +2 | |
} | |
# Run until we get a result, outputting headers and breaking out of the while | |
while true; do | |
getFullCSV && break | |
done | |
# Run forever, we don't care if the output fails as we've already outputted headers | |
while true; do | |
getValueLine || true | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment