Skip to content

Instantly share code, notes, and snippets.

@pocari
Last active April 9, 2018 09:25
Show Gist options
  • Save pocari/edd693b9c1ab09c4bed4b6451d9f47cd to your computer and use it in GitHub Desktop.
Save pocari/edd693b9c1ab09c4bed4b6451d9f47cd to your computer and use it in GitHub Desktop.
docker statsの結果を整形
require 'bigdecimal'
# while true; do TZ="Asia/Tokyo" ruby check_stats.rb; sleep 3; done > stat_log.txt
cmd = <<EOS
sudo docker stats --no-stream --format '{{.Name}}\t{{.ID | printf "%.10s" }}\t{{.CPUPerc}}\t{{.MemUsage}}'
EOS
def to_mib(str)
val = BigDecimal.new(str[0..-3])
if str.end_with?('GiB')
val * 1000
else
val
end
end
def memory_info(str)
current, max = str.split(/\//).map(&:strip)
[to_mib(current).to_f, to_mib(max).to_f]
end
now = Time.now.strftime('%Y-%m-%d %H:%M:%S')
`#{cmd}`.lines.map(&:chomp).each do |line|
name, id, cpu, mem = line.split(/\t/)
cpu = cpu[0..-1].to_f
mem_use, mem_total = memory_info(mem)
puts [now, name, id, cpu, mem_use, mem_total].join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment