gelf 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +3 -0
- data/Rakefile +10 -0
- data/gelf.gemspec +30 -0
- data/lib/gelf.rb +83 -0
- metadata +74 -0
data/Manifest
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('gelf', '0.9.1') do |p|
|
6
|
+
p.description = "Library to send Graylog2 Extended Log Format (GELF) messages"
|
7
|
+
p.url = "http://www.graylog2.org/"
|
8
|
+
p.author = "Lennart Koopmann"
|
9
|
+
p.email = "lennart@socketfeed.com"
|
10
|
+
end
|
data/gelf.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{gelf}
|
5
|
+
s.version = "0.9.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Lennart Koopmann"]
|
9
|
+
s.date = %q{2010-10-08}
|
10
|
+
s.description = %q{Library to send Graylog2 Extended Log Format (GELF) messages}
|
11
|
+
s.email = %q{lennart@socketfeed.com}
|
12
|
+
s.extra_rdoc_files = ["lib/gelf.rb"]
|
13
|
+
s.files = ["Rakefile", "lib/gelf.rb", "Manifest", "gelf.gemspec"]
|
14
|
+
s.homepage = %q{http://www.graylog2.org/}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Gelf"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{gelf}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{Library to send Graylog2 Extended Log Format (GELF) messages}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/lib/gelf.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'json'
|
3
|
+
require 'socket'
|
4
|
+
require 'zlib'
|
5
|
+
require 'digest/sha2'
|
6
|
+
|
7
|
+
class Gelf
|
8
|
+
|
9
|
+
MAX_CHUNK_SIZE = 8154
|
10
|
+
|
11
|
+
attr_accessor :short_message, :full_message, :level, :host, :type, :line, :file
|
12
|
+
|
13
|
+
def initialize hostname, port
|
14
|
+
@hostname = hostname
|
15
|
+
@port = port
|
16
|
+
end
|
17
|
+
|
18
|
+
def send
|
19
|
+
# Check if all required parameters are set.
|
20
|
+
if self.short_message == nil or self.host == nil
|
21
|
+
raise "Missing required information. Attributes short_message and host must be set."
|
22
|
+
end
|
23
|
+
|
24
|
+
data = {
|
25
|
+
"short_message" => self.short_message,
|
26
|
+
"full_message" => self.full_message,
|
27
|
+
"level" => self.level,
|
28
|
+
"host" => self.host,
|
29
|
+
"type" => self.type,
|
30
|
+
"line" => self.line,
|
31
|
+
"file" => self.file
|
32
|
+
}
|
33
|
+
|
34
|
+
# Convert to JSON and deflate (ZLIB)
|
35
|
+
data = Zlib::Deflate.deflate(data.to_json)
|
36
|
+
|
37
|
+
# Create a socket to send the data.
|
38
|
+
sock = UDPSocket.open
|
39
|
+
|
40
|
+
# Maximum total size is 8192 byte for UDP datagram. Split to chunks if bigger. (GELFv2 supports chunking)
|
41
|
+
if data.length > MAX_CHUNK_SIZE
|
42
|
+
# Too big for one datagram. Send in chunks.
|
43
|
+
|
44
|
+
# Build a message ID.
|
45
|
+
msg_id = Time.now.to_f.to_s + rand(10000).to_s
|
46
|
+
|
47
|
+
# Split data to chunks
|
48
|
+
data_chunks = Array.new
|
49
|
+
data.chars.each_slice(MAX_CHUNK_SIZE){|slice| data_chunks << slice.join}
|
50
|
+
|
51
|
+
# Send every chunk
|
52
|
+
i = 0
|
53
|
+
data_chunks.each do |chunk|
|
54
|
+
sock.send prepend_chunk_data(chunk, msg_id, i, data_chunks.size), 0, @hostname, @port
|
55
|
+
i += 1
|
56
|
+
end
|
57
|
+
else
|
58
|
+
# Data fits in datagram without chunking. Send!
|
59
|
+
sock.send data, 0, @hostname, @port
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def prepend_chunk_data data, msg_id, sequence_number, sequence_count
|
64
|
+
raise "Data must be a string and not be empty." if data == nil or data.length == 0
|
65
|
+
raise "Message ID must be a string and not be empty." if msg_id == nil or msg_id.length == 0
|
66
|
+
raise "Sequence count must be bigger than 0." if sequence_count <= 0
|
67
|
+
raise "Sequence number must not be higher than sequence count." if sequence_number > sequence_count
|
68
|
+
|
69
|
+
# Get raw binary (packed) GELF ID
|
70
|
+
gelf_id_bin = [ 30, 15 ].pack('CC')
|
71
|
+
|
72
|
+
# Get raw binary SHA256 hash of message ID
|
73
|
+
digest = Digest::SHA256.new << msg_id
|
74
|
+
msg_id_bin = digest.digest
|
75
|
+
|
76
|
+
# Get raw binary (packed) sequence count and number
|
77
|
+
sequence_nums_bin = [ sequence_number, sequence_count ].pack('nn');
|
78
|
+
|
79
|
+
# Combine and prepend to message chunk
|
80
|
+
return gelf_id_bin + msg_id_bin + sequence_nums_bin + data
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gelf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 57
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 9
|
9
|
+
- 1
|
10
|
+
version: 0.9.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Lennart Koopmann
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-08 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Library to send Graylog2 Extended Log Format (GELF) messages
|
23
|
+
email: lennart@socketfeed.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- lib/gelf.rb
|
30
|
+
files:
|
31
|
+
- Rakefile
|
32
|
+
- lib/gelf.rb
|
33
|
+
- Manifest
|
34
|
+
- gelf.gemspec
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://www.graylog2.org/
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --line-numbers
|
42
|
+
- --inline-source
|
43
|
+
- --title
|
44
|
+
- Gelf
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 11
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 2
|
65
|
+
version: "1.2"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: gelf
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Library to send Graylog2 Extended Log Format (GELF) messages
|
73
|
+
test_files: []
|
74
|
+
|