Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from thdls55/master
Browse files Browse the repository at this point in the history
The Logstash HandsetDetection plugin.
  • Loading branch information
elricho authored Oct 27, 2016
2 parents dd89a4a + 9e31e08 commit 0f7d5fc
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.gem
Gemfile.lock
Gemfile.bak
.bundle
vendor
output.txt
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: ruby

sudo: required

services:
- docker

install: /bin/true

script:
- ./ci/docker-run.sh
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gemspec
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2016 Handset Detection <http://www.handsetdetection.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
67 changes: 65 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,65 @@
# logstash-filter-handsetdetection
A logstash filter for Handset Detection API v4. Enrich your logs with all sorts of device information. :-)
[![Build Status](https://travis-ci.org/HandsetDetection/logstash-filter-handsetdetection.svg?branch=master)](https://travis-ci.org/HandsetDetection/logstash-filter-handsetdetection)
[![Gem Version](https://badge.fury.io/rb/logstash-filter-handsetdetection.svg)](https://badge.fury.io/rb/logstash-filter-handsetdetection)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

# Handset Detection for Logstash

This is the [Handset Detection](http://www.handsetdetection.com/) plugin for [Logstash](https://github.com/elastic/logstash).

## Example

Extract the User-Agent header from an Apache log with Grok, and then enrich the log with Headset Detection data:

```
filter {
grok {
match => {
"message" => "%{COMBINEDAPACHELOG}"
}
}
handsetdetection {
username => "xxxxxxxxxx"
password => "xxxxxxxxxxxxxxxx"
site_id => 000000
online_api => false
match => {
"agent" => "user-agent"
}
filter => [
"general_vendor", "general_model", "general_type"
]
}
}
```

## Configuration Fields

| Field | Description | Default |
| --- | --- | --- |
| `online_api` | Set to `true` in order to do **online** lookups against the Handset Detection API | `false` |
| `username` | Your Handset Detection API username | |
| `password` | Your Handset Detection API password | |
| `site_id` | The Handset Detection API site ID to use | |
| `match` | An associative array mapping input field names to header names used for handset detection. For example: Extract the `user-agent` header from the `agent` input field. | `{ "agent" => "user-agent" }` |
| `filter` | Optionally, define an array of the handset spec properties to be included in the output. By default, all properties are included in the output. | [] |
| `use_proxy` | Set to `true` if accessing the web through a proxy | `false` |
| `proxy_server` | The proxy server address, if using a proxy ||
| `proxy_port` | The proxy server port, if using a proxy ||
| `proxy_user` | Your proxy server username, if authenticating to a proxy ||
| `proxy_password` | Your proxy server password, if authenticating to a proxy ||

## Installation

```
# Logstash 2.3 and higher
bin/logstash-plugin install logstash-filter-handsetdetection-1.1.0.gem
# Prior to Logstash 2.3
bin/plugin install logstash-filter-handsetdetection-1.1.0.gem
```

## Building from source

```
gem build logstash-filter-handsetdetection.gemspec
```
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "logstash/devutils/rake"
17 changes: 17 additions & 0 deletions ci/docker-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

gem build logstash-filter-handsetdetection.gemspec

mv logstash-filter-handsetdetection-*.gem test.gem

docker run -i --rm \
-e API_USERNAME \
-e API_SECRET \
-e API_SITE_ID \
-v $(pwd)/test.gem:/test.gem:z \
-v $(pwd)/ci/logstash.sh:/logstash.sh:z \
-v $(pwd)/ci/sample.log:/sample.log:z \
logstash:2.3.4 \
/logstash.sh | tee output.txt

grep general_model output.txt | cmp ci/general_model.txt
10 changes: 10 additions & 0 deletions ci/general_model.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"general_model" => "GT-I9003",
"general_model" => "MB525",
"general_model" => "6085",
"general_model" => "ROKR E2",
"general_model" => "SCH-M828C",
"general_model" => "GT-I9003",
"general_model" => "MB525",
"general_model" => "6085",
"general_model" => "ROKR E2",
"general_model" => "SCH-M828C",
56 changes: 56 additions & 0 deletions ci/logstash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

logstash-plugin install /test.gem

# Online API

gosu logstash logstash -e '
input {
stdin { }
}
filter {
grok {
match => {
"message" => "%{COMBINEDAPACHELOG}"
}
}
handsetdetection {
username => "'$API_USERNAME'"
password => "'$API_SECRET'"
site_id => '$API_SITE_ID'
filter => ["general_vendor", "general_model", "general_type"]
online_api => true
}
}
output {
stdout {
codec => rubydebug
}
}
' < sample.log

# Offline API

gosu logstash logstash -e '
input {
stdin { }
}
filter {
grok {
match => {
"message" => "%{COMBINEDAPACHELOG}"
}
}
handsetdetection {
username => "'$API_USERNAME'"
password => "'$API_SECRET'"
site_id => '$API_SITE_ID'
filter => ["general_vendor", "general_model", "general_type"]
}
}
output {
stdout {
codec => rubydebug
}
}
' < sample.log
5 changes: 5 additions & 0 deletions ci/sample.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
127.0.0.1 - - [11/Dec/2013:00:01:45 -0800] "GET /xampp/status.php HTTP/1.1" 200 3891 "http://cadenza/xampp/navi.php" "Mozilla/5.0 (Linux; U; Android 2.2.1; fr-fr; GT-I9003 Build/FROYO) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2 (AdMob-ANDROID-20091123)"
127.0.0.1 - - [11/Dec/2013:00:01:45 -0800] "GET /xampp/status.php HTTP/1.1" 200 3891 "http://cadenza/xampp/navi.php" "Mozilla/5.0 (Linux; U; Android 2.1-update1; ar-eg; MB525 Build/JRDNEM_U3_2.59.0) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17"
127.0.0.1 - - [11/Dec/2013:00:01:45 -0800] "GET /xampp/status.php HTTP/1.1" 200 3891 "http://cadenza/xampp/navi.php" "Nokia6085/2.0 (06.00) Profile/MIDP-2.0 Configuration/CLDC-1.1 nokia6085/UC Browser7.9.0.102/69/352 UNTRUSTED/1.0"
127.0.0.1 - - [11/Dec/2013:00:01:45 -0800] "GET /xampp/status.php HTTP/1.1" 200 3891 "http://cadenza/xampp/navi.php" "MOT-ROKR E2/R564_G_12.01.46P K1nDinuX Mozilla/4.0 (compatible; MSIE 6.0; Linux; Motorola ROKR E2; 781) Profile/MIDP-2.0 Configuration/CLDC-1.1 Opera 8.50 [es-LA]"
127.0.0.1 - - [11/Dec/2013:00:01:45 -0800] "GET /xampp/status.php HTTP/1.1" 200 3891 "http://cadenza/xampp/navi.php" "Mozilla/5.0 (Linux; U; Android 2.2.2; en-us; SCH-M828C[7658912216] Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
112 changes: 112 additions & 0 deletions lib/logstash/filters/handsetdetection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#--
# Copyright (c) Richard Uren 2016 <[email protected]>
# All Rights Reserved
#
# LICENSE: Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met: Redistributions of source code must retain the
# above copyright notice, this list of conditions and the following
# disclaimer. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#++

require 'logstash/filters/base'
require 'logstash/namespace'
require 'handset_detection'

class LogStash::Filters::HandsetDetection < LogStash::Filters::Base

config_name 'handsetdetection'

config :online_api, :validate => :boolean, :default => false
config :username, :validate => :string, :default => ''
config :password, :validate => :string, :default => ''
config :site_id, :validate => :number, :default => 0
config :apiserver, :validate => :string, :default => 'api.handsetdetection.com'
config :match, :validate => :hash, :default => { 'agent' => 'user-agent' }
config :filter, :validate => :array, :default => []
config :use_proxy, :validate => :boolean, :default => false
config :proxy_server, :validate => :string, :default => ''
config :proxy_port, :validate => :number, :default => 3128
config :proxy_user, :validate => :string, :default => ''
config :proxy_pass, :validate => :string, :default => ''
config :local_archive_source, :validate => :string, :default => nil

public
def register
@hd_config = {}
@hd_config['username'] = @username
@hd_config['secret'] = @password
@hd_config['site_id'] = @site_id
@hd_config['use_local'] = @online_api ? false : true
@hd_config['filesdir'] = '/tmp'
@hd_config['cache'] = {'none' => {}}
@hd_config['debug'] = false
@hd_config['api_server'] = @apiserver
@hd_config['cache_requests'] = false
@hd_config['geoip'] = true
@hd_config['timeout'] = 30
@hd_config['use_proxy'] = @use_proxy
@hd_config['proxy_server'] = @proxy_server
@hd_config['proxy_port'] = @proxy_port
@hd_config['proxy_user'] = @proxy_user
@hd_config['proxy_pass'] = @proxy_pass
@hd_config['retries'] = 3
@hd_config['log_unknown'] = true
@hd_config['local_archive_source'] = @local_archive_source

@@pool = []
unless @online_api
hd = HD4.new @hd_config
hd.set_timeout 500
hd.device_fetch_archive
hd.set_timeout 30
@@pool << hd
end
end

public
def filter(event)
data = {}
@match.each do |src, dest|
if event.include? src
data[dest] = event[src]
end
end
hd = @@pool.pop
hd = HD4.new @hd_config if hd.nil?
hd.device_detect data
r = hd.get_reply
@@pool << hd
if r.key? 'class'
event['handset_class'] = r['class']
end
if r.key? 'hd_specs'
if @filter.empty?
event['handset_specs'] = r['hd_specs']
else
event['handset_specs'] = {}
@filter.each do |f|
if r['hd_specs'].key? f
event['handset_specs'][f] = r['hd_specs'][f]
end
end
end
end
filter_matched event
end
end
26 changes: 26 additions & 0 deletions logstash-filter-handsetdetection.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Gem::Specification.new do |s|
s.name = 'logstash-filter-handsetdetection'
s.version = '4.1.1'
s.licenses = ['MIT']
s.summary = "Handset Detection filter plugin for Logstash"
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
s.authors = ["Handset Detection"]
s.email = '[email protected]'
s.homepage = "http://www.handsetdetection.com/"
s.require_paths = ["lib"]

# Files
s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
# Tests
s.test_files = s.files.grep(%r{^(test|spec|features)/})

# Special flag to let us know this is actually a logstash plugin
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }

# Gem dependencies
s.add_runtime_dependency "logstash-core", ">= 2.0.0", "< 3.0.0"
s.add_runtime_dependency "json", "~> 1.8"
s.add_runtime_dependency "handset_detection", "~> 4.1"

#s.add_development_dependency 'logstash-devutils'
end
20 changes: 20 additions & 0 deletions spec/filters/handsetdetection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# encoding: utf-8
require 'spec_helper'
require "logstash/filters/handsetdetection"

describe LogStash::Filters::Example do
describe "Simple detection" do
let(:config) do <<-CONFIG
filter {
handsetdetection {
}
}
CONFIG
end

sample("agent" => "Nokia6085/2.0 (06.00) Profile/MIDP-2.0 Configuration/CLDC-1.1 nokia6085/UC Browser7.9.0.102/69/352 UNTRUSTED/1.0",) do
expect(subject).to include("handset_specs")
expect(subject["handset_specs"]["general_vendor"]).to eq("Nokia")
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"

0 comments on commit 0f7d5fc

Please sign in to comment.