diff --git a/app/controllers/concerns/setting_concern.rb b/app/controllers/concerns/setting_concern.rb
new file mode 100644
index 0000000..cf2984b
--- /dev/null
+++ b/app/controllers/concerns/setting_concern.rb
@@ -0,0 +1,34 @@
+module SettingConcern
+ extend ActiveSupport::Concern
+
+ included do
+ before_action :login_required
+ before_action :find_fluentd
+ end
+
+ def show
+ @setting = target_class.new(initial_params)
+ end
+
+ def finish
+ @setting = target_class.new(setting_params)
+ unless @setting.valid?
+ return render "show"
+ end
+
+ @fluentd.agent.config_append @setting.to_config
+ if @fluentd.agent.running?
+ unless @fluentd.agent.restart
+ @setting.errors.add(:base, @fluentd.agent.log_tail(1).first)
+ return render "show"
+ end
+ end
+ redirect_to daemon_setting_path(@fluentd)
+ end
+
+ private
+
+ def setting_params
+ params.require(target_class.to_s.underscore.gsub("/", "_")).permit(*target_class.const_get(:KEYS))
+ end
+end
diff --git a/app/controllers/fluentd/settings/in_syslog_controller.rb b/app/controllers/fluentd/settings/in_syslog_controller.rb
index 9f7035e..c3355de 100644
--- a/app/controllers/fluentd/settings/in_syslog_controller.rb
+++ b/app/controllers/fluentd/settings/in_syslog_controller.rb
@@ -1,34 +1,16 @@
class Fluentd::Settings::InSyslogController < ApplicationController
- before_action :login_required
- before_action :find_fluentd
-
- def show
- @setting = Fluentd::Setting::InSyslog.new({
- bind: "0.0.0.0",
- port: 5140,
- })
- end
-
- def finish
- @setting = Fluentd::Setting::InSyslog.new(setting_params)
- unless @setting.valid?
- return render "show"
- end
-
- @fluentd.agent.config_append @setting.to_config
- if @fluentd.agent.running?
- unless @fluentd.agent.restart
- @setting.errors.add(:base, @fluentd.agent.log_tail(1).first)
- return render "show"
- end
- end
- redirect_to daemon_setting_path(@fluentd)
- end
+ include SettingConcern
private
- def setting_params
- params.require(:fluentd_setting_in_syslog).permit(*Fluentd::Setting::InSyslog::KEYS)
+ def target_class
+ Fluentd::Setting::InSyslog
end
+ def initial_params
+ {
+ bind: "0.0.0.0",
+ port: 5140,
+ }
+ end
end
diff --git a/app/controllers/fluentd/settings/out_elasticsearch_controller.rb b/app/controllers/fluentd/settings/out_elasticsearch_controller.rb
new file mode 100644
index 0000000..502eea3
--- /dev/null
+++ b/app/controllers/fluentd/settings/out_elasticsearch_controller.rb
@@ -0,0 +1,21 @@
+class Fluentd::Settings::OutElasticsearchController < ApplicationController
+ include SettingConcern
+
+ private
+
+ def target_class
+ Fluentd::Setting::OutElasticsearch
+ end
+
+ def initial_params
+ {
+ host: "127.0.0.1",
+ port: 9200,
+ index_name: "via_fluentd",
+ type_name: "via_fluentd",
+ logstash_format: true,
+ include_tag_key: false,
+ utc_index: true,
+ }
+ end
+end
diff --git a/app/controllers/fluentd/settings/out_forward_controller.rb b/app/controllers/fluentd/settings/out_forward_controller.rb
index c4d7dcb..e473999 100644
--- a/app/controllers/fluentd/settings/out_forward_controller.rb
+++ b/app/controllers/fluentd/settings/out_forward_controller.rb
@@ -1,35 +1,12 @@
class Fluentd::Settings::OutForwardController < ApplicationController
- before_action :login_required
- before_action :find_fluentd
-
- def show
- @setting = Fluentd::Setting::OutForward.new({
- secondary: {
- "0" => {
- type: "file",
- }
- }
- })
- end
-
- def finish
- @setting = Fluentd::Setting::OutForward.new(setting_params)
- unless @setting.valid?
- return render "show"
- end
-
- @fluentd.agent.config_append @setting.to_config
- if @fluentd.agent.running?
- unless @fluentd.agent.restart
- @setting.errors.add(:base, @fluentd.agent.log_tail(1).first)
- return render "show"
- end
- end
- redirect_to daemon_setting_path(@fluentd)
- end
+ include SettingConcern
private
+ def target_class
+ Fluentd::Setting::OutForward
+ end
+
def setting_params
params.require(:fluentd_setting_out_forward).permit(*Fluentd::Setting::OutForward::KEYS).merge(
params.require(:fluentd_setting_out_forward).permit(
@@ -39,4 +16,13 @@ class Fluentd::Settings::OutForwardController < ApplicationController
)
end
+ def initial_params
+ {
+ secondary: {
+ "0" => {
+ type: "file",
+ }
+ }
+ }
+ end
end
diff --git a/app/controllers/fluentd/settings/out_mongo_controller.rb b/app/controllers/fluentd/settings/out_mongo_controller.rb
index 806bd36..1f0d87a 100644
--- a/app/controllers/fluentd/settings/out_mongo_controller.rb
+++ b/app/controllers/fluentd/settings/out_mongo_controller.rb
@@ -1,36 +1,18 @@
class Fluentd::Settings::OutMongoController < ApplicationController
- before_action :login_required
- before_action :find_fluentd
+ include SettingConcern
- def show
- @setting = Fluentd::Setting::OutMongo.new({
+ private
+
+ def target_class
+ Fluentd::Setting::OutMongo
+ end
+
+ def initial_params
+ {
host: "127.0.0.1",
port: 27017,
capped: true,
capped_size: "100m",
- })
+ }
end
-
- def finish
- @setting = Fluentd::Setting::OutMongo.new(setting_params)
- unless @setting.valid?
- return render "show"
- end
-
- @fluentd.agent.config_append @setting.to_config
- if @fluentd.agent.running?
- unless @fluentd.agent.restart
- @setting.errors.add(:base, @fluentd.agent.log_tail(1).first)
- return render "show"
- end
- end
- redirect_to daemon_setting_path(@fluentd)
- end
-
- private
-
- def setting_params
- params.require(:fluentd_setting_out_mongo).permit(*Fluentd::Setting::OutMongo::KEYS)
- end
-
end
diff --git a/app/controllers/fluentd/settings/out_td_controller.rb b/app/controllers/fluentd/settings/out_td_controller.rb
index 990bc19..8e3368d 100644
--- a/app/controllers/fluentd/settings/out_td_controller.rb
+++ b/app/controllers/fluentd/settings/out_td_controller.rb
@@ -1,36 +1,18 @@
class Fluentd::Settings::OutTdController < ApplicationController
- before_action :login_required
- before_action :find_fluentd
+ include SettingConcern
- def show
- @setting = Fluentd::Setting::OutTd.new({
+ private
+
+ def target_class
+ Fluentd::Setting::OutTd
+ end
+
+ def initial_params
+ {
buffer_type: "file",
buffer_path: "/var/log/td-agent/buffer/td",
auto_create_table: true,
match: "td.*.*",
- })
+ }
end
-
- def finish
- @setting = Fluentd::Setting::OutTd.new(setting_params)
- unless @setting.valid?
- return render "show"
- end
-
- @fluentd.agent.config_append @setting.to_config
- if @fluentd.agent.running?
- unless @fluentd.agent.restart
- @setting.errors.add(:base, @fluentd.agent.log_tail(1).first)
- return render "show"
- end
- end
- redirect_to daemon_setting_path(@fluentd)
- end
-
- private
-
- def setting_params
- params.require(:fluentd_setting_out_td).permit(*Fluentd::Setting::OutTd::KEYS)
- end
-
end
diff --git a/app/models/fluentd/setting/out_elasticsearch.rb b/app/models/fluentd/setting/out_elasticsearch.rb
new file mode 100644
index 0000000..1bb4a2f
--- /dev/null
+++ b/app/models/fluentd/setting/out_elasticsearch.rb
@@ -0,0 +1,24 @@
+class Fluentd
+ module Setting
+ class OutElasticsearch
+ include Common
+
+ KEYS = [
+ :match,
+ :host, :port, :index_name, :type_name,
+ :logstash_format, :logstash_prefix, :logstash_dateformat, :utc_index,
+ :hosts, :request_timeout, :include_tag_key
+ ].freeze
+
+ attr_accessor(*KEYS)
+
+ booleans :logstash_format, :utc_index, :include_tag_key
+
+ validates :match, presence: true
+ validates :host, presence: true
+ validates :port, presence: true
+ validates :index_name, presence: true
+ validates :type_name, presence: true
+ end
+ end
+end
diff --git a/app/views/fluentd/settings/out_elasticsearch/_form.html.haml b/app/views/fluentd/settings/out_elasticsearch/_form.html.haml
new file mode 100644
index 0000000..e34e489
--- /dev/null
+++ b/app/views/fluentd/settings/out_elasticsearch/_form.html.haml
@@ -0,0 +1,23 @@
+= render "shared/setting_errors"
+
+= form_for(@setting, url: finish_daemon_setting_out_elasticsearch_path(@fluentd), html: {class: "ignore-rails-error-div"}) do |f|
+ = field(f, :match)
+ = field(f, :host)
+ = field(f, :port)
+ = field(f, :logstash_format)
+ = field(f, :index_name)
+ = field(f, :type_name)
+
+ .well.well-sm
+ %h4{"data-toggle" => "collapse", "href" => "#advanced-setting"}
+ = icon('fa-caret-down')
+ = t('terms.advanced_setting')
+ #advanced-setting.collapse
+ = field(f, :hosts)
+ = field(f, :logstash_prefix)
+ = field(f, :logstash_dateformat)
+ = field(f, :utc_index)
+ = field(f, :request_timeout)
+ = field(f, :include_tag_key)
+ = f.submit t('fluentd.common.finish'), class: "btn btn-lg btn-primary pull-right"
+
diff --git a/app/views/fluentd/settings/out_elasticsearch/show.html.haml b/app/views/fluentd/settings/out_elasticsearch/show.html.haml
new file mode 100644
index 0000000..8e4354c
--- /dev/null
+++ b/app/views/fluentd/settings/out_elasticsearch/show.html.haml
@@ -0,0 +1,6 @@
+- page_title t(".page_title")
+
+.well
+ = raw t('fluentd.settings.out_elasticsearch.option_guide')
+
+= render "form"
diff --git a/app/views/fluentd/settings/source_and_output.html.haml b/app/views/fluentd/settings/source_and_output.html.haml
index 318a70e..c5a5cbd 100644
--- a/app/views/fluentd/settings/source_and_output.html.haml
+++ b/app/views/fluentd/settings/source_and_output.html.haml
@@ -39,6 +39,10 @@
= link_to(daemon_setting_out_mongo_path(@fluentd)) do
= icon('fa-file-text-o fa-lg')
= t("fluentd.common.setup_out_mongo")
+ %p
+ = link_to(daemon_setting_out_elasticsearch_path(@fluentd)) do
+ = icon('fa-file-text-o fa-lg')
+ = t("fluentd.common.setup_out_elasticsearch")
%p
= link_to(daemon_setting_out_forward_path(@fluentd)) do
= icon('fa-file-text-o fa-lg')
diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml
index 31835dc..2d698a5 100644
--- a/config/locales/translation_en.yml
+++ b/config/locales/translation_en.yml
@@ -97,6 +97,7 @@ en:
setup_out_mongo: MongoDB
setup_out_forward: Forwarding
setup_out_s3: Amazon S3
+ setup_out_elasticsearch: Elasticsearch
finish: Update & Restart
fluentd_info: Setting info
recent_errors: "Errors within recent %{days} days"
@@ -144,6 +145,11 @@ en:
For each config parameter, pelase refer to the Treasure Data output plugin documentation page.
show:
page_title: Add Output to Treasure Data
+ out_elasticsearch:
+ option_guide: |
+ For each config parameter, pelase refer to the Elasticsearch output plugin documentation page.
+ show:
+ page_title: Add Output to Elasticsearch
out_mongo:
option_guide: |
For each config parameter, pelase refer to the MongoDB output plugin documentation page.
diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml
index 1d25fd8..c172ca1 100644
--- a/config/locales/translation_ja.yml
+++ b/config/locales/translation_ja.yml
@@ -96,6 +96,7 @@ ja:
setup_out_mongo: MongoDB
setup_out_forward: 転送
setup_out_s3: AWS S3
+ setup_out_elasticsearch: Elasticsearch
finish: 設定する
fluentd_info: 設定情報
recent_errors: "直近 %{days} 日内のエラー"
@@ -143,6 +144,12 @@ ja:
fluent-plugin-tdプラグインのインストールが必要です。
show:
page_title: Treasure Data書き出し設定
+ out_elasticsearch:
+ option_guide: |
+ fluent-plugin-elasticsearchプラグインのインストールが必要です。
+ out_elasticsearchプラグインの解説もご参照ください。
+ show:
+ page_title: Elasticsearch設定
out_mongo:
option_guide: |
fluent-plugin-mongoプラグインのインストールが必要です。
diff --git a/config/routes.rb b/config/routes.rb
index f7af73c..6bfc44d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -43,6 +43,10 @@ Rails.application.routes.draw do
resource :out_forward, only: [:show], module: :settings, controller: :out_forward do
post "finish"
end
+
+ resource :out_elasticsearch, only: [:show], module: :settings, controller: :out_elasticsearch do
+ post "finish"
+ end
end
end
end
diff --git a/spec/features/fluentd/setting/out_elasticsearch_spec.rb b/spec/features/fluentd/setting/out_elasticsearch_spec.rb
new file mode 100644
index 0000000..9d967dc
--- /dev/null
+++ b/spec/features/fluentd/setting/out_elasticsearch_spec.rb
@@ -0,0 +1,38 @@
+require "spec_helper"
+
+describe "out_elasticsearch" do
+ let(:exists_user) { build(:user) }
+ let(:daemon) { build(:fluentd, variant: "td-agent") }
+ let(:match) { "test.out_elasticsearch.#{Time.now.to_i}.*" }
+ let(:location) { daemon_setting_out_elasticsearch_path }
+
+ before do
+ Fluentd.stub(:instance).and_return(daemon)
+ Fluentd::Agent::TdAgent.any_instance.stub(:detached_command).and_return(true)
+ daemon.agent.config_write ""
+
+ visit '/sessions/new'
+ within("form") do
+ fill_in 'session_name', :with => exists_user.name
+ fill_in 'session_password', :with => exists_user.password
+ end
+ click_button I18n.t("terms.sign_in")
+ end
+
+ it "Shown form" do
+ visit location
+ page.should have_css('input[name="fluentd_setting_out_elasticsearch[match]"]')
+ end
+
+ it "Updated config after submit", js: true do
+ daemon.agent.config.should_not include(match)
+ visit location
+ within('#new_fluentd_setting_out_elasticsearch') do
+ fill_in "Match", with: match
+ fill_in "Index name", with: "index"
+ fill_in "Type name", with: "type_name"
+ end
+ click_button I18n.t("fluentd.common.finish")
+ daemon.agent.config.should include(match)
+ end
+end