Class: Fluent::Dockerid2Name

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_docker_image_name.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



7
8
9
10
11
# File 'lib/fluent/plugin/filter_docker_image_name.rb', line 7

def configure(conf)
  super

  @containerid_hash = Hash.new
end

#filter_stream(tag, es) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
# 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_idsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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