Sha256: e0c2446735e375bdbf227cda4ba7243f7ccdd541d50fe2a0d1ddceda8e213f66

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'rest-client'

module Fluent
  class Dockerid2Name < Filter
    Fluent::Plugin.register_filter('docker_image', 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-dockerid2name-0.0.1 lib/fluent/plugin/filter_dockerid2name.rb