msgknife 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 12486e66b362622c270279e3f899c878aefacbc2
4
+ data.tar.gz: 0f7c2744d6339331415be1642d4b9e050d1902cb
5
+ SHA512:
6
+ metadata.gz: 46957373ce19f947101ffd7e15880616c0c96659350615779c99b45df87afc680114c0c7cc7af35e89d9b78bcb3e68ddcaeb0e83f59dddfae9e9bb9a3e86886d
7
+ data.tar.gz: 635c553d8ba0ebb8b1ac6d58cdef331535f0cdf67a83f3960bc5e36cb27a06472d6bdea5ba8e81d9e8aeb56bbacfdc6adb686ab2ea988c1fc68f3c9b85e19f84
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in msgknife.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Masayoshi Mizutani
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Msgknife
2
+
3
+ Utilities of MessagePack format file and stream.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'msgknife'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install msgknife
20
+
21
+ ## Usage
22
+
23
+ ### mpp: Msgpack Prittey Print
24
+
25
+ $ hexdump -C data.msg
26
+ 0000000 82 a1 61 01 a1 62 01 82 a1 61 03 a1 62 04 82 a1
27
+ 0000010 61 01 a1 62 08
28
+ 0000015
29
+ $ mpp data.msg
30
+ {"a"=>1, "b"=>1}
31
+ {"a"=>3, "b"=>4}
32
+ {"a"=>1, "b"=>8}
33
+
34
+ ### mkeys: Msgpack KEYS
35
+
36
+ Count and show the number of each key fields.
37
+
38
+ $ mkeys data.msg
39
+ {"a"=>3, "b"=>3}
40
+
41
+ ### mentropy: Msgpack ENTROPY
42
+
43
+ $ mentropy data.msg
44
+ {"a"=>0.9182958340544896, "b"=>1.584962500721156}
45
+
46
+
47
+ ## Common Options
48
+
49
+ ### `-m` output with msgpack encoding
50
+
51
+ $ mkeys data.msg
52
+ {"a"=>3, "b"=>3}
53
+ $ mkeys -m data.msg | hexdump
54
+ 0000000 82 a1 61 03 a1 62 03
55
+ 0000007
56
+
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/[my-github-username]/msgknife/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/mentropy ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'msgknife'
4
+ require 'pp'
5
+
6
+ # MsgPack Prety Print
7
+ class MEntropy < Msgknife::Stream
8
+ def initialize
9
+ super
10
+
11
+ @optpsr.on('-t', 'tag mode') {|v| @tag_mode = v }
12
+ @optpsr.on('-r', 'show as ratio of maximum') {|v| @ratio = v }
13
+ end
14
+
15
+ def setup(argv)
16
+ @key_map = Hash.new {|h1,k1| h1[k1] = Hash.new{|h2,k2| h2[k2] = 0}}
17
+ end
18
+
19
+ def recv(obj, ts, tag)
20
+ if @tag_mode
21
+ @key_map['tag'][tag] += 1 unless tag.nil?
22
+ else
23
+ obj.each do |k,v|
24
+ if !(@ignore_nil) or !(v.nil?)
25
+ @key_map[k][v] += 1
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def teardown
32
+ res = {}
33
+ maxc = 0
34
+ @key_map.each do |k, m|
35
+ sum = (m.inject(0){|s,v| s + v[1]}).to_f
36
+ sump = 0.0
37
+ m.each do |val, count|
38
+ prob = count.to_f / sum
39
+ sump += prob * Math.log2(prob)
40
+ end
41
+ res[k] = -sump
42
+ maxc = sum if maxc < sum
43
+ end
44
+
45
+ maxp = -(Math.log2(1.0/maxc))
46
+ if @ratio
47
+ keys = res.keys
48
+ keys.each { |k| res[k] = res[k] / maxp }
49
+ end
50
+
51
+ write_stream(res);
52
+ end
53
+ end
54
+
55
+ ins = MEntropy.new()
56
+ ins.run(ARGV)
data/bin/mheat ADDED
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require 'msgknife'
5
+ require 'pp'
6
+ require 'time'
7
+ require 'msgpack'
8
+ require 'json'
9
+
10
+ # MsgPack Prety Print
11
+ class MHeatMap < Msgknife::Stream
12
+ BLANK = ' '
13
+ BARS = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']
14
+
15
+ def initialize
16
+ super
17
+
18
+ # Set options
19
+ @ts_key = 'ts'
20
+ @optpsr.on('-t VAL', 'timestamp key name') { |v| @ts_key = v }
21
+
22
+ @count_key = nil
23
+ @optpsr.on('-c VAL', 'key name to sum numbers') { |v| @count_key = v }
24
+
25
+ @width = 80 - 32
26
+ @optpsr.on('-w VAL', 'width') { |v| @width = v.to_i }
27
+
28
+ @relative = false
29
+ @optpsr.on('-r', 'relative time') { |v| @relative = true }
30
+
31
+ @output_fmt = 'console'
32
+ @optpsr.on('-j', 'output as json') { |v| @output_fmt = 'json' }
33
+ @optpsr.on('-m', 'output as msgpack') { |v| @output_fmt = 'msgpack' }
34
+
35
+ # Set internval parameters
36
+ @key = nil
37
+ @ts_min = nil
38
+ @ts_max = nil
39
+ @rows = Hash.new {|h,k| h[k] = Array.new}
40
+ end
41
+
42
+ def setup(argv)
43
+ @key = argv[0]
44
+ end
45
+
46
+ def add_ts(key, ts, val)
47
+
48
+ if key.instance_of? Array
49
+ key.each { |v| add_ts(v, ts, val) }
50
+ elsif key.instance_of? Hash
51
+ key.each { |k,v| add_ts(v, ts, val) }
52
+ elsif key.instance_of? String
53
+ @rows[key] << [ts, val]
54
+ elsif key.instance_of? Fixnum
55
+ @rows[key.to_s] << [ts, val]
56
+ end
57
+ end
58
+
59
+ def recv(msg, ts, tag)
60
+ @ts_min = ts if @ts_min.nil? or ts < @ts_min
61
+ @ts_max = ts if @ts_max.nil? or @ts_max < ts
62
+
63
+ if @count_key.nil?
64
+ add_ts(msg[@key], ts, 1)
65
+ else
66
+ add_ts(msg[@key], ts, msg[@count_key]) if msg.has_key? @count_key
67
+ end
68
+ end
69
+
70
+ def output_console(obj)
71
+ STDOUT.write(sprintf("%30s | %s", "", obj['cols'][0]))
72
+ puts
73
+ STDOUT.write(sprintf("%30s | %s|", "-" * 28,
74
+ "-" * @width))
75
+ puts
76
+
77
+ obj['rows'].each do |k, row|
78
+ label = sprintf("%30s | ", k[0..28])
79
+ STDOUT.write(label)
80
+
81
+ v_max = row['row'].max
82
+
83
+ row['row'].each do |v|
84
+ if v == 0
85
+ STDOUT.write(BLANK)
86
+ else
87
+ bi = (v / v_max) * BARS.size
88
+ bi = BARS.size - 1 if BARS.size <= bi
89
+ STDOUT.write(BARS[bi])
90
+ end
91
+ end
92
+ STDOUT.write('|')
93
+ puts
94
+ end
95
+ end
96
+
97
+ def teardown
98
+ dur_ts = @ts_max - @ts_min
99
+ ts_unit = dur_ts / @width
100
+
101
+ cols = Array.new(@width)
102
+ (0..@width-1).each do |i|
103
+ if @relative
104
+ ts = ts_unit * i
105
+ cols[i] = sprintf("%02d:%02d:%02d", (ts / 3600).to_i,
106
+ ((ts % 3600) / 60).to_i, (ts % 60))
107
+ else
108
+ dt = Time.at(@ts_min + ts_unit * i)
109
+ cols[i] = dt.to_s.split(' ')[0..1].join(' ')
110
+ end
111
+ end
112
+
113
+
114
+ data_map = Hash.new {|h,k| h[k] = {
115
+ 'row' => Array.new(@width, 0),
116
+ 'max' => 0,
117
+ }}
118
+ @rows.each do |k, ts_list|
119
+ ts_list.each do |ts, val|
120
+ idx = ((ts - @ts_min) / ts_unit).to_i
121
+ idx = @width - 1 if idx >= @width
122
+ data_map[k]['row'][idx] += val
123
+ if data_map[k]['max'] < data_map[k]['row'][idx]
124
+ data_map[k]['max'] = data_map[k]['row'][idx]
125
+ end
126
+ end
127
+ end
128
+
129
+ obj = {
130
+ 'cols' => cols,
131
+ 'rows' => data_map,
132
+ 'ts_max' => @ts_max,
133
+ 'ts_min' => @ts_min,
134
+ }
135
+
136
+ # write_stream(obj)
137
+ case @output_fmt
138
+ when 'console'; output_console(obj)
139
+ when 'json'; STDOUT.write(obj.to_json)
140
+ when 'msgpack'; STDOUT.write(obj.to_msgpack)
141
+ end
142
+ end
143
+ end
144
+
145
+ ins = MHeatMap.new()
146
+ ins.run(ARGV, 0..0)
data/bin/mkeys ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'msgknife'
4
+ require 'pp'
5
+
6
+ # MsgPack Prety Print
7
+ class MKEYS < Msgknife::Stream
8
+ def initialize
9
+ super
10
+
11
+ @optpsr.on('-t', 'tag mode') {|v| @tag_mode = v }
12
+ @optpsr.on('-N', 'ignore if value is nil') {|v| @ignore_nil = v }
13
+ end
14
+
15
+ def setup(argv)
16
+ @key_map = Hash.new {|h1,k1| h1[k1] = 0}
17
+ end
18
+
19
+ def recv(obj, ts, tag)
20
+ if @tag_mode
21
+ @key_map[tag] += 1 unless tag.nil?
22
+ else
23
+ obj.each do |k,v|
24
+ if !(@ignore_nil) or !(v.nil?)
25
+ @key_map[k] += 1
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def teardown
32
+ write_stream(@key_map);
33
+ end
34
+ end
35
+
36
+ ins = MKEYS.new()
37
+ ins.run(ARGV)
data/bin/mpp ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'msgknife'
4
+ require 'pp'
5
+
6
+ # MsgPack Prety Print
7
+ class MPP < Msgknife::Stream
8
+ def setup(argv); end
9
+ def recv(obj, ts, tag)
10
+ write_stream(obj, ts, tag)
11
+ end
12
+ end
13
+
14
+ ins = MPP.new()
15
+ ins.run(ARGV)
data/bin/mval ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'msgknife'
4
+ require 'pp'
5
+
6
+ # MsgPack Prety Print
7
+ class MPP < Msgknife::Stream
8
+ def setup(argv)
9
+ @key = argv[0]
10
+ end
11
+ def recv(obj, ts, tag)
12
+ if obj.key?(@key) and (!@ignore_nil or !(obj[@key].nil?))
13
+ write_stream({@key => obj[@key]}, ts, tag)
14
+ end
15
+ end
16
+ end
17
+
18
+ ins = MPP.new()
19
+ ins.run(ARGV, 0..0)
@@ -0,0 +1,3 @@
1
+ module Msgknife
2
+ VERSION = "0.0.1"
3
+ end
data/lib/msgknife.rb ADDED
@@ -0,0 +1,120 @@
1
+ require "msgknife/version"
2
+ require 'msgpack'
3
+ require 'optparse'
4
+ require 'time'
5
+
6
+ module Msgknife
7
+ class Stream
8
+ attr_accessor :optpsr
9
+
10
+ def initialize
11
+ @out_encode = false
12
+ @optpsr = OptionParser.new
13
+ @optpsr.on('-m', 'output as msgpack encode') { |v| @out_encode = v }
14
+ @optpsr.on('-F', 'input as fluentd format') { |v| @in_fluentd = v }
15
+ @optpsr.on('-T VAL', 'key of timestamp in message') { |v| @ts_key = v }
16
+ @optpsr.on('-N', 'ignore if value is nil') {|v| @ignore_nil = v }
17
+ @argv = []
18
+ end
19
+
20
+ def run(cmd_argv, range = nil)
21
+ args = @optpsr.parse(cmd_argv)
22
+
23
+ unless range.nil?
24
+ raise "Not enough arguments" if args.size < range.last + 1
25
+ @argv = args.slice!(range)
26
+ end
27
+
28
+ setup(@argv)
29
+ read_stream(args)
30
+ teardown
31
+ end
32
+
33
+
34
+ def read_io(io)
35
+ u = MessagePack::Unpacker.new(io)
36
+ begin
37
+ u.each {|obj|
38
+ if @in_fluentd
39
+ recv(obj[2], obj[1], obj[0])
40
+ else
41
+ if @ts_key.nil? or !(obj.key?(@ts_key))
42
+ recv(obj, nil, nil)
43
+ else
44
+ ts = nil
45
+ ts_val = obj[@ts_key]
46
+ case ts_val
47
+ when String
48
+ dt = Time.parse(ts_val) rescue nil
49
+ ts = dt.to_i
50
+ when Fixnum
51
+ ts = ts_val
52
+ when Float
53
+ ts = ts_val.to_i
54
+ end
55
+
56
+ recv(obj, ts, nil)
57
+ end
58
+ end
59
+ }
60
+ rescue EOFError
61
+ # ignore
62
+ rescue Interrupt
63
+ return
64
+ end
65
+ end
66
+
67
+ def read_stream(files=nil)
68
+ if files == nil or files.size == 0
69
+ read_io(STDIN)
70
+ else
71
+ f_list = Array.new
72
+ if files.instance_of? String
73
+ f_list << files
74
+ else
75
+ f_list += files
76
+ end
77
+
78
+ f_list.each do |fpath|
79
+ if File.directory?(fpath)
80
+ Find.find(fpath) do |file|
81
+ next if File.directory?(file)
82
+ read_io(File.open(file, 'r'))
83
+ end
84
+ else
85
+ read_io(File.open(fpath, 'r'))
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ def write_stream(obj, ts=nil, tag=nil)
92
+ if @out_encode == false
93
+ if ts.nil? and tag.nil?
94
+ pp obj
95
+ else
96
+ pp [tag, ts, obj]
97
+ end
98
+ else
99
+ if ts.nil? and tag.nil?
100
+ STDOUT.write(obj.to_msgpack)
101
+ else
102
+ STDOUT.write([tag, ts, obj].to_msgpack)
103
+ end
104
+ end
105
+ rescue Errno::EPIPE => e ;
106
+ end
107
+
108
+
109
+ def setup(argv)
110
+ raise 'setup(argv) must be implemented' if argv.size > 0
111
+ end
112
+
113
+ def recv(obj, ts, tag)
114
+ raise 'exec(obj) must be implemented'
115
+ end
116
+
117
+ def teardown; end
118
+ end
119
+
120
+ end
data/msgknife.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'msgknife/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "msgknife"
8
+ spec.version = Msgknife::VERSION
9
+ spec.authors = ["Masayoshi Mizutani"]
10
+ spec.email = ["mizutani@sfc.wide.ad.jp"]
11
+ spec.summary = %q{MsgKnife: Utilities of MessagePack format file and stream}
12
+ spec.description = %q{MsgKnife makes easy to view, check and analyze MessagePack format file and stream such as Fluentd data}
13
+ spec.homepage = "https://github.com/m-mizutani/msgknife"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_dependency "msgpack", '~> 0.5'
24
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: msgknife
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Masayoshi Mizutani
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-07 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: msgpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.5'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.5'
55
+ description: MsgKnife makes easy to view, check and analyze MessagePack format file
56
+ and stream such as Fluentd data
57
+ email:
58
+ - mizutani@sfc.wide.ad.jp
59
+ executables:
60
+ - mentropy
61
+ - mheat
62
+ - mkeys
63
+ - mpp
64
+ - mval
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".gitignore"
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/mentropy
74
+ - bin/mheat
75
+ - bin/mkeys
76
+ - bin/mpp
77
+ - bin/mval
78
+ - lib/msgknife.rb
79
+ - lib/msgknife/version.rb
80
+ - msgknife.gemspec
81
+ homepage: https://github.com/m-mizutani/msgknife
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.5
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: 'MsgKnife: Utilities of MessagePack format file and stream'
105
+ test_files: []