Created
August 22, 2012 12:31
-
-
Save kozy4324/3425112 to your computer and use it in GitHub Desktop.
fluentd.confを内部DSLで生成
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| # coding: utf-8 | |
| require "./fluentd_conf_dsl" | |
| # CONSTANTS | |
| DOMAIN = "fluentd-01" | |
| # MACRO | |
| def macro_forward_settings | |
| type "forward" | |
| send_timeout "60s" | |
| recover_wait "10s" | |
| heartbeat_interval "1s" | |
| phi_threshold "8" | |
| server do | |
| name "#{DOMAIN}" | |
| host "#{DOMAIN}.example.com" | |
| port "24224" | |
| end | |
| end | |
| # DSL START | |
| source do | |
| type "forward" | |
| port "24224" | |
| bind "0.0.0.0" | |
| end | |
| source do | |
| type "http" | |
| port "9880" | |
| bind "0.0.0.0" | |
| body_size_limit "1m" | |
| keepalive_timeout "10s" | |
| end | |
| match "**" do | |
| macro_forward_settings | |
| secondary do | |
| macro_forward_settings | |
| end | |
| end |
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| # coding: utf-8 | |
| module FluentdConfDSL | |
| class Conf | |
| def initialize | |
| @current = Element.new | |
| @level = -1 | |
| end | |
| def consume(id, *args, &block) | |
| @level += 1 | |
| if block_given? | |
| parent = @current | |
| parent.push(@current = Tag.new(id.to_s, args, @level)) | |
| yield | |
| @current = parent | |
| else | |
| @current.push KeyVal.new(id.to_s, args[0], @level) | |
| end | |
| @level -= 1 | |
| end | |
| def to_s | |
| @current.to_s | |
| end | |
| end | |
| class Element | |
| def elements | |
| @elements ||= [] | |
| end | |
| def push(element) | |
| elements.push element | |
| end | |
| def indent | |
| " " * @level | |
| end | |
| def to_s | |
| elements.join "\n" | |
| end | |
| end | |
| class Tag < Element | |
| def initialize(name, args, level) | |
| @name = name | |
| @tag = args[0] | |
| @level = level | |
| end | |
| def tag | |
| if @tag.nil? then "" else " #{@tag}" end | |
| end | |
| def to_s | |
| "#{indent}<#{@name}#{tag}>\n#{elements.join("\n")}\n#{indent}</#{@name}>" | |
| end | |
| end | |
| class KeyVal < Element | |
| def initialize(key, val, level) | |
| @key = key | |
| @val = val | |
| @level = level | |
| end | |
| def to_s | |
| "#{indent}#{@key} #{@val}" | |
| end | |
| end | |
| end | |
| @output_file = false | |
| @conf = FluentdConfDSL::Conf.new | |
| at_exit { puts @conf.to_s unless @output_file } | |
| @handle_dsl = false | |
| alias :method_missing_org :method_missing | |
| def method_missing(id, *args, &block) | |
| if id.to_s == "output_file" | |
| @output_file = true | |
| @conf = FluentdConfDSL::Conf.new | |
| yield | |
| File.open("./#{args[0]}", 'w') do |io| | |
| io.puts @conf.to_s | |
| end | |
| puts "[INFO] create #{args[0]}" | |
| puts @conf.to_s | |
| elsif @handle_dsl or %w|source match|.include?(id.to_s) | |
| @handle_dsl = true | |
| @conf.consume(id, *args, &block) | |
| else | |
| method_missing_org(id, *args, &block) | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
メインを修正したので
$ ./conf.rbでおk