Skip to content

Instantly share code, notes, and snippets.

@kozy4324
Created August 22, 2012 12:31
Show Gist options
  • Select an option

  • Save kozy4324/3425112 to your computer and use it in GitHub Desktop.

Select an option

Save kozy4324/3425112 to your computer and use it in GitHub Desktop.
fluentd.confを内部DSLで生成
#!/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
#!/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
@kozy4324
Copy link
Author

😄

$ ./main.rb conf.rb 
<source>
  type forward
  port 24224
  bind 0.0.0.0
</source>
<source>
  type http
  port 9880
  bind 0.0.0.0
  body_size_limit 1m
  keepalive_timeout 10s
</source>
<match **>
  type forward
  send_timeout 60s
  recover_wait 10s
  heartbeat_interval 1s
  phi_threshold 8
  <server>
    name fluentd
    host fluentd-01
    port 24224
  </server>
  <secondary>

  </secondary>
</match>

@kozy4324
Copy link
Author

メインを修正したので$ ./conf.rbでおk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment