logstash-filter-device_detection 1.0.0-java

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: 7ffe06b04a9293e5f53fbe1a527706d103e47506
4
+ data.tar.gz: c6684c978ba8b6e47d8e93074dbaf916f64cf702
5
+ SHA512:
6
+ metadata.gz: 81479cfa879590787dec8b6e82431590be99705cad732a9f59a145e6aa96df66693a697569d8f08254fe39ac2544b5b1282ad2afdd29eb1c020d4a065fd61a60
7
+ data.tar.gz: 67b869e651ac768bb7c0a8c81c8eda82384413c3e582d8f0c492da0fb77f799a4b50131936b13aefa66e3a22147d6ea78a9ae1181a095ca7490632f0c24aaf6a
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 1.0.0
2
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
data/NOTICE.TXT ADDED
@@ -0,0 +1,4 @@
1
+ This product includes Device Detection data created by 51Degrees, available from
2
+ <a href="https://51degrees.com/">https://51degrees.com/</a>.
3
+
4
+ The Device Detection databases are distributed under the Mozilla Public License 2.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Logstash Plugin
2
+
3
+ [![Build Status](https://travis-ci.org/lalex/logstash-filter-device_detection.svg?branch=master)](https://travis-ci.org/lalex/logstash-filter-device_detection)
4
+
5
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
6
+
7
+ 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.
8
+
9
+ ## Documentation
10
+
11
+ 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.co/guide/en/logstash/current/).
12
+
13
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
14
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
15
+
16
+ ## Need Help?
17
+
18
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
19
+
20
+ ## Developing
21
+
22
+ ### 1. Plugin Developement and Testing
23
+
24
+ #### Code
25
+ - To get started, you'll need JRuby with the Bundler gem installed.
26
+
27
+ - 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).
28
+
29
+ - Install dependencies
30
+ ```sh
31
+ bundle install
32
+ ```
33
+
34
+ #### Test
35
+
36
+ - Update your dependencies
37
+
38
+ ```sh
39
+ bundle install
40
+ ```
41
+
42
+ - Run tests
43
+
44
+ ```sh
45
+ bundle exec rspec
46
+ ```
47
+
48
+ ### 2. Running your unpublished Plugin in Logstash
49
+
50
+ #### 2.1 Run in a local Logstash clone
51
+
52
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
53
+ ```ruby
54
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
55
+ ```
56
+ - Install plugin
57
+ ```sh
58
+ bin/logstash-plugin install --no-verify
59
+ ```
60
+ - Run Logstash with your plugin
61
+ ```sh
62
+ bin/logstash -e 'filter {awesome {}}'
63
+ ```
64
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
65
+
66
+ #### 2.2 Run in an installed Logstash
67
+
68
+ 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:
69
+
70
+ - Build your plugin gem
71
+ ```sh
72
+ gem build logstash-filter-awesome.gemspec
73
+ ```
74
+ - Install the plugin from the Logstash home
75
+ ```sh
76
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
77
+ ```
78
+ - Start Logstash and proceed to test the plugin
79
+
80
+ ## Contributing
81
+
82
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
83
+
84
+ 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.
85
+
86
+ It is more important to the community that you are able to contribute.
87
+
88
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+
5
+ require "logstash-filter-device_detection_jars.rb"
6
+
7
+ # [NOTE]
8
+ # --
9
+ # This product includes Device Detection data created by 51Degrees, available from
10
+ # https://51degrees.com/. This database is licensed under
11
+ # http://www.mozilla.org/MPL/2.0/[Mozilla Public License 2].
12
+ # --
13
+
14
+ class LogStash::Filters::DeviceDetection < LogStash::Filters::Base
15
+
16
+ config_name "device_detection"
17
+
18
+ # The field containing the User-Agent header string.
19
+ config :source, :validate => :string, :default => 'user_agent'
20
+
21
+ # Specify the field into which Logstash should store the device data.
22
+ config :target, :validate => :string, :default => 'device_match'
23
+
24
+ # Path to 51Degrees Device Detection data file. Only BinaryV32 format is supported now.
25
+ #
26
+ # If not specified, this will default to the 51Degrees Device Detection database
27
+ # that ships with Logstash.
28
+ config :datafile, :validate => :path
29
+
30
+ # Cache size for Device Detection library.
31
+ #
32
+ # This MUST be set to a value > 0.
33
+ config :cache_size, :validate => :number, :default => 1000
34
+
35
+ # An array of device properties to be included in the event.
36
+ #
37
+ # For the full property dictionary refer to
38
+ # https://51degrees.com/resources/property-dictionary
39
+ config :properties, :validate => :array
40
+
41
+ public
42
+ def register
43
+
44
+ if @datafile.nil?
45
+ @datafile = ::Dir.glob(::File.expand_path("../../../vendor/", ::File.dirname(__FILE__))+"/51Degrees-LiteV3.2.dat").first
46
+
47
+ if @datafile.nil? || !::File.exists?(@datafile)
48
+ raise "You must specify 'datafile => ...' in your device_detection filter (currently is set to '#{@datafile}')"
49
+ end
50
+ end
51
+
52
+ if @properties.nil? || @properties.empty?
53
+ raise "An array of device properties must not be empty."
54
+ end
55
+
56
+ begin
57
+ dataset = Java::FiftyoneMobileDetectionFactories::StreamFactory.create(@datafile, false)
58
+ @provider = Java::FiftyoneMobileDetection::Provider.new(dataset, @cache_size)
59
+ rescue StandardError => e
60
+ @logger.error("Error while initializing device detection provider object", :exception => e)
61
+ return
62
+ end
63
+
64
+ end # def register
65
+
66
+ public
67
+ def filter(event)
68
+ begin
69
+
70
+ begin
71
+ match = @provider.match(event.get(@source))
72
+ rescue StandardError => e
73
+ @logger.error("Error while parsing user agent data", :exception => e, :field => @source, :event => event)
74
+ return
75
+ end
76
+
77
+ return unless match
78
+
79
+ apply_match(match, event)
80
+
81
+ #event.set(@target, match.getValues("IsMobile").toString())
82
+
83
+ rescue Exception=>e
84
+ @logger.error("Failed to detect device", :exception => e, :field => @source)
85
+ end
86
+
87
+ # filter_matched should go in the last line of our successful code
88
+ filter_matched(event)
89
+ end # def filter
90
+
91
+ def apply_match(match, event)
92
+ @properties.each do |property|
93
+ field = "[#{@target}][#{property}]"
94
+ value = match.getValues(property)
95
+ event.set(field, value.toString()) if value
96
+ end
97
+ true
98
+ end
99
+
100
+ end # class LogStash::Filters::Browser
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ require 'logstash/environment'
3
+
4
+ root_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
5
+ LogStash::Environment.load_runtime_jars! File.join(root_dir, "vendor")
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-device_detection'
3
+ s.version = '1.0.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = 'Use 51Degrees Device Detection library to parse User-Agent string'
6
+ s.authors = ['lalex']
7
+ s.email = 'github@lalex.nsk.ru'
8
+ s.platform = "java"
9
+ s.require_paths = ['lib']
10
+
11
+ # Files
12
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','Gemfile','LICENSE','NOTICE.TXT']
13
+ # Tests
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+
16
+ # Special flag to let us know this is actually a logstash plugin
17
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
18
+
19
+ # Gem dependencies
20
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
21
+ s.add_development_dependency 'logstash-devutils'
22
+
23
+ # Jar dependencies
24
+ s.requirements << "jar 'com.51degrees:device-detection-core', '3.2.14.2'"
25
+ s.add_development_dependency 'jar-dependencies', '~> 0.3.2'
26
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/device_detection"
4
+
5
+ DATAFILE = ::Dir.glob(::File.expand_path("../../vendor/", ::File.dirname(__FILE__))+"/51Degrees-LiteV3.2.dat").first
6
+
7
+ describe LogStash::Filters::DeviceDetection do
8
+
9
+ describe "properties is not set" do
10
+ let(:config) do <<-CONFIG
11
+ filter {
12
+ device_detection {
13
+ #datafile => "#{DATAFILE}"
14
+ source => "user_agent"
15
+ target => "device_detection"
16
+ }
17
+ }
18
+ CONFIG
19
+ end
20
+
21
+ sample("user_agent" => "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0") do
22
+ expect { subject }.to raise_error(RuntimeError)
23
+ end
24
+
25
+ end
26
+
27
+ describe "empty properties" do
28
+ let(:config) do <<-CONFIG
29
+ filter {
30
+ device_detection {
31
+ #datafile => "#{DATAFILE}"
32
+ source => "user_agent"
33
+ target => "device_detection"
34
+ properties => []
35
+ }
36
+ }
37
+ CONFIG
38
+ end
39
+
40
+ sample("user_agent" => "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0") do
41
+ expect { subject }.to raise_error(RuntimeError)
42
+ end
43
+
44
+ end
45
+
46
+ describe "defaults" do
47
+ let(:config) do <<-CONFIG
48
+ filter {
49
+ device_detection {
50
+ #datafile => "#{DATAFILE}"
51
+ source => "user_agent"
52
+ target => "device_detection"
53
+ properties => ["BrowserName","BrowserVersion","IsMobile"]
54
+ }
55
+ }
56
+ CONFIG
57
+ end
58
+
59
+ sample("user_agent" => "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0") do
60
+ expect(subject.get('[device_detection][BrowserName]')).to eq('Firefox')
61
+ expect(subject.get('[device_detection][BrowserVersion]')).to eq('41.0')
62
+ expect(subject.get('[device_detection][IsMobile]')).to eq('False')
63
+ end
64
+
65
+ end
66
+
67
+
68
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
Binary file
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-device_detection
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: java
6
+ authors:
7
+ - lalex
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-22 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: '2.0'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: logstash-devutils
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.3.2
47
+ name: jar-dependencies
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.2
55
+ description:
56
+ email: github@lalex.nsk.ru
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - CHANGELOG.md
62
+ - Gemfile
63
+ - LICENSE
64
+ - NOTICE.TXT
65
+ - README.md
66
+ - lib/ch/qos/logback/logback-classic/1.1.8/logback-classic-1.1.8.jar
67
+ - lib/ch/qos/logback/logback-core/1.1.8/logback-core-1.1.8.jar
68
+ - lib/com/51degrees/device-detection-core/3.2.14.2/device-detection-core-3.2.14.2.jar
69
+ - lib/logstash-filter-device_detection_jars.rb
70
+ - lib/logstash/filters/device_detection.rb
71
+ - lib/org/slf4j/slf4j-api/1.7.21/slf4j-api-1.7.21.jar
72
+ - logstash-filter-device_detection.gemspec
73
+ - spec/filters/device_detection_spec.rb
74
+ - spec/spec_helper.rb
75
+ - vendor/51Degrees-LiteV3.2.dat
76
+ - vendor/jar-dependencies/runtime-jars/device-detection-core-3.2.14.2.jar
77
+ - vendor/jar-dependencies/runtime-jars/logback-classic-1.1.8.jar
78
+ - vendor/jar-dependencies/runtime-jars/logback-core-1.1.8.jar
79
+ - vendor/jar-dependencies/runtime-jars/slf4j-api-1.7.21.jar
80
+ homepage:
81
+ licenses:
82
+ - Apache License (2.0)
83
+ metadata:
84
+ logstash_plugin: 'true'
85
+ logstash_group: filter
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements:
101
+ - jar 'com.51degrees:device-detection-core', '3.2.14.2'
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.8
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Use 51Degrees Device Detection library to parse User-Agent string
107
+ test_files:
108
+ - spec/filters/device_detection_spec.rb
109
+ - spec/spec_helper.rb