以前紹介した記事(BashのHistoryをFluentdのDataCounterで集計してみた)にて集計したデータを
CloudWatchのカスタムメトリクスに登録し、次のようなAWSコンソールでのグラフ表示等の機能が
利用できるようにしてみました。
CloudWatchへのデータ登録は下記のFluentdのプラグインを作成して行なっています。
/etc/td-agent/plugin/out_cloudwatch.rb
module Fluent
require 'aws-sdk'
class CloudWatchOutput
Fluent::Plugin.register_output('cloudwatch', self)
include SetTagKeyMixin
config_set_default :include_tag_key, false
include SetTimeKeyMixin
config_set_default :include_time_key, true
config_param :aws_key_id, :string, :default => nil
config_param :aws_sec_key, :string, :default => nil
config_param :cloud_watch_endpoint, :string, :default => 'monitoring.ap-northeast-1.amazonaws.com'
config_param :namespace, :string
config_param :metric_name, :string
config_param :metric_data_key, :string
config_param :metric_unit, :string
def configure(conf)
super
end
def start
super
AWS.config(
:access_key_id => @aws_key_id,
:secret_access_key => @aws_sec_key,
:cloud_watch_endpoint => @cloud_watch_endpoint
)
@cloudwatch = AWS::CloudWatch.new
end
def shutdown
super
end
def format(tag, time, record)
record.to_msgpack
end
def write(chunk)
metric_data = []
chunk.msgpack_each do |record|
metric_data :metric_name => @metric_name,
:timestamp => record['time'],
:value => record[@metric_data_key],
:unit => @metric_unit
}
end
begin
@cloudwatch.put_metric_data(
:namespace => @namespace,
:metric_data => metric_data
)
rescue => e
$log.error(e)
end
end
end
end
設定ファイル(/etc/td-agent/td-agent.conf)は次の通りです。
type copy
type file
path /tmp/tail.syslog
type datacounter
unit minute
aggregate all
count_key ident
pattern1 history ^-bash$
tag datacounter.syslog
>
type copy
type file
path /tmp/datacounter.syslog
type cloudwatch
namespace SUZ-LAB/TEST
metric_name HistoryCount
metric_data_key history_count
metric_unit Count