fluent-plugin-cadvisor 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b6e2ae42ed5c77b0fe09a7027bd5b0dab8ed52f
4
+ data.tar.gz: 828067e414091f9f931a8af7b7f815cc1b3e4a71
5
+ SHA512:
6
+ metadata.gz: 33c3817d4d518bace9e48a3b281d8f66d8625391c2262dd47d2b64b835e0b1cf3f883fcb8f3bbf9f2cd4950b8713e41eab206e0fb2fcff4105a16122db504f7e
7
+ data.tar.gz: 471c52d87ddfbcbb508fa1f3f34835230c00249b9420c18eb6d0d7f85fed219fb4f784d59f99784519f388fc92ea1ed2128eb4f77540f46e37e5975ce78eab64
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-cadvisor.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Fluent::Plugin::Cadvisor
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fluent-plugin-cadvisor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fluent-plugin-cadvisor
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/fluent-plugin-cadvisor/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "fluent-plugin-cadvisor"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Woorank"]
9
+ spec.email = ["dev@woorank.com"]
10
+ spec.summary = "cadvisor input plugin for Fluent event collector"
11
+ spec.homepage = "https://github.com/Woorank/fluent-plugin-cadvisor"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.5"
20
+ spec.add_development_dependency "rake"
21
+ end
@@ -0,0 +1,126 @@
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
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-cadvisor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Woorank
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - dev@woorank.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - fluent-plugin-cadvisor.gemspec
54
+ - lib/fluent/plugin/fluent-plugun-cadvisor.rb
55
+ homepage: https://github.com/Woorank/fluent-plugin-cadvisor
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.0.14
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: cadvisor input plugin for Fluent event collector
79
+ test_files: []