diff --git a/app/controllers/tutorials_controller.rb b/app/controllers/tutorials_controller.rb index 55b85e6..b540058 100644 --- a/app/controllers/tutorials_controller.rb +++ b/app/controllers/tutorials_controller.rb @@ -1,6 +1,7 @@ class TutorialsController < ApplicationController before_action :find_fluentd before_action :check_ready, only: [:chapter1, :chapter2] + before_action :set_in_http, only: [:chapter1, :request_fluentd] helper_method :tutorial_ready? def index @@ -20,12 +21,16 @@ class TutorialsController < ApplicationController end def request_fluentd - HTTPClient.post("http://localhost:9880#{params[:path]}", json: params[:data].to_json) + HTTPClient.post("http://localhost:#{@in_http["port"]}#{params[:path]}", json: params[:data].to_json) render nothing: true, status: 204 end private + def set_in_http + @in_http = @fluentd.agent.configuration.sources.find{|directive| directive["type"] == "http"} + end + def find_fluentd # NOTE: use first fluentd for tutorial @fluentd = Fluentd.first diff --git a/app/models/fluentd/agent/common.rb b/app/models/fluentd/agent/common.rb index d094452..d2c219f 100644 --- a/app/models/fluentd/agent/common.rb +++ b/app/models/fluentd/agent/common.rb @@ -90,6 +90,12 @@ class Fluentd extra_options[:config_file] || self.class.default_options[:config_file] end + def configuration + if File.exists? config_file + ::Fluentd::Agent::Configuration.new(config_file) + end + end + %w(start stop restart).each do |method| define_method(method) do raise NotImplementedError diff --git a/app/models/fluentd/agent/configuration.rb b/app/models/fluentd/agent/configuration.rb new file mode 100644 index 0000000..5228521 --- /dev/null +++ b/app/models/fluentd/agent/configuration.rb @@ -0,0 +1,35 @@ +require "fluent/config/v1_parser" + +class Fluentd + class Agent + class Configuration + include Enumerable + + attr_reader :file + + def initialize(config_file) + @file = config_file + end + + def config + @config ||= ::Fluent::Config::V1Parser.read(file) + end + + def to_s + config.to_s.gsub(/\A\n/, "").gsub(/<\/ROOT>\n\z/, "").gsub(/^ {2}/, "") + end + + def each(&block) + config.each_element(&block) + end + + def sources + find_all{|e| e.name == "source"} + end + + def matches + find_all{|e| e.name == "match"} + end + end + end +end diff --git a/app/views/tutorials/chapter1.html.erb b/app/views/tutorials/chapter1.html.erb index 874a35d..504a21d 100644 --- a/app/views/tutorials/chapter1.html.erb +++ b/app/views/tutorials/chapter1.html.erb @@ -16,7 +16,7 @@

" /> - $ curl -X POST http://localhost:9880{{ path }} -F 'json={{ data | to_json }}' + $ curl -X POST http://localhost:<%= @in_http["port"] %>{{ path }} -F 'json={{ data | to_json }}'