gelfd2 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE +14 -0
  6. data/README.md +8 -0
  7. data/Rakefile +8 -0
  8. data/bin/gelfd +19 -0
  9. data/gelfd2.gemspec +25 -0
  10. data/lib/gelfd2/chunked_parser.rb +48 -0
  11. data/lib/gelfd2/exceptions.rb +8 -0
  12. data/lib/gelfd2/gzip_parser.rb +16 -0
  13. data/lib/gelfd2/parser.rb +22 -0
  14. data/lib/gelfd2/version.rb +3 -0
  15. data/lib/gelfd2/zlib_parser.rb +15 -0
  16. data/lib/gelfd2.rb +17 -0
  17. data/test/fixtures/chunked_10_37.chunk +0 -0
  18. data/test/fixtures/chunked_11_37.chunk +0 -0
  19. data/test/fixtures/chunked_12_37.chunk +0 -0
  20. data/test/fixtures/chunked_13_37.chunk +0 -0
  21. data/test/fixtures/chunked_14_37.chunk +0 -0
  22. data/test/fixtures/chunked_15_37.chunk +0 -0
  23. data/test/fixtures/chunked_16_37.chunk +0 -0
  24. data/test/fixtures/chunked_17_37.chunk +0 -0
  25. data/test/fixtures/chunked_18_37.chunk +0 -0
  26. data/test/fixtures/chunked_19_37.chunk +0 -0
  27. data/test/fixtures/chunked_1_37.chunk +0 -0
  28. data/test/fixtures/chunked_20_37.chunk +0 -0
  29. data/test/fixtures/chunked_21_37.chunk +0 -0
  30. data/test/fixtures/chunked_22_37.chunk +0 -0
  31. data/test/fixtures/chunked_23_37.chunk +0 -0
  32. data/test/fixtures/chunked_24_37.chunk +0 -0
  33. data/test/fixtures/chunked_25_37.chunk +0 -0
  34. data/test/fixtures/chunked_26_37.chunk +0 -0
  35. data/test/fixtures/chunked_27_37.chunk +0 -0
  36. data/test/fixtures/chunked_28_37.chunk +0 -0
  37. data/test/fixtures/chunked_29_37.chunk +0 -0
  38. data/test/fixtures/chunked_2_37.chunk +0 -0
  39. data/test/fixtures/chunked_30_37.chunk +0 -0
  40. data/test/fixtures/chunked_31_37.chunk +0 -0
  41. data/test/fixtures/chunked_32_37.chunk +0 -0
  42. data/test/fixtures/chunked_33_37.chunk +0 -0
  43. data/test/fixtures/chunked_34_37.chunk +0 -0
  44. data/test/fixtures/chunked_35_37.chunk +0 -0
  45. data/test/fixtures/chunked_36_37.chunk +0 -0
  46. data/test/fixtures/chunked_37_37.chunk +0 -0
  47. data/test/fixtures/chunked_3_37.chunk +0 -0
  48. data/test/fixtures/chunked_4_37.chunk +0 -0
  49. data/test/fixtures/chunked_5_37.chunk +0 -0
  50. data/test/fixtures/chunked_6_37.chunk +0 -0
  51. data/test/fixtures/chunked_7_37.chunk +0 -0
  52. data/test/fixtures/chunked_8_37.chunk +0 -0
  53. data/test/fixtures/chunked_9_37.chunk +0 -0
  54. data/test/fixtures/unchunked.uc +1 -0
  55. data/test/fixtures/unchunked.zl +0 -0
  56. data/test/tc_chunked.rb +17 -0
  57. data/test/tc_unchunked.rb +14 -0
  58. data/test/tc_uncompressed.rb +14 -0
  59. data/test/ts_all.rb +5 -0
  60. metadata +145 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b3499cd8199e37e4da3a8ff9ca40f532e15369f
4
+ data.tar.gz: acf0f142d4f915d3f2381be5c96f8b71abf58d70
5
+ SHA512:
6
+ metadata.gz: be9ca319feb6f9dce7bfb779ecb20af6391e33e4fa110c09a661ecbb6792f8519c2f88563e08e1e827e98d7ca9e33c2cb89a8316dfb41af7b1532d376327623c
7
+ data.tar.gz: 6f42adedef1b33599c89f27a1bae7e9e183de63055fa7188cfc13a88f2eaceaa91049e349c7a7d61d2ff58ced68315a9c6eb0a74b0f86fec9dfa020247acef75
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - 2.3.1
5
+ - rbx
6
+ - jruby
7
+
8
+ script: "bundle exec rake test"
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright 2011-2013 John E. Vincent and contributors.
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.
14
+
data/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # gelfd2
2
+ [![Build Status](https://travis-ci.org/ptqa/gelfd2.svg?branch=master)](https://travis-ci.org/ptqa/gelfd2)
3
+
4
+
5
+ This is a fork of gelfd (see https://github.com/lusis/gelfd). Gelf is a standalone implementation of GELF (Graylog2 Extended Log Format). This is used by the graylog2 server as an extended format for handling log messages.
6
+
7
+ ## Rationale
8
+ This was originally concieved as a way to provide a graylog2-compatible input for logstash.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/ts_*.rb']
7
+ t.verbose = true
8
+ end
data/bin/gelfd ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
3
+ require 'socket'
4
+ require 'gelfd2'
5
+
6
+ server = UDPSocket.new
7
+ puts "Starting up"
8
+ server.bind(nil, 11211)
9
+ trap("INT") { puts "Shutting down"; exit }
10
+
11
+ loop do
12
+ data, addr = server.recvfrom(8192)
13
+ begin
14
+ res = Gelfd2::Parser.parse(data)
15
+ rescue Exception => e
16
+ puts e.message
17
+ end
18
+ puts res unless res.nil?
19
+ end
data/gelfd2.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gelfd2/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gelfd2"
7
+ s.version = Gelfd2::VERSION
8
+ s.authors = ["John E. Vincent", "ptQa"]
9
+ s.homepage = "https://github.com/ptqa/gelfd2"
10
+ s.summary = %q{Pure ruby gelf server and decoding library}
11
+ s.description = %q{Standalone implementation of the Graylog Extended Log Format}
12
+
13
+ s.rubyforge_project = "gelfd2"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for example:
21
+ s.add_development_dependency "json", "~> 1.8.3"
22
+ s.add_development_dependency "rake", "~> 11.2.2"
23
+ s.add_development_dependency "test-unit", "~> 3.2.1"
24
+ # s.add_runtime_dependency "rest-client"
25
+ end
@@ -0,0 +1,48 @@
1
+ module Gelfd2
2
+ class ChunkedParser
3
+ @@chunk_map = Hash.new {|hash,key| hash[key] = {:total_chunks => 0, :chunks => {} } }
4
+
5
+ attr_accessor :message_id, :max_chunks, :decoded_data, :chunks, :seen
6
+
7
+ def self.parse(data)
8
+ msg_id = self.parse_chunk(data)
9
+ if @@chunk_map[msg_id][:chunks].size == @@chunk_map[msg_id][:total_chunks]
10
+ assemble_chunks(msg_id)
11
+ end
12
+ end
13
+
14
+ def self.assemble_chunks(msg_id)
15
+ buff = ''
16
+ chunks = @@chunk_map[msg_id][:chunks]
17
+ chunks.keys.sort.each do |k|
18
+ buff += chunks[k]
19
+ end
20
+ begin
21
+ # TODO
22
+ # This has a chance for an DoS
23
+ # you can send a chunked message as a chunked message
24
+ t = Parser.parse(buff.clone)
25
+ @@chunk_map.delete(msg_id)
26
+ t
27
+ rescue Exception => e
28
+ "Exception: #{e.message}"
29
+ end
30
+ end
31
+
32
+ private
33
+ def self.parse_chunk(data)
34
+ header = data[0..1]
35
+ raise NotChunkedDataError, "This doesn't look like a Chunked GELF message!" if header != CHUNKED_MAGIC
36
+ begin
37
+ msg_id = data[2..9].unpack('C*').join
38
+ seq_number, total_number = data[10].ord, data[11].ord
39
+ zlib_chunk = data[12..-1]
40
+ raise TooManyChunksError, "#{total_number} greater than #{MAX_CHUNKS}" if total_number > MAX_CHUNKS
41
+ @@chunk_map[msg_id][:total_chunks] = total_number.to_i
42
+ @@chunk_map[msg_id][:chunks].merge!({seq_number.to_i => zlib_chunk})
43
+ msg_id
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ module Gelfd2
2
+ class NotChunkedDataError < StandardError; end
3
+ class DuplicateChunkError < StandardError; end
4
+ class TooManyChunksError < StandardError; end
5
+ class UnknownHeaderError < StandardError; end
6
+ class DecodeError < StandardError; end
7
+ class NotYetImplementedError < StandardError; end
8
+ end
@@ -0,0 +1,16 @@
1
+ require 'zlib'
2
+ module Gelfd2
3
+ class GzipParser
4
+
5
+ def self.parse(data)
6
+ begin
7
+ t = Zlib::GzipReader.new(StringIO.new(data))
8
+ t.read
9
+ #raise NotYetImplementedError, "GZip decoding is not yet implemented"
10
+ rescue Exception => e
11
+ raise DecodeError, "Failed to decode data: #{e}"
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module Gelfd2
2
+ class Parser
3
+
4
+ def self.parse(data)
5
+ header = data[0..1]
6
+ case header
7
+ when ZLIB_MAGIC
8
+ ZlibParser.parse(data)
9
+ when CHUNKED_MAGIC
10
+ ChunkedParser.parse(data)
11
+ when GZIP_MAGIC
12
+ GzipParser.parse(data)
13
+ else
14
+ # by default assume the payload to be "raw, uncompressed" GELF, parsing will fail if it's malformed.
15
+ data
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+
22
+
@@ -0,0 +1,3 @@
1
+ module Gelfd2
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'zlib'
2
+ module Gelfd2
3
+ class ZlibParser
4
+
5
+ def self.parse(data)
6
+ begin
7
+ t = Zlib::Inflate.inflate(data)
8
+ t
9
+ rescue Exception => e
10
+ raise DecodeError, "Failed to decode data: #{e}"
11
+ end
12
+ end
13
+
14
+ end
15
+ end
data/lib/gelfd2.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "gelfd2/version"
2
+
3
+ module Gelfd2
4
+ CHUNKED_MAGIC = [0x1e,0x0f].pack('C*').freeze
5
+ ZLIB_MAGIC = [0x78,0x9c].pack('C*').freeze
6
+ GZIP_MAGIC = [0x1f,0x8b].pack('C*').freeze
7
+ UNCOMPRESSED_MAGIC = [0x1f,0x3c].pack('C*').freeze
8
+ HEADER_LENGTH = 12
9
+ DATA_LENGTH = 8192 - HEADER_LENGTH
10
+ MAX_CHUNKS = 128
11
+ end
12
+
13
+ require File.join(File.dirname(__FILE__), 'gelfd2', 'exceptions')
14
+ require File.join(File.dirname(__FILE__), 'gelfd2', 'zlib_parser')
15
+ require File.join(File.dirname(__FILE__), 'gelfd2', 'gzip_parser')
16
+ require File.join(File.dirname(__FILE__), 'gelfd2', 'chunked_parser')
17
+ require File.join(File.dirname(__FILE__), 'gelfd2', 'parser')
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1 @@
1
+ {"this":"is","my":"boomstick"}
Binary file
@@ -0,0 +1,17 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
2
+ require 'gelfd2'
3
+ require 'json'
4
+ require 'test/unit'
5
+
6
+ class TestChhunkedGelf < Test::Unit::TestCase
7
+ JSON_MESSAGE = '{"version":"1.0","host":"somehost","level":"debug","facility":"myapp","short_message":"boom","full_message":"something failed horribly","file":"myapp.rb","timestamp":1315539095.041,"line":105}'
8
+ FIXTURE_PATH = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
9
+ def test_chunked_message
10
+ files = Dir.glob("#{FIXTURE_PATH}/*.chunk")
11
+ files.each do |file|
12
+ data = File.open("#{file}", "rb") {|f| f.read}
13
+ @t = Gelfd2::Parser.parse(data)
14
+ end
15
+ assert_equal(JSON_MESSAGE, @t)
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
2
+ require 'gelfd2'
3
+ require 'json'
4
+ require 'test/unit'
5
+
6
+ class TestUnchunkedGelf < Test::Unit::TestCase
7
+ FIXTURE_PATH = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
8
+
9
+ def test_unchunked_message
10
+ data = File.open("#{FIXTURE_PATH}/unchunked.zl", "rb") {|f| f.read}
11
+ t = Gelfd2::Parser.parse(data)
12
+ assert_equal('{"this":"is","my":"boomstick"}', t)
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
2
+ require 'gelfd2'
3
+ require 'json'
4
+ require 'test/unit'
5
+
6
+ class TestUncompressedGelf < Test::Unit::TestCase
7
+ FIXTURE_PATH = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
8
+
9
+ def test_uncompressed_message
10
+ data = File.open("#{FIXTURE_PATH}/unchunked.uc", "rb") {|f| f.read}
11
+ t = Gelfd2::Parser.parse(data)
12
+ assert_equal('{"this":"is","my":"boomstick"}', t)
13
+ end
14
+ end
data/test/ts_all.rb ADDED
@@ -0,0 +1,5 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "test")))
2
+ require 'test/unit'
3
+ require 'tc_unchunked'
4
+ require 'tc_uncompressed'
5
+ require 'tc_chunked'
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gelfd2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - John E. Vincent
8
+ - ptQa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-09-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 1.8.3
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 1.8.3
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 11.2.2
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 11.2.2
42
+ - !ruby/object:Gem::Dependency
43
+ name: test-unit
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 3.2.1
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 3.2.1
56
+ description: Standalone implementation of the Graylog Extended Log Format
57
+ email:
58
+ executables:
59
+ - gelfd
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - bin/gelfd
70
+ - gelfd2.gemspec
71
+ - lib/gelfd2.rb
72
+ - lib/gelfd2/chunked_parser.rb
73
+ - lib/gelfd2/exceptions.rb
74
+ - lib/gelfd2/gzip_parser.rb
75
+ - lib/gelfd2/parser.rb
76
+ - lib/gelfd2/version.rb
77
+ - lib/gelfd2/zlib_parser.rb
78
+ - test/fixtures/chunked_10_37.chunk
79
+ - test/fixtures/chunked_11_37.chunk
80
+ - test/fixtures/chunked_12_37.chunk
81
+ - test/fixtures/chunked_13_37.chunk
82
+ - test/fixtures/chunked_14_37.chunk
83
+ - test/fixtures/chunked_15_37.chunk
84
+ - test/fixtures/chunked_16_37.chunk
85
+ - test/fixtures/chunked_17_37.chunk
86
+ - test/fixtures/chunked_18_37.chunk
87
+ - test/fixtures/chunked_19_37.chunk
88
+ - test/fixtures/chunked_1_37.chunk
89
+ - test/fixtures/chunked_20_37.chunk
90
+ - test/fixtures/chunked_21_37.chunk
91
+ - test/fixtures/chunked_22_37.chunk
92
+ - test/fixtures/chunked_23_37.chunk
93
+ - test/fixtures/chunked_24_37.chunk
94
+ - test/fixtures/chunked_25_37.chunk
95
+ - test/fixtures/chunked_26_37.chunk
96
+ - test/fixtures/chunked_27_37.chunk
97
+ - test/fixtures/chunked_28_37.chunk
98
+ - test/fixtures/chunked_29_37.chunk
99
+ - test/fixtures/chunked_2_37.chunk
100
+ - test/fixtures/chunked_30_37.chunk
101
+ - test/fixtures/chunked_31_37.chunk
102
+ - test/fixtures/chunked_32_37.chunk
103
+ - test/fixtures/chunked_33_37.chunk
104
+ - test/fixtures/chunked_34_37.chunk
105
+ - test/fixtures/chunked_35_37.chunk
106
+ - test/fixtures/chunked_36_37.chunk
107
+ - test/fixtures/chunked_37_37.chunk
108
+ - test/fixtures/chunked_3_37.chunk
109
+ - test/fixtures/chunked_4_37.chunk
110
+ - test/fixtures/chunked_5_37.chunk
111
+ - test/fixtures/chunked_6_37.chunk
112
+ - test/fixtures/chunked_7_37.chunk
113
+ - test/fixtures/chunked_8_37.chunk
114
+ - test/fixtures/chunked_9_37.chunk
115
+ - test/fixtures/unchunked.uc
116
+ - test/fixtures/unchunked.zl
117
+ - test/tc_chunked.rb
118
+ - test/tc_unchunked.rb
119
+ - test/tc_uncompressed.rb
120
+ - test/ts_all.rb
121
+ homepage: https://github.com/ptqa/gelfd2
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project: gelfd2
140
+ rubygems_version: 2.4.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Pure ruby gelf server and decoding library
144
+ test_files: []
145
+ has_rdoc: