Class: Fluent::Plugin::DockerStatsInput
- Inherits:
-
Input
- Object
- Input
- Fluent::Plugin::DockerStatsInput
- Defined in:
- lib/fluent/plugin/in_docker_stats.rb
Defined Under Namespace
Classes: TimerWatcher
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #emit_container_stats(container_id) ⇒ Object
- #get_metrics ⇒ Object
-
#initialize ⇒ DockerStatsInput
constructor
A new instance of DockerStatsInput.
- #list_container_ids ⇒ Object
- #run ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ DockerStatsInput
Returns a new instance of DockerStatsInput.
14 15 16 17 18 19 |
# File 'lib/fluent/plugin/in_docker_stats.rb', line 14 def initialize super puts "Found Docker details: #{Docker.version}" puts "Using interval: #{@stats_interval}" puts "Using tag: #{@tag}" end |
Instance Method Details
#configure(conf) ⇒ Object
21 22 23 |
# File 'lib/fluent/plugin/in_docker_stats.rb', line 21 def configure(conf) super end |
#emit_container_stats(container_id) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/fluent/plugin/in_docker_stats.rb', line 46 def emit_container_stats(container_id) container = Docker::Container.get(container_id) record = { "container_id": container_id, "container_name": container.info['Name'].sub(/^\//, ''), "created_time": container.info["Created"] } state = container.info['State'] record["status"] = state['Status'] record["is_running"] = state['Running'] record["is_restarting"] = state['Restarting'] record["is_paused"] = state['Paused'] record["is_oom_killed"] = state['OOMKilled'] record["started_time"] = state['StartedAt'] record["finished_time"] = state['FinishedAt'] stats = container.stats(stream: false) memory_stats = stats['memory_stats'] record["mem_usage"] = memory_stats['usage'] record["mem_limit"] = memory_stats['limit'] record["mem_max_usage"] = memory_stats['max_usage'] cpu_stats, = stats['cpu_stats'] cpu_usage = cpu_stats['cpu_usage'] cpu_system_usage = cpu_stats['system_cpu_usage'] cpu_total_usage = cpu_usage['total_usage'] cpu_percent = (cpu_total_usage.to_f / cpu_system_usage.to_f) * 100 record["cpu_system_usage"] = cpu_system_usage record["cpu_total_usage"] = cpu_total_usage record["cpu_percent"] = cpu_percent record["networks"] = [] stats['networks'].each do |network_name, network_info| record["networks"] << { "network_name": network_name, "rx": network_info['rx_bytes'], "tx": network_info['tx_bytes'], } end storage_stats = stats['storage_stats'] if stats['storage_stats'] && !stats['storage_stats'].empty? record["volumes"] = [] volume_stats = storage_stats['volumes'] volume_stats.each do |volume_name, volume_info| puts "Volume #{volume_name} - Used: #{volume_info['used']} bytes, Total: #{volume_info['total']} bytes" record["volumes"] << { "volume_name": volume_name, "volume_used": volume_info['used'], "volume_total": volume_info['total'], } end end router.emit(@tag, Fluent::Engine.now, record) end |
#get_metrics ⇒ Object
39 40 41 42 43 44 |
# File 'lib/fluent/plugin/in_docker_stats.rb', line 39 def get_metrics ids = @container_ids || list_container_ids ids.each do |container_id| emit_container_stats(container_id) end end |
#list_container_ids ⇒ Object
108 109 110 111 112 |
# File 'lib/fluent/plugin/in_docker_stats.rb', line 108 def list_container_ids Docker::Container.all.map do |container| container.id end end |
#run ⇒ Object
32 33 34 35 36 37 |
# File 'lib/fluent/plugin/in_docker_stats.rb', line 32 def run @loop.run rescue log.error "unexpected error", :error => $!.to_s log.error_backtrace end |
#shutdown ⇒ Object
114 115 116 117 |
# File 'lib/fluent/plugin/in_docker_stats.rb', line 114 def shutdown @loop.stop @thread.join end |
#start ⇒ Object
25 26 27 28 29 30 |
# File 'lib/fluent/plugin/in_docker_stats.rb', line 25 def start @loop = Coolio::Loop.new tw = TimerWatcher.new(@stats_interval, true, @log, &method(:get_metrics)) tw.attach(@loop) @thread = Thread.new(&method(:run)) end |