Skip to content

Instantly share code, notes, and snippets.

@david-zw-liu
Last active April 15, 2019 03:34
Show Gist options
  • Select an option

  • Save david-zw-liu/c7b9c257e586f06dd5f1b29ab038ecb2 to your computer and use it in GitHub Desktop.

Select an option

Save david-zw-liu/c7b9c257e586f06dd5f1b29ab038ecb2 to your computer and use it in GitHub Desktop.

Revisions

  1. David Liu revised this gist Apr 15, 2019. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion docker-stats.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,12 @@
    #!/usr/bin/env ruby
    require 'open3'

    class String
    def truncate(max, omission: '')
    length > max ? "#{self[0...max - omission.length]}#{omission}" : self
    end
    end

    COLUMN_WIDTH = 30
    id2name = {}
    header = 'IMAGE NAME'.ljust(COLUMN_WIDTH).freeze
    @@ -21,7 +27,9 @@
    if matched
    container_id = matched[:container_id]
    image_name = id2name[container_id] || "Not Found"
    puts "#{image_name.ljust(COLUMN_WIDTH)}#{line}"
    optimized_image_name = image_name.truncate(COLUMN_WIDTH - 1, omission: '...').ljust(COLUMN_WIDTH)

    puts "#{optimized_image_name}#{line}"
    else
    line.gsub!('CONTAINER ID', "#{header}CONTAINER ID")
    puts line
  2. David Liu revised this gist Apr 15, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions docker-stats.rb
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,9 @@
    header = 'IMAGE NAME'.ljust(COLUMN_WIDTH).freeze
    Thread.new do
    while true
    _, stdout2 = Open3.popen2('docker ps --format "{{.ID}}\t{{.Image}}"')
    stdout2.each_line do |line2|
    container_id, image_name = line2.chomp.split("\t")
    _, stdout = Open3.popen2('docker ps --format "{{.ID}}\t{{.Image}}"')
    stdout.each_line do |line|
    container_id, image_name = line.chomp.split("\t")
    id2name[container_id] = image_name
    end
    sleep 0.5
  3. David Liu created this gist Apr 15, 2019.
    30 changes: 30 additions & 0 deletions docker-stats.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/usr/bin/env ruby
    require 'open3'

    COLUMN_WIDTH = 30
    id2name = {}
    header = 'IMAGE NAME'.ljust(COLUMN_WIDTH).freeze
    Thread.new do
    while true
    _, stdout2 = Open3.popen2('docker ps --format "{{.ID}}\t{{.Image}}"')
    stdout2.each_line do |line2|
    container_id, image_name = line2.chomp.split("\t")
    id2name[container_id] = image_name
    end
    sleep 0.5
    end
    end

    Open3.popen3('docker stats') do |stdin, stdout, stderr, wait_thr|
    stdout.each_line do |line|
    matched = line.to_s.match(/^(?<container_id>[a-z0-9]+)\s+/)
    if matched
    container_id = matched[:container_id]
    image_name = id2name[container_id] || "Not Found"
    puts "#{image_name.ljust(COLUMN_WIDTH)}#{line}"
    else
    line.gsub!('CONTAINER ID', "#{header}CONTAINER ID")
    puts line
    end
    end
    end