Skip to content

Instantly share code, notes, and snippets.

@holysugar
Last active December 15, 2015 15:39
Show Gist options
  • Select an option

  • Save holysugar/5283773 to your computer and use it in GitHub Desktop.

Select an option

Save holysugar/5283773 to your computer and use it in GitHub Desktop.
dstat -> growthforecast にするにあたり map プラグイン使いたくなかったのでフィルタっぽいのを書く.out_exec_filter でやる方がいいかもしれんけど.
# coding: utf-8
# dstat プラグインの出力を growthforecast 向けにデータ整形する
#
# 設定例:
# <match dstat>
# type dstat_format
# tag_format server.${hostname}.dstat
# </match>
#
class DstatFormatOutput < Fluent::Output
Fluent::Plugin.register_output('dstat_format', self)
config_param :tag_format, :default => 'dstat.${hostname}'
config_param :key_format, :default => '${type}.${name}'
# 好みで設定する
config_param :keys, :default => "
cpu.usr cpu.sys
dsk.read dsk.writ
net.recv net.send
mem.used mem.buff mem.cach
"
config_param :additional_keys, :default => nil
def configure(conf)
super
raw_keys = keys.split
raw_keys += additional_keys.split if additional_keys
@output_keys = raw_keys.map{|key| key.split(/\W+/) }
@tag_name = @tag_format.gsub('${hostname}', `hostname`.chomp)
end
def key_name(type, name)
@key_format.gsub('${type}', type).gsub('${name}', name)
end
def tag_name(hostname)
@tag_format.gsub('${hostname}', hostname)
end
def get_stat_type(full_stat_name)
# 必要に応じて修正
case full_stat_name
when /cpu usage/
'cpu'
else
full_stat_name[0,3]
end
end
def filter_dstat(dstat)
dstat.each_with_object({}){|(k, v), result|
stat_type = get_stat_type(k)
v.each{|name, value|
if @output_keys.include?([stat_type, name])
result[key_name(stat_type, name)] = value
end
}
}
end
def emit(tag, es, chain)
chain.next
es.each {|time,record|
filtered_dstat = filter_dstat(record["dstat"])
Fluent::Engine.emit(tag_name(record["hostname"]), Fluent::Engine.now, filtered_dstat)
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment