|
| 1 | +require 'rest_client' |
| 2 | +require 'time' |
| 3 | + |
| 4 | +class CadvisorInput < Fluent::Input |
| 5 | + class TimerWatcher < Coolio::TimerWatcher |
| 6 | + |
| 7 | + def initialize(interval, repeat, log, &callback) |
| 8 | + @callback = callback |
| 9 | + @log = log |
| 10 | + super(interval, repeat) |
| 11 | + end |
| 12 | + def on_timer |
| 13 | + @callback.call |
| 14 | + rescue |
| 15 | + @log.error $!.to_s |
| 16 | + @log.error_backtrace |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + Fluent::Plugin.register_input('cadvisor', self) |
| 21 | + |
| 22 | + config_param :host, :string, :default => 'localhost' |
| 23 | + config_param :port, :string, :default => 8080 |
| 24 | + config_param :api_version, :string, :default => '1.1' |
| 25 | + config_param :stats_interval, :time, :default => 10 # every minute |
| 26 | + config_param :tag_prefix, :string, :default => "metric" |
| 27 | + |
| 28 | + def initialize |
| 29 | + super |
| 30 | + require 'socket' |
| 31 | + @hostname = Socket.gethostname |
| 32 | + @dict = {} |
| 33 | + end |
| 34 | + |
| 35 | + def configure(conf) |
| 36 | + super |
| 37 | + end |
| 38 | + |
| 39 | + def start |
| 40 | + @cadvisorEP ||= "http://#{@host}:#{@port}/api/v#{@api_version}" |
| 41 | + @machine ||= get_spec |
| 42 | + |
| 43 | + @loop = Coolio::Loop.new |
| 44 | + tw = TimerWatcher.new(@stats_interval, true, @log, &method(:get_metrics)) |
| 45 | + tw.attach(@loop) |
| 46 | + @thread = Thread.new(&method(:run)) |
| 47 | + end |
| 48 | + |
| 49 | + def run |
| 50 | + @loop.run |
| 51 | + rescue |
| 52 | + log.error "unexpected error", :error=>$!.to_s |
| 53 | + log.error_backtrace |
| 54 | + end |
| 55 | + |
| 56 | + def get_spec |
| 57 | + response = RestClient.get(@cadvisorEP + "/machine") |
| 58 | + JSON.parse(response.body) |
| 59 | + end |
| 60 | + |
| 61 | + # Metrics collection methods |
| 62 | + def get_metrics |
| 63 | + list_container_ids.each do |obj| |
| 64 | + emit_container_info(obj) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def list_container_ids |
| 69 | + socket_path = "/var/run/docker.sock" |
| 70 | + if File.exists?(socket_path) |
| 71 | + socket = Socket.unix(socket_path) |
| 72 | + socket.puts("GET /containers/json HTTP/1.0\n\r") |
| 73 | + |
| 74 | + res = socket.readlines |
| 75 | + socket.close |
| 76 | + |
| 77 | + #Remove HTTP Headers and parse the body |
| 78 | + jsn = JSON.parse(res.to_a[5..-1].join) |
| 79 | + jsn.collect { |obj| {:id => obj['Id'], :name => obj['Image']} } |
| 80 | + else |
| 81 | + [] |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + def emit_container_info(obj) |
| 86 | + id = obj[:id] |
| 87 | + response = RestClient.get(@cadvisorEP + "/containers/docker/" + id) |
| 88 | + res = JSON.parse(response.body) |
| 89 | + |
| 90 | + # Set max memory |
| 91 | + memory_limit = @machine['memory_capacity'] < res['spec']['memory']['limit'] ? @machine['memory_capacity'] : res['spec']['memory']['limit'] |
| 92 | + |
| 93 | + prev = @dict[id] ||= 0 |
| 94 | + res['stats'].each do | stats | |
| 95 | + timestamp = Time.parse(stats['timestamp']).to_i |
| 96 | + break if timestamp < prev |
| 97 | + |
| 98 | + @dict[id] = timestamp |
| 99 | + |
| 100 | + record = { |
| 101 | + 'container_id' => id, |
| 102 | + 'image' => obj[:name], |
| 103 | + 'memory_current' => stats['memory']['usage'], |
| 104 | + 'memory_limit' => memory_limit, |
| 105 | + 'cpu_usage' => stats['cpu']['usage']['total'], |
| 106 | + 'cpu_num_cores' => stats['cpu']['usage']['per_cpu_usage'].count, |
| 107 | + 'network_rx_bytes' => stats['network']['rx_bytes'], |
| 108 | + 'network_rx_packets' => stats['network']['rx_packets'], |
| 109 | + 'network_rx_errors' => stats['network']['rx_errors'], |
| 110 | + 'network_rx_dropped' => stats['network']['rx_dropped'], |
| 111 | + 'network_tx_bytes' => stats['network']['tx_bytes'], |
| 112 | + 'network_tx_packets' => stats['network']['tx_packets'], |
| 113 | + 'network_tx_errors' => stats['network']['tx_errors'], |
| 114 | + 'network_tx_dropped' => stats['network']['tx_dropped'], |
| 115 | + } |
| 116 | + |
| 117 | + Fluent::Engine.emit("stats", timestamp, record) |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + def shutdown |
| 122 | + @loop.stop |
| 123 | + @thread.join |
| 124 | + end |
| 125 | +end |
| 126 | + |
0 commit comments