require 'rest-client' module Fluent class Dockerid2Name < Filter Fluent::Plugin.register_filter('dockerid2name', self) def configure(conf) super @containerid_hash = Hash.new end 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 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 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 end end