fluent-plugin-input-gelf 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b72e348033187ca8353e8f0f458586e757f7ae49
4
+ data.tar.gz: b481677ab24fed18c12e90b0e0e01fb41a4f105a
5
+ SHA512:
6
+ metadata.gz: c0e9646e2608c25e9a6f2f3a198f27eb5b44034d5df2c388034f8c68ed5350f54e20d49a77d34d31418a9e81a156dc395b2129668f749fbff4b1f0474e59bf0c
7
+ data.tar.gz: 23524913869c9d57d8f1ea03f48fd0edfd5cb3828863513f4319b4150b22ff707ad16e1fa1ef21f494c010848bd03e0b3c7e8ca36d5d9c171a6a0b8add1efc18
@@ -0,0 +1,11 @@
1
+ ~*
2
+ #*
3
+ *~
4
+ [._]*.s[a-w][a-z]
5
+ .DS_Store
6
+
7
+ *.gem
8
+ .bundle
9
+ Gemfile.lock
10
+ vendor
11
+ .ruby-version
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2.2
8
+ - 2.3.0
9
+
10
+ gemfile:
11
+ - Gemfile
12
+
13
+ branches:
14
+ only:
15
+ - master
16
+
17
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Daniel Malon
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # fluent-plugin-input-gelf
2
+
3
+ [![Build Status](https://travis-ci.org/MerlinDMC/fluent-plugin-input-gelf.svg?branch=master)](https://travis-ci.org/MerlinDMC/fluent-plugin-input-gelf)
4
+ [![Gem Version](https://badge.fury.io/rb/fluent-plugin-input-gelf.svg)](http://badge.fury.io/rb/fluent-plugin-input-gelf)
5
+
6
+ ## Overview
7
+
8
+ A GELF compatible input for [Fluentd](http://www.fluentd.org/).
9
+
10
+ ### Configuration
11
+
12
+ Accept GELF encoded messages over UDP
13
+
14
+ ```
15
+ <source>
16
+ type gelf
17
+ tag example.gelf
18
+ bind 127.0.0.1
19
+ port 12201
20
+ </source>
21
+
22
+ <match example.gelf>
23
+ type stdout
24
+ </match>
25
+ ```
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.test_files = FileList['test/plugin/test_*.rb']
9
+ test.verbose = true
10
+ end
11
+
12
+ task :default => [:build]
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "fluent-plugin-input-gelf"
6
+ gem.description = "A GELF input plugin for fluentd"
7
+ gem.license = "MIT"
8
+ gem.homepage = "https://github.com/MerlinDMC/fluent-plugin-input-gelf"
9
+ gem.summary = gem.description
10
+ gem.version = "0.1.0"
11
+ gem.authors = ["Daniel Malon"]
12
+ gem.email = "daniel.malon@me.com"
13
+ gem.has_rdoc = false
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_runtime_dependency "fluentd"
20
+ gem.add_runtime_dependency "gelfd", ">= 0.2.0"
21
+
22
+ gem.add_development_dependency "bundler"
23
+ gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "test-unit"
25
+ gem.add_development_dependency "gelf", ">= 2.0.0"
26
+ end
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+ require 'time'
3
+
4
+ require 'cool.io'
5
+ require 'yajl'
6
+
7
+ require 'fluent/input'
8
+
9
+ module Fluent
10
+ class GelfInput < Fluent::Input
11
+ Fluent::Plugin.register_input('gelf', self)
12
+
13
+ def initialize
14
+ super
15
+ require 'fluent/plugin/socket_util'
16
+ require 'gelfd'
17
+ end
18
+
19
+ desc "The value is the tag assigned to the generated events."
20
+ config_param :tag, :string
21
+ desc 'The format of the payload.'
22
+ config_param :format, :string, default: 'json'
23
+ desc 'The port to listen to.'
24
+ config_param :port, :integer, default: 12201
25
+ desc 'The bind address to listen to.'
26
+ config_param :bind, :string, default: '0.0.0.0'
27
+ desc 'The transport protocol used to receive logs.(udp, tcp)'
28
+ config_param :protocol_type, default: :udp do |val|
29
+ case val.downcase
30
+ when 'tcp'
31
+ :tcp
32
+ when 'udp'
33
+ :udp
34
+ else
35
+ raise ConfigError, "gelf input protocol type should be 'tcp' or 'udp'"
36
+ end
37
+ end
38
+ config_param :blocking_timeout, :time, default: 0.5
39
+
40
+ def configure(conf)
41
+ super
42
+
43
+ @parser = Plugin.new_parser(@format)
44
+ @parser.configure(conf)
45
+ end
46
+
47
+ def start
48
+ @loop = Coolio::Loop.new
49
+ @handler = listen(method(:receive_data))
50
+ @loop.attach(@handler)
51
+
52
+ @thread = Thread.new(&method(:run))
53
+ end
54
+
55
+ def shutdown
56
+ @loop.watchers.each { |w| w.detach }
57
+ @loop.stop
58
+ @handler.close
59
+ @thread.join
60
+ end
61
+
62
+ def run
63
+ @loop.run(@blocking_timeout)
64
+ rescue
65
+ log.error 'unexpected error', error: $!.to_s
66
+ log.error_backtrace
67
+ end
68
+
69
+ def receive_data(data, addr)
70
+ begin
71
+ msg = Gelfd::Parser.parse(data)
72
+ rescue => e
73
+ log.warn 'Gelfd failed to parse a message', error: e.to_s
74
+ log.warn_backtrace
75
+ end
76
+
77
+ # Gelfd parser will return nil if it received and parsed a non-final chunk
78
+ return if msg.nil?
79
+
80
+ @parser.parse(msg) { |time, record|
81
+ unless time && record
82
+ log.warn "pattern not match: #{msg.inspect}"
83
+ return
84
+ end
85
+
86
+ # Use the recorded event time if available
87
+ time = record.delete('timestamp').to_i if record.key?('timestamp')
88
+
89
+ emit(time, record)
90
+ }
91
+ rescue => e
92
+ log.error data.dump, error: e.to_s
93
+ log.error_backtrace
94
+ end
95
+
96
+ def listen(callback)
97
+ log.info "listening gelf socket on #{@bind}:#{@port} with #{@protocol_type}"
98
+ if @protocol_type == :tcp
99
+ Coolio::TCPServer.new(@bind, @port, SocketUtil::TcpHandler, log, "\n", callback)
100
+ else
101
+ @usock = SocketUtil.create_udp_socket(@bind)
102
+ @usock.bind(@bind, @port)
103
+ SocketUtil::UdpHandler.new(@usock, log, 8192, callback)
104
+ end
105
+ end
106
+
107
+ def emit(time, record)
108
+ router.emit(@tag, time, record)
109
+ rescue => e
110
+ log.error 'gelf failed to emit', error: e.to_s, error_class: e.class.to_s, tag: @tag, record: Yajl.dump(record)
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,72 @@
1
+ require_relative '../test_helper'
2
+ require 'gelf'
3
+
4
+ class GelfInputTest < Test::Unit::TestCase
5
+ def setup
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ PORT = 12345
10
+ BASE_CONFIG = %[
11
+ port #{PORT}
12
+ protocol_type udp
13
+ tag gelf
14
+ ]
15
+ CONFIG = BASE_CONFIG + %!
16
+ bind 127.0.0.1
17
+ !
18
+ IPv6_CONFIG = BASE_CONFIG + %!
19
+ bind ::1
20
+ !
21
+
22
+ def create_driver(conf)
23
+ Fluent::Test::InputTestDriver.new(Fluent::GelfInput).configure(conf)
24
+ end
25
+
26
+ def test_configure
27
+ configs = {'127.0.0.1' => CONFIG}
28
+ configs.merge!('::1' => IPv6_CONFIG) if ipv6_enabled?
29
+
30
+ configs.each_pair { |k, v|
31
+ d = create_driver(v)
32
+ assert_equal PORT, d.instance.port
33
+ assert_equal k, d.instance.bind
34
+ assert_equal 'json', d.instance.format
35
+ }
36
+ end
37
+
38
+ def test_parse
39
+ configs = {'127.0.0.1' => CONFIG}
40
+ # gelf-rb currently does not support IPv6 over UDP
41
+ # configs.merge!('::1' => IPv6_CONFIG) if ipv6_enabled?
42
+
43
+ configs.each_pair { |k, v|
44
+ d = create_driver(v)
45
+
46
+ tests = [
47
+ {:short_message => 'short message', :full_message => 'full message'},
48
+ {:short_message => 'short message', :full_message => 'full message', :timestamp => 12345678.12345}
49
+ ]
50
+
51
+ d.run do
52
+ n = GELF::Notifier.new(k, PORT)
53
+
54
+ tests.each { |test|
55
+ n.notify!(test)
56
+ }
57
+
58
+ sleep 1
59
+ end
60
+
61
+ emits = d.emits
62
+ assert_equal tests.length, emits.length, 'missing emitted events'
63
+ emits.each_index { |i|
64
+ puts emits[i].to_s
65
+ assert_equal 'gelf', emits[i][0]
66
+ assert_equal tests[i][:timestamp].to_i, emits[i][1] unless tests[i][:timestamp].nil?
67
+ assert_equal tests[i][:short_message], emits[i][2]['short_message']
68
+ assert_equal tests[i][:full_message], emits[i][2]['full_message']
69
+ }
70
+ }
71
+ end
72
+ end
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+
15
+ require 'fluent/test'
16
+ unless ENV.has_key?('VERBOSE')
17
+ nulllogger = Object.new
18
+ nulllogger.instance_eval {|obj|
19
+ def method_missing(method, *args)
20
+ # pass
21
+ end
22
+ }
23
+ $log = nulllogger
24
+ end
25
+
26
+ require 'fluent/plugin/in_gelf'
27
+
28
+ def unused_port
29
+ s = TCPServer.open(0)
30
+ port = s.addr[1]
31
+ s.close
32
+ port
33
+ end
34
+
35
+ def ipv6_enabled?
36
+ require 'socket'
37
+
38
+ begin
39
+ TCPServer.open("::1", 0)
40
+ true
41
+ rescue
42
+ false
43
+ end
44
+ end
45
+
46
+ class Test::Unit::TestCase
47
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-input-gelf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Malon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gelfd
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test-unit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: gelf
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.0.0
97
+ description: A GELF input plugin for fluentd
98
+ email: daniel.malon@me.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - ".travis.yml"
105
+ - Gemfile
106
+ - LICENSE
107
+ - README.md
108
+ - Rakefile
109
+ - fluent-plugin-input-gelf.gemspec
110
+ - lib/fluent/plugin/in_gelf.rb
111
+ - test/plugin/test_in_gelf.rb
112
+ - test/test_helper.rb
113
+ homepage: https://github.com/MerlinDMC/fluent-plugin-input-gelf
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.5.1
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: A GELF input plugin for fluentd
137
+ test_files:
138
+ - test/plugin/test_in_gelf.rb
139
+ - test/test_helper.rb