logstash-filter-bytes2human 0.1.0

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: 1b2708a5f0cfd2893814460ac226abc56cbc84e5
4
+ data.tar.gz: a7f06a7280e707c3f28bf673f10b6339834c6feb
5
+ SHA512:
6
+ metadata.gz: 1c84be25afb9800da3df8e50f3de387a3190b02f82917379fd8dbb32bde430856b68c2cf9c53fc1a4fa3af711c161fd76b98f11a73352b7aa465b00b404b7c46
7
+ data.tar.gz: 7feb4061871d719e74f8e967500b0fbc9f62cc57b1c0f9fdb965bd2d1bd43c92579ffbb26829028dc5a24e36f4fab827a8cb9901d6f0e042620e74a682728167
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
4
+ vendor
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012-2015 Elastics.co <http://www.elastics.co>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.org/guide/en/logstash/current/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the logstash-users@googlegroups.com mailing list.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "logstash/devutils/rake"
2
+ require "logstash/devutils/rake"
@@ -0,0 +1,80 @@
1
+ require "logstash/filters/base"
2
+ require "logstash/filters/bytes2human/filesize"
3
+ require "logstash/namespace"
4
+
5
+ # The bytes2human filter allows conversion from text (e.g. 2.5 GB) to integer bytes
6
+ # or from integer bytes to human readable form
7
+ # It understands MB, MiB etc.
8
+
9
+ class LogStash::Filters::Bytes2Human < LogStash::Filters::Base
10
+ config_name "bytes2human"
11
+
12
+ # The fields which should be converted, and the direction to convert (human, bytes)
13
+ # Example:
14
+ #
15
+ # filter {
16
+ # bytes2human {
17
+ # # Converts 'data_size' field from 2.4 KB to 2400
18
+ # convert => { "data_size" => "bytes" }
19
+ # }
20
+ # }
21
+ #
22
+ # or
23
+ #
24
+ # filter {
25
+ # bytes2human {
26
+ # # Converts 'data_size' field from 123456 to "123.456 MB"
27
+ # convert => { "data_size" => "human" }
28
+ # }
29
+ # }
30
+ config :convert, :validate => :hash
31
+
32
+ public
33
+ def register
34
+ valid_directions = %w(human bytes)
35
+ @convert.nil? or @convert.each do |field, direction|
36
+ if !valid_directions.include?(direction)
37
+ @logger.error("Invalid conversion direction", "direction" => direction, "expected one of" => valid_directions )
38
+ # TODO: RCL 2014-03-19 propper 'configuration broken' exception
39
+ raise "Bad configuration, aborting."
40
+ end
41
+ end # @convert.each
42
+ end # def register
43
+
44
+ public
45
+ def filter(event)
46
+ return unless filter?(event)
47
+ convert(event) if @convert
48
+ filter_matched(event)
49
+ end # def filter
50
+
51
+ def convert(event)
52
+ @convert.each do |field, direction|
53
+ next unless event.include?(field)
54
+ original = event[field]
55
+
56
+ if original.nil?
57
+ next
58
+ elsif original.is_a?(Hash)
59
+ @logger.debug("I don't know how to type convert a hash, skipping",
60
+ :field => field, :value => original)
61
+ next
62
+ elsif original.is_a?(Array)
63
+ if direction == "human"
64
+ value = original.map { |v| Filesize.from(v.to_s + " B").pretty.force_encoding("UTF-8") }
65
+ else
66
+ value = original.map { |v| Filesize.from(v).to_i }
67
+ end
68
+ else
69
+ if direction == "human"
70
+ value = Filesize.from(original.to_s + " B").pretty.force_encoding("UTF-8")
71
+ else
72
+ value = Filesize.from(original).to_i;
73
+ end
74
+ end
75
+ event[field] = value
76
+ end
77
+ end # def convert
78
+
79
+ end # class LogStash::Filters::Bytes2Human
80
+
@@ -0,0 +1,200 @@
1
+ # This module was based on and modified from:
2
+ # https://github.com/dominikh/filesize/blob/master/lib/filesize.rb
3
+ # Copyright (c) 2015 Robin Clarke
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ class Filesize
24
+ TYPE_PREFIXES ||= {
25
+ # Unit prefixes used for SI file sizes.
26
+ SI: %w{k M G T P E Z Y},
27
+ # Unit prefixes used for binary file sizes.
28
+ BINARY: %w{Ki Mi Gi Ti Pi Ei Zi Yi}
29
+ }
30
+
31
+ # @deprecated Please use TYPE_PREFIXES[:SI] instead
32
+ PREFIXES ||= TYPE_PREFIXES[:SI]
33
+
34
+ # Set of rules describing file sizes according to SI units.
35
+ SI ||= {
36
+ :regexp => /^([\d,.]+)?\s?([kmgtpezy]?)b$/i,
37
+ :multiplier => 1000,
38
+ :prefixes => TYPE_PREFIXES[:SI],
39
+ :presuffix => '' # deprecated
40
+ }
41
+ # Set of rules describing file sizes according to binary units.
42
+ BINARY ||= {
43
+ :regexp => /^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i,
44
+ :multiplier => 1024,
45
+ :prefixes => TYPE_PREFIXES[:BINARY],
46
+ :presuffix => 'i' # deprecated
47
+ }
48
+
49
+ # @param [Number] size A file size, in bytes.
50
+ # @param [SI, BINARY] type Which type to use for conversions.
51
+ def initialize(size, type = BINARY)
52
+ @bytes = size.to_i
53
+ @type = type
54
+ end
55
+
56
+ # @return [Number] Returns the size in bytes.
57
+ def to_i
58
+ @bytes
59
+ end
60
+ alias_method :to_int, :to_i
61
+
62
+ # @param [String] unit Which unit to convert to.
63
+ # @return [Float] Returns the size in a given unit.
64
+ def to(unit = 'B')
65
+ to_parts = self.class.parse(unit)
66
+ prefix = to_parts[:prefix]
67
+
68
+ if prefix == 'B' or prefix.empty?
69
+ return to_i.to_f
70
+ end
71
+
72
+ to_type = to_parts[:type]
73
+ size = @bytes
74
+
75
+ pos = (@type[:prefixes].map { |s| s[0].downcase }.index(prefix.downcase) || -1) + 1
76
+
77
+ size = size/(to_type[:multiplier].to_f**(pos)) unless pos < 1
78
+ end
79
+ alias_method :to_f, :to
80
+
81
+ # @param (see #to_f)
82
+ # @return [String] Same as {#to_f}, but as a string, with the unit appended.
83
+ # @see #to_f
84
+ def to_s(unit = 'B')
85
+ "%.2f %s" % [to(unit).to_f.to_s, unit]
86
+ end
87
+
88
+ # Same as {#to_s} but with an automatic determination of the most
89
+ # sensible unit.
90
+ #
91
+ # @return [String]
92
+ # @see #to_s
93
+ def pretty
94
+ size = @bytes
95
+ if size < @type[:multiplier]
96
+ unit = "B"
97
+ else
98
+ pos = (Math.log(size) / Math.log(@type[:multiplier])).floor
99
+ pos = @type[:prefixes].size-1 if pos > @type[:prefixes].size - 1
100
+
101
+ unit = @type[:prefixes][pos-1] + "B"
102
+ end
103
+
104
+ to_s(unit)
105
+ end
106
+
107
+ # @return [Filesize]
108
+ def +(other)
109
+ self.class.new(@bytes + other.to_i, @type)
110
+ end
111
+
112
+ # @return [Filesize]
113
+ def -(other)
114
+ self.class.new(@bytes - other.to_i, @type)
115
+ end
116
+
117
+ # @return [Filesize]
118
+ def *(other)
119
+ self.class.new(@bytes * other.to_i, @type)
120
+ end
121
+
122
+ # @return [Filesize]
123
+ def /(other)
124
+ result = @bytes / other.to_f
125
+ if other.is_a? Filesize
126
+ result
127
+ else
128
+ self.class.new(result, @type)
129
+ end
130
+ end
131
+
132
+ # @return [Boolean]
133
+ def ==(other)
134
+ other.is_a?(self.class) && other.to_i == self.to_i
135
+ end
136
+
137
+ # @return [Array<self, other>]
138
+ # @api private
139
+ def coerce(other)
140
+ return self, other
141
+ end
142
+
143
+ class << self
144
+ # Parses a string, which describes a file size, and returns a
145
+ # Filesize object.
146
+ #
147
+ # @param [String] arg A file size to parse.
148
+ # @raise [ArgumentError] Raised if the file size cannot be parsed properly.
149
+ # @return [Filesize]
150
+ def from(arg)
151
+ parts = parse(arg)
152
+ prefix = parts[:prefix]
153
+ size = parts[:size]
154
+ type = parts[:type]
155
+
156
+ raise ArgumentError, "Unparseable filesize" unless type
157
+
158
+ offset = (type[:prefixes].map { |s| s[0].downcase }.index(prefix.downcase) || -1) + 1
159
+
160
+ new(size * (type[:multiplier] ** (offset)), type)
161
+ end
162
+
163
+ # @return [Hash<:prefix, :size, :type>]
164
+ # @api private
165
+ def parse(string)
166
+ type = nil
167
+ # in this order, so we prefer si :)
168
+ [SI, BINARY].each { |_type|
169
+ if string =~ _type[:regexp]
170
+ type = _type
171
+ break
172
+ end
173
+ }
174
+
175
+ prefix = $2 || ''
176
+ size = ($1 || 0).to_f
177
+
178
+ return { :prefix => prefix, :size => size, :type => type}
179
+ end
180
+ end
181
+
182
+ # The size of a floppy disk
183
+ Floppy ||= Filesize.from("1474 KiB")
184
+ # The size of a CD
185
+ CD ||= Filesize.from("700 MB")
186
+ # The size of a common DVD
187
+ DVD_5 ||= Filesize.from("4.38 GiB")
188
+ # The same as a DVD 5
189
+ DVD ||= DVD_5
190
+ # The size of a single-sided dual-layer DVD
191
+ DVD_9 ||= Filesize.from("7.92 GiB")
192
+ # The size of a double-sided single-layer DVD
193
+ DVD_10 ||= DVD_5 * 2
194
+ # The size of a double-sided DVD, combining a DVD-9 and a DVD-5
195
+ DVD_14 ||= DVD_9 + DVD_5
196
+ # The size of a double-sided dual-layer DVD
197
+ DVD_18 ||= DVD_14 * 2
198
+ # The size of a Zip disk
199
+ ZIP ||= Filesize.from("100 MB")
200
+ end
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+
3
+ s.name = 'logstash-filter-bytes2human'
4
+ s.version = '0.1.0'
5
+ s.licenses = ['Apache License (2.0)']
6
+ s.summary = "Convert to and from human readable bytes."
7
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
8
+ s.authors = ["Elastics.co"]
9
+ s.email = 'info@elastics.co'
10
+ s.homepage = "http://www.elastics.co/guide/en/logstash/current/index.html"
11
+ s.require_paths = ["lib"]
12
+
13
+ # Files
14
+ s.files = `git ls-files`.split($\)
15
+
16
+ # Tests
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+
19
+ # Special flag to let us know this is actually a logstash plugin
20
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
21
+
22
+ # Gem dependencies
23
+ s.add_runtime_dependency "logstash-core", '>= 1.4.0', '< 2.0.0'
24
+
25
+ s.add_development_dependency 'logstash-devutils'
26
+ end
27
+
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/filters/bytes2human"
4
+
5
+ describe LogStash::Filters::Bytes2Human do
6
+
7
+ describe "To Bytes" do
8
+ # The logstash config.
9
+ config <<-CONFIG
10
+ filter { bytes2human { convert => { "size" => "bytes" } } }
11
+ CONFIG
12
+
13
+ describe "should convert from kb" do
14
+ sample( "size" => "10kb" ) do
15
+ expect( subject["size"] ).to eq( 10000 )
16
+ end
17
+ end
18
+
19
+ describe "should convert from mb" do
20
+ sample( "size" => "10MB" ) do
21
+ expect( subject["size"] ).to eq( 10000000 )
22
+ end
23
+ end
24
+
25
+ describe "should convert from gb" do
26
+ sample( "size" => "10gb" ) do
27
+ expect( subject["size"] ).to eq( 10000000000 )
28
+ end
29
+ end
30
+
31
+
32
+ describe "should convert from tb" do
33
+ sample( "size" => "10.4tb" ) do
34
+ expect( subject["size"] ).to eq( 10400000000000 )
35
+ end
36
+ end
37
+
38
+
39
+ describe "should convert from mib" do
40
+ sample( "size" => "10.4mib" ) do
41
+ expect( subject["size"] ).to eq( 10905190 )
42
+ end
43
+ end
44
+
45
+ describe "should round to nearest byte - partial bytes don't exist" do
46
+ sample( "size" => "1.54321kb" ) do
47
+ expect( subject["size"] ).to eq( 1543 )
48
+ end
49
+ end
50
+
51
+ describe "should deal undefined" do
52
+ sample( "size_not" => 123 ) do
53
+ expect( subject["size"] ).to be_nil
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "To Human" do
59
+ # The logstash config.
60
+ config <<-CONFIG
61
+ filter { bytes2human { convert => { "size" => "human" } } }
62
+ CONFIG
63
+
64
+ describe "should convert integer to KB" do
65
+ sample( "size" => 123456 ) do
66
+ expect( subject["size"] ).to eq( "123.46 kB" )
67
+ end
68
+ end
69
+
70
+ describe "should convert string to MB" do
71
+ sample( "size" => "987654321" ) do
72
+ expect( subject["size"] ).to eq( "987.65 MB" )
73
+ end
74
+ end
75
+
76
+ describe "should deal with a zero value" do
77
+ sample( "size" => 0 ) do
78
+ expect( subject["size"] ).to eq( "0.00 B" )
79
+ end
80
+ end
81
+
82
+ describe "should deal undefined" do
83
+ sample( "size_not" => 123 ) do
84
+ expect( subject["size"] ).to be_nil
85
+ end
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-bytes2human
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elastics.co
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: 1.4.0
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ name: logstash-core
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: logstash-devutils
40
+ prerelease: false
41
+ type: :development
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
48
+ email: info@elastics.co
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/logstash/filters/bytes2human.rb
59
+ - lib/logstash/filters/bytes2human/filesize.rb
60
+ - logstash-filter-bytes2human.gemspec
61
+ - spec/filters/bytes2human_spec.rb
62
+ homepage: http://www.elastics.co/guide/en/logstash/current/index.html
63
+ licenses:
64
+ - Apache License (2.0)
65
+ metadata:
66
+ logstash_plugin: 'true'
67
+ logstash_group: filter
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Convert to and from human readable bytes.
88
+ test_files:
89
+ - spec/filters/bytes2human_spec.rb