Class: Fluentd::Setting::InTail

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/fluentd/setting/in_tail.rb

Constant Summary collapse

MULTI_LINE_MAX_FORMAT_COUNT =
20

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formatObject

Returns the value of attribute format.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def format
  @format
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def path
  @path
end

#pos_fileObject

Returns the value of attribute pos_file.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def pos_file
  @pos_file
end

#read_from_headObject

Returns the value of attribute read_from_head.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def read_from_head
  @read_from_head
end

#refresh_intervalObject

Returns the value of attribute refresh_interval.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def refresh_interval
  @refresh_interval
end

#regexpObject

Returns the value of attribute regexp.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def regexp
  @regexp
end

#rotate_waitObject

Returns the value of attribute rotate_wait.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def rotate_wait
  @rotate_wait
end

#tagObject

Returns the value of attribute tag.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def tag
  @tag
end

#time_formatObject

Returns the value of attribute time_format.



7
8
9
# File 'app/models/fluentd/setting/in_tail.rb', line 7

def time_format
  @time_format
end

Class Method Details

.known_formatsObject

validates :format, presence: true



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/fluentd/setting/in_tail.rb', line 13

def self.known_formats
  {
    :apache2 => [:time_format],
    :nginx => [:time_format],
    :syslog => [:time_format],
    :tsv => [:keys, :time_key],
    :csv => [:keys, :time_key],
    :ltsv => [:delimiter, :time_key],
    :json => [:time_key],
    :regexp => [:time_format, :regexp],
    :multiline => [:format_firstline] + (1..MULTI_LINE_MAX_FORMAT_COUNT).map{|n| "format#{n}".to_sym }
    # TODO: Grok could generate Regexp including \d, \s, etc. fluentd config parser raise error with them for escape sequence check.
    #       TBD How to handle Grok/Regexp later, just comment out for hide
    # :grok => [:grok_str],
  }
end

Instance Method Details

#certain_format_lineObject



82
83
84
85
86
87
88
89
90
91
# File 'app/models/fluentd/setting/in_tail.rb', line 82

def certain_format_line
  case format
  when "grok"
    "format /#{grok.convert_to_regexp(grok_str).source.gsub("/", "\\/")}/ # grok: '#{grok_str}'"
  when "regexp"
    "format /#{regexp}/"
  else
    "format #{format}"
  end
end

#extra_format_optionsObject



56
57
58
# File 'app/models/fluentd/setting/in_tail.rb', line 56

def extra_format_options
  self.class.known_formats[format.to_sym] || []
end

#format_specific_confObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/fluentd/setting/in_tail.rb', line 60

def format_specific_conf
  return "" if %w(grok regexp).include?(format)

  indent = " " * 2
  format_specific_conf = ""

  if format.to_sym == :multiline
    known_formats[:multiline].each do |key|
      value = send(key)
      if value.present?
        format_specific_conf << "#{indent}#{key} /#{value}/\n"
      end
    end
  else
    extra_format_options.each do |key|
      format_specific_conf << "#{indent}#{key} #{send(key)}\n"
    end
  end

  format_specific_conf
end

#grokObject



93
94
95
96
97
98
99
100
# File 'app/models/fluentd/setting/in_tail.rb', line 93

def grok
  @grok ||=
    begin
      grok = GrokConverter.new
      grok.load_patterns
      grok
    end
end

#guess_formatObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/fluentd/setting/in_tail.rb', line 35

def guess_format
  case path
  when /\.json$/
    :json
  when /\.csv$/
    :csv
  when /\.tsv$/
    :tsv
  when /\.ltsv$/
    :ltsv
  when /nginx/
    :nginx
  when /apache/
    :apache2
  when %r|/var/log|
    :syslog
  else
    :regexp
  end
end

#known_formatsObject



31
32
33
# File 'app/models/fluentd/setting/in_tail.rb', line 31

def known_formats
  self.class.known_formats
end

#to_confObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/fluentd/setting/in_tail.rb', line 102

def to_conf
  # NOTE: Using strip_heredoc makes more complex for format_specific_conf indent
  <<-XML.gsub(/^[ ]*\n/m, "")
<source>
  type tail
  path #{path}
  tag #{tag}
  #{certain_format_line}
#{format_specific_conf}

  #{read_from_head.to_i.zero? ? "" : "read_from_head true"}
  #{pos_file.present? ? "pos_file #{pos_file}" : ""}
  #{rotate_wait.present? ? "rotate_wait #{rotate_wait}" : ""}
  #{refresh_interval.present? ? "refresh_interval #{refresh_interval}" : ""}
</source>
  XML
end