class Fluent::Dockerid2Name

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/filter_docker_image_name.rb, line 7
def configure(conf)
  super

  @containerid_hash = Hash.new
end
filter_stream(tag, es) click to toggle source
# File lib/fluent/plugin/filter_docker_image_name.rb, line 49
def filter_stream(tag, es)
  new_es = MultiEventStream.new

  container_id = tag.match(/(\w{64})/)
  @containerid_hash[container_id] ||= get_appname(container_id)

  es.each {|time, record|
    record[:app_name] = @containerid_hash[container_id]
    new_es.add(time, record)
  }

  new_es
end
get_appname(container_id) click to toggle source
# File lib/fluent/plugin/filter_docker_image_name.rb, line 39
def get_appname(container_id)
  list_container_ids.each do |obj|
    if container_id.to_s == obj[:id].to_s
      return obj[:name]
    end
  end

  return nil
end
list_container_ids() click to toggle source
# File lib/fluent/plugin/filter_docker_image_name.rb, line 13
def list_container_ids
  socket_path = "/var/run/docker.sock"
  if File.exists?(socket_path)
    socket = Socket.unix(socket_path)
    socket.puts("GET /containers/json HTTP/1.0\n\r")

    res = socket.readlines
    socket.close

    # Find body position
    idx = -1
    res.to_a.each_with_index do | stats, index |
      if stats[0] == '['
        idx = index
        break;
      end
    end

    #Remove HTTP Headers and parse the body
    jsn = JSON.parse(res.to_a[idx..-1].join)
    jsn.collect { |obj| {:id => obj['Id'], :name => obj['Image']} }
  else
    []
  end
end