Created
June 26, 2014 11:51
-
-
Save a3no/09ec55d83cfa46db78af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Fluent | |
class DstatStdinInput < Input | |
Plugin.register_input('dstat_stdin', self) | |
def initialize | |
super | |
end | |
config_param :tag, :string | |
config_param :dstat_path, :string, :default => "dstat" | |
config_param :option, :string | |
config_param :delay, :integer, :default => 1 | |
config_param :hostname_command, :string, :default => "hostname" | |
class TimerWatcher < Coolio::TimerWatcher | |
def initialize(interval, repeat, &callback) | |
@callback = callback | |
super(interval, repeat) | |
end | |
def on_timer | |
@callback.call | |
rescue | |
# TODO log? | |
$log.error $!.to_s | |
$log.error_backtrace | |
end | |
end | |
def configure(conf) | |
super | |
@hostname = `#{@hostname_command}`.chomp! | |
end | |
def start | |
@loop = Coolio::Loop.new | |
@timer = TimerWatcher.new(@delay, true, &method(:on_timer)) | |
@loop.attach(@timer) | |
@thread = Thread.new(&method(:run)) | |
end | |
def shutdown | |
@loop.watchers.each {|w| w.detach } | |
@loop.stop | |
@thread.join | |
end | |
def run | |
@loop.run | |
rescue | |
$log.error "unexpected error", :error=>$!.to_s | |
$log.error_backtrace | |
end | |
def on_timer | |
command = "#{@dstat_path} #{@option} #{@delay} 1" | |
now = Engine.now | |
io = IO.popen(command, "r") | |
lines = io.readlines() | |
io.close | |
headers1 = lines[0].gsub("\u001B[7l", "").split() | |
headers2 = lines[1].split("|") | |
datas = lines[3].split("|") | |
data = Hash.new | |
headers1.each_with_index{|header, i| | |
child_headers = headers2[i].split() | |
child_datas = datas[i].split() | |
child_record = Hash.new | |
child_headers.each_with_index{|cheader, j| | |
child_record[cheader] = child_datas[j].to_f | |
} | |
data[strip_dash(header)] = child_record | |
} | |
record = { | |
'hostname' => @hostname, | |
'dstat' => data | |
} | |
Engine.emit(@tag, now, record) | |
end | |
def strip_dash(word) | |
word.gsub("-", " ").split().join("-") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment