Skip to content

Commit e3d6791

Browse files
committed
First commit
0 parents  commit e3d6791

File tree

7 files changed

+222
-0
lines changed

7 files changed

+222
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -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

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in fluent-plugin-cadvisor.gemspec
4+
gemspec

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -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.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -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

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "bundler/gem_tasks"

fluent-plugin-cadvisor.gemspec

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'fluent/plugin/cadvisor/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "fluent-plugin-cadvisor"
8+
spec.version = Fluent::Plugin::Cadvisor::VERSION
9+
spec.authors = ["Alex"]
10+
spec.email = ["al.gaspar@gmail.com"]
11+
spec.summary = %q{TODO: Write a short summary. Required.}
12+
spec.description = %q{TODO: Write a longer description. Optional.}
13+
spec.homepage = ""
14+
spec.license = "MIT"
15+
16+
spec.files = `git ls-files -z`.split("\x0")
17+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19+
spec.require_paths = ["lib"]
20+
21+
spec.add_development_dependency "bundler", "~> 1.5"
22+
spec.add_development_dependency "rake"
23+
end
+126
Original file line numberDiff line numberDiff line change
@@ -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+

0 commit comments

Comments
 (0)