embulk-plugin-input-pcapng-files 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a831f929119e0209c353052fe5bf6e049dbafe36
4
+ data.tar.gz: 6c5d1a06ae6c8921c21a5577c03ad460eec672ea
5
+ SHA512:
6
+ metadata.gz: c3a1b45cfd90162d1670a0f57ac2a78c10713feb26e9a44c09dffa72b6a2c1d615a253fda80ff4aaf082400daf6cea7a06d0e70d4d0ca69e85fd9aee7e868b85
7
+ data.tar.gz: fa8833f4b3d290e80ec1f9a1257890ce17edfb653e8ce60e1af956e4ca8798c382bc892b905643509f630bf4f542c3a342c5c9e59a15e8eb526534055b6e34d2
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ embulk-plugin-input-pcapng-files
2
+ ================================
3
+
4
+ Embulk plugin for pcapng files input.
5
+
6
+
7
+ To extract specific field from pcapng files
8
+
9
+ 1. check sample\_config.yml, modify "paths" to where ever pcapng files are.
10
+ 2. specify fields to collect in "schema", name should correspond to tshark's field name
11
+
@@ -0,0 +1,106 @@
1
+ require "csv"
2
+
3
+ module Embulk
4
+ class InputPcapngFiles < InputPlugin
5
+ # input plugin file name must be: embulk/input_<name>.rb
6
+ Plugin.register_input('pcapng_files', self)
7
+
8
+ def self.transaction(config, &control)
9
+ threads = config.param('threads', :integer, default: 2)
10
+ task = {
11
+ 'paths' => [],
12
+ 'done' => config.param('done', :array, default: []),
13
+ 'paths_per_thread' => [],
14
+ }
15
+
16
+ task['paths'] = config.param('paths', :array, default: []).map {|path|
17
+ next [] unless Dir.exists?(path)
18
+ Dir.entries(path).sort.select {|entry| entry.match(/^.+\.pcapng$/)}.map {|entry|
19
+ path + "/" + entry
20
+ }
21
+ }.flatten
22
+ task['paths'] = task['paths'] - task['done']
23
+ task['paths_per_thread'] = task['paths'].each_slice(task['paths'].length / threads + 1).to_a
24
+
25
+ if task['paths'] == []
26
+ raise "no valid pcapng file found"
27
+ end
28
+
29
+ schema = config.param('schema', :array, default: [])
30
+ columns = []
31
+ columns << Column.new(0, "path", :string)
32
+ idx = 0
33
+ columns.concat schema.map{|c|
34
+ idx += 1
35
+ Column.new(idx, "#{c['name']}", c['type'].to_sym)
36
+ }
37
+
38
+ commit_reports = yield(task, columns, threads)
39
+ done = commit_reports.map{|hash| hash["done"]}.flatten.compact
40
+
41
+ return config.merge({ "done" => done })
42
+ end
43
+
44
+ def initialize(task, schema, index, page_builder)
45
+ super
46
+ end
47
+
48
+ attr_reader :task
49
+ attr_reader :schema
50
+ attr_reader :page_builder
51
+
52
+ def run
53
+ paths = task['paths_per_thread'][@index]
54
+ if paths == nil or paths == []
55
+ return {} # no task, no fail
56
+ end
57
+
58
+ paths.each do |path|
59
+ each_packet(path, schema[1..-1].map{|elm| elm.name}) do |hash|
60
+ entry = [ path ] + schema[1..-1].map {|c|
61
+ convert(hash[c.name], c.type)
62
+ }
63
+ @page_builder.add(entry)
64
+ end
65
+ end
66
+ @page_builder.finish # must call finish they say
67
+
68
+ return {"done" => paths}
69
+ end
70
+
71
+ private
72
+
73
+ def convert val, type
74
+ v = val
75
+ v = "" if val == nil
76
+ v = v.to_i if type == :long
77
+ return v
78
+ end
79
+
80
+ def build_options(fields)
81
+ options = ""
82
+ fields.each do |field|
83
+ options += "-e '#{field}' "
84
+ end
85
+ return options
86
+ end
87
+
88
+ def each_packet(path, fields, &block)
89
+ options = build_options(fields)
90
+ io = IO.popen("tshark -E separator=, #{options} -T fields -r #{path}")
91
+ while line = io.gets
92
+ array = [fields, CSV.parse(line).flatten].transpose
93
+ yield(Hash[*array.flatten])
94
+ end
95
+ io.close
96
+ end
97
+
98
+ def fetch_from_pcap(path, fields)
99
+ options = build_options(fields)
100
+ io = IO.popen("tshark -E separator=, #{options} -T fields -r #{path}")
101
+ data = io.read
102
+ io.close
103
+ return data
104
+ end
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: embulk-plugin-input-pcapng-files
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Naoya Kaneko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2
41
+ description: embulk plugin for pcapng file input
42
+ email: enukane@glenda9.org
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - README.md
48
+ - lib/embulk/input_pcapng_files.rb
49
+ homepage: https://github.com/enukane/embulk-plugin-input-pcapng-files
50
+ licenses:
51
+ - Apache 2.0
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.2.2
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: embulk plugin for pcapng file input
73
+ test_files: []