class Embulk::Parser::PcapngParserPlugin

Public Class Methods

transaction(config) { |task, columns| ... } click to toggle source
# File lib/embulk/parser/pcapng.rb, line 9
def self.transaction(config, &control)
  schema = config.param("schema", :array, default: [])
  task = {
    "schema" => schema
  }

  idx = -1
  columns = schema.map{|s|
    idx += 1
    elm = Column.new(idx, "#{s['name']}", s['type'].to_sym)
  }

  yield(task, columns)
end

Public Instance Methods

init() click to toggle source
# File lib/embulk/parser/pcapng.rb, line 24
def init
  @schema = @task["schema"]
end
run(file_input) click to toggle source
# File lib/embulk/parser/pcapng.rb, line 28
def run(file_input)
  while file = file_input.next_file
    tmpf, tmppath = tmppcapng(file.read)
    each_packet(tmppath, @schema.map{|elm| elm["name"]}) do |hash|
      entry = @schema.map{|s| convert(hash[s["name"]], s["type"])}
      page_builder.add(entry)
    end
    tmpf.close
  end
  page_builder.finish
end

Private Instance Methods

build_options(fields) click to toggle source
# File lib/embulk/parser/pcapng.rb, line 49
def build_options(fields)
  options = ""
  fields.each do |field|
    options += "-e '#{field}' "
  end
  return options
end
convert(val, type) click to toggle source
# File lib/embulk/parser/pcapng.rb, line 41
def convert val, type
  v = val
  v = "" if val == nil
  v = v.to_i if type == "long"
  v = v.to_f if type == "float"
  return v
end
each_packet(path, fields) { |Hash| ... } click to toggle source
# File lib/embulk/parser/pcapng.rb, line 64
def each_packet(path, fields, &block)
  options = build_options(fields)
  io = IO.popen("tshark -E separator=, #{options} -T fields -r #{path}")
  while line = io.gets
    array = [fields, CSV.parse(line).flatten].transpose
    yield(Hash[*array.flatten])
  end
  io.close
end
fetch_from_pcap(path, fields) click to toggle source
# File lib/embulk/parser/pcapng.rb, line 74
def fetch_from_pcap(path, fields)
  options = build_options(fields)
  io = IO.popen("tshark -E separator=, #{options} -T fields -r #{path}")
  data = io.read
  io.close
  return data
end
tmppcapng(data) click to toggle source
# File lib/embulk/parser/pcapng.rb, line 57
def tmppcapng(data)
  tmpf = Tempfile.open("pcapng")
  tmpf.write(data)
  tmpf.flush()
  return tmpf, tmpf.path
end