Class: Fluent::Plugin::NfctParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/fluent/plugin/parser_nfct.rb

Constant Summary collapse

REGEXP =
regexp_base[{}]
REGEXP_EXTENDED =
regexp_base[
  l3protocol: '(?<l3protocol>.+?)\s+(?<l3protonum>\d+)\s+',
]
TIME_REGEXP =
/\A\[.+=/
NUM_REGEXP =
/\A\d+\z/
DELIMITER =
/\s+/
LABEL_SCAN =
/(\[.+?\]|.+?)(?:\s|\z)/

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/fluent/plugin/parser_nfct.rb', line 39

def configure(conf)
  super
  @regexp = @extended ? REGEXP_EXTENDED : REGEXP
  if @ktimestamp
    @time_parser = Strptime.new('%b %d %H:%M:%S %Y')
  end
end

#parse(text) {|time, value| ... } ⇒ Object

Yields:

  • (time, value)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/parser_nfct.rb', line 47

def parse(text)
  m = text.match(@regexp)
  unless m
    yield nil, nil
    return
  end

  r = m.named_captures
  %w(protonum l3protonum timeout).each do |k|
    r[k] = r[k].to_i if r[k]
  end

  if @ktimestamp
    parts = r.delete('remaining')&.scan(LABEL_SCAN).flatten || []
  else
    parts = r.delete('remaining')&.split(DELIMITER) || []
  end
  parts.each do |part|
    case
    when @ktimestamp && part.match?(TIME_REGEXP)
      k,v = part[1..-2].split(?=,2)
      begin
        r[k] = @time_parser.execi(v[4..-1])
      rescue ArgumentError
      end
    when part[0] == '['
      r[part[1..-2].downcase] = true
    else
      k,v = part.split(?=, 2)
      if v.match(NUM_REGEXP)
        r[k] = v.to_i
      else
        r[k] = v
      end
    end
  end
  time, value = convert_values(parse_time(r), r)
  yield time, value
end