le 1.9 → 1.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LE.gemspec +1 -1
- data/lib/le.rb +18 -11
- data/lib/le/host.rb +32 -18
- data/lib/le/host/https.rb +45 -0
- data/lib/le/host/https/tcp.rb +73 -0
- metadata +5 -3
- data/lib/le/host/http.rb +0 -110
data/LE.gemspec
CHANGED
data/lib/le.rb
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
#
|
5
|
+
# Logentries Ruby monitoring agent
|
6
|
+
# Copyright 2010,2011 Logentries, Jlizard
|
7
|
+
# Mark Lacomber <marklacomber@gmail.com>
|
8
|
+
#
|
9
|
+
|
1
10
|
require File.join(File.dirname(__FILE__), 'le', 'host')
|
2
11
|
|
3
12
|
require 'logger'
|
@@ -11,21 +20,19 @@ module Le
|
|
11
20
|
host = Le::Host.new(key, location, local)
|
12
21
|
logger = Logger.new(host)
|
13
22
|
|
14
|
-
|
15
|
-
logger.formatter = host.formatter
|
16
|
-
end
|
23
|
+
logger.formatter = host.formatter
|
17
24
|
|
18
25
|
logger
|
19
26
|
end
|
20
27
|
|
21
28
|
def self.checkParams(key, location)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
if key == nil or location == nil
|
30
|
+
puts "\nLE: Incorrect parameters for Logentries Plugin!\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Check if the key is valid UUID format
|
34
|
+
if (key =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) == nil
|
35
|
+
puts "\nLE: It appears the LOGENTRIES_ACCOUNT_KEY you entered is invalid!\n"
|
36
|
+
end
|
30
37
|
end
|
31
38
|
end
|
data/lib/le/host.rb
CHANGED
@@ -1,35 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
#
|
5
|
+
# Logentries Ruby monitoring agent
|
6
|
+
# Copyright 2010,2011 Logentries, Jlizard
|
7
|
+
# Mark Lacomber <marklacomber@gmail.com>
|
8
|
+
#
|
9
|
+
|
1
10
|
module Le
|
2
11
|
module Host
|
12
|
+
|
13
|
+
# Creates a new Logentries host, based on a user-key and location of destination file on logentries,
|
14
|
+
# both must be provided correctly for a connection to be made.
|
3
15
|
|
4
16
|
def self.new(key, location, local)
|
5
17
|
|
6
|
-
Le::Host::
|
18
|
+
Le::Host::HTTPS.new(key, location, local)
|
7
19
|
|
8
20
|
end
|
9
21
|
|
10
|
-
module
|
22
|
+
module HelperMethods
|
23
|
+
|
11
24
|
def formatter
|
12
25
|
proc do |severity, datetime, progname, msg|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
26
|
+
message = "#{datetime} "
|
27
|
+
message << format_message(msg, severity)
|
28
|
+
end
|
29
|
+
end
|
17
30
|
|
18
|
-
def format_message(
|
19
|
-
|
31
|
+
def format_message(msg_in, severity)
|
32
|
+
msg_in = msg_in.lstrip
|
33
|
+
|
34
|
+
msg_out = ""
|
35
|
+
msg_out << "severity=#{severity}, "
|
20
36
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
message_out
|
37
|
+
case msg_in
|
38
|
+
when String
|
39
|
+
msg_out << msg_in
|
40
|
+
else
|
41
|
+
msg_out << msg_in.inspect
|
42
|
+
end
|
43
|
+
msg_out
|
30
44
|
end
|
31
45
|
end
|
32
46
|
end
|
33
47
|
end
|
34
48
|
|
35
|
-
require File.join(File.dirname(__FILE__), 'host', '
|
49
|
+
require File.join(File.dirname(__FILE__), 'host', 'https')
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
#
|
5
|
+
# Logentries Ruby monitoring agent
|
6
|
+
# Copyright 2010,2011 Logentries, Jlizard
|
7
|
+
# Mark Lacomber <marklacomber@gmail.com>
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'socket'
|
11
|
+
require 'openssl'
|
12
|
+
|
13
|
+
require File.join(File.dirname(__FILE__), 'https', 'tcp')
|
14
|
+
|
15
|
+
module Le
|
16
|
+
module Host
|
17
|
+
class HTTPS
|
18
|
+
include Le::Host::HelperMethods
|
19
|
+
|
20
|
+
attr_reader :deliverer, :local_bool
|
21
|
+
|
22
|
+
def initialize(key, location, local)
|
23
|
+
@local_bool = local
|
24
|
+
if not local
|
25
|
+
@deliverer = Le::Host::HTTPS::TCPSOCKET.new(key, location)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def write(message)
|
30
|
+
|
31
|
+
if @local_bool
|
32
|
+
puts message
|
33
|
+
else
|
34
|
+
# Deliver the message to logentries via TCP
|
35
|
+
@deliverer.deliver(message)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def close
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
#
|
5
|
+
# Logentries Ruby monitoring agent
|
6
|
+
# Copyright 2010,2011 Logentries, Jlizard
|
7
|
+
# Mark Lacomber <marklacomber@gmail.com>
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'uri'
|
11
|
+
|
12
|
+
module Le
|
13
|
+
module Host
|
14
|
+
class HTTPS
|
15
|
+
class TCPSOCKET
|
16
|
+
|
17
|
+
attr_accessor :sock, :conn, :key, :location
|
18
|
+
def initialize(key, location)
|
19
|
+
|
20
|
+
@key = key
|
21
|
+
@location = URI::encode(location)
|
22
|
+
begin
|
23
|
+
createSocket(key, location)
|
24
|
+
rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
|
25
|
+
$stderr.puts "WARNING: #{e.class} creating the connection to Logentries. #{e.message}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def createSocket(key, location)
|
30
|
+
|
31
|
+
addr = sprintf('/%s/hosts/%s/?realtime=1', key, location)
|
32
|
+
|
33
|
+
# Open the TCP connection to the Logentries Server
|
34
|
+
@sock = TCPSocket.new('api.logentries.com', 443)
|
35
|
+
|
36
|
+
@conn = OpenSSL::SSL::SSLSocket.new(@sock, OpenSSL::SSL::SSLContext.new())
|
37
|
+
@conn.sync_close = true
|
38
|
+
@conn.connect
|
39
|
+
|
40
|
+
# Set up connection with Logentries API to receive messages in chunks, i.e, logs
|
41
|
+
request = sprintf("PUT %s HTTP/1.1\r\n\r\n", addr)
|
42
|
+
@conn.write(request)
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def deliver(message)
|
47
|
+
|
48
|
+
if @conn == nil
|
49
|
+
begin
|
50
|
+
createSocket(@key, @location)
|
51
|
+
rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
|
52
|
+
$stderr.puts "WARNING: #{e.class} Could not write log. No connection to Logentries #{e.message}"
|
53
|
+
return
|
54
|
+
end
|
55
|
+
end
|
56
|
+
# Sends the log to the Logentries Server
|
57
|
+
begin
|
58
|
+
@conn.print(message + "\r\n")
|
59
|
+
rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ENOTCONN, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
|
60
|
+
$stderr.puts "WARNING: #{e.class} sending log #{e.message}"
|
61
|
+
begin
|
62
|
+
createSocket(@key, @location)
|
63
|
+
rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
|
64
|
+
$stderr.puts "WARNING: #{e.class} creating the connection to Logentries. #{e.message}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: le
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 49
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 9
|
9
|
-
|
9
|
+
- 1
|
10
|
+
version: 1.9.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Mark Lacomber (Logentries)
|
@@ -30,7 +31,8 @@ extra_rdoc_files: []
|
|
30
31
|
files:
|
31
32
|
- LE.gemspec
|
32
33
|
- ./lib/le/host.rb
|
33
|
-
- ./lib/le/host/
|
34
|
+
- ./lib/le/host/https/tcp.rb
|
35
|
+
- ./lib/le/host/https.rb
|
34
36
|
- ./lib/le.rb
|
35
37
|
homepage:
|
36
38
|
licenses: []
|
data/lib/le/host/http.rb
DELETED
@@ -1,110 +0,0 @@
|
|
1
|
-
require 'socket'
|
2
|
-
require 'openssl'
|
3
|
-
require 'thread'
|
4
|
-
require 'uri'
|
5
|
-
|
6
|
-
module Le
|
7
|
-
module Host
|
8
|
-
class HTTP
|
9
|
-
include Le::Host::InstanceMethods
|
10
|
-
attr_accessor :key, :location, :queue, :started, :thread, :sock, :conn, :local
|
11
|
-
|
12
|
-
def initialize(key, location, local)
|
13
|
-
@key = key
|
14
|
-
@location = URI::encode(location)
|
15
|
-
@local = local
|
16
|
-
@queue = Queue.new
|
17
|
-
@started = false
|
18
|
-
end
|
19
|
-
|
20
|
-
def write(message)
|
21
|
-
if @local then
|
22
|
-
puts message
|
23
|
-
return
|
24
|
-
end
|
25
|
-
|
26
|
-
@queue << "#{message}\r\n"
|
27
|
-
|
28
|
-
if not @started then
|
29
|
-
puts "LE: Starting asynchronous socket writer"
|
30
|
-
@thread = Thread.new{run()}
|
31
|
-
@started = true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def close
|
36
|
-
puts "LE: Closing asynchronous socket writer"
|
37
|
-
@thread.raise Interrupt
|
38
|
-
end
|
39
|
-
|
40
|
-
def openConnection
|
41
|
-
addr = sprintf('/%s/hosts/%s/?realtime=1', @key, @location)
|
42
|
-
|
43
|
-
puts "LE: Reopening connection to Logentries API server"
|
44
|
-
@sock = TCPSocket.new('api.logentries.com', 443)
|
45
|
-
|
46
|
-
@conn = OpenSSL::SSL::SSLSocket.new(@sock, OpenSSL::SSL::SSLContext.new())
|
47
|
-
@conn.sync_close = true
|
48
|
-
@conn.connect
|
49
|
-
|
50
|
-
headers = sprintf("PUT %s HTTP/1.1\r\n\r\n", addr)
|
51
|
-
@conn.write(headers)
|
52
|
-
|
53
|
-
puts "LE: Connection established"
|
54
|
-
end
|
55
|
-
|
56
|
-
def reopenConnection
|
57
|
-
closeConnection
|
58
|
-
root_delay = 0.1
|
59
|
-
while true
|
60
|
-
begin
|
61
|
-
openConnection
|
62
|
-
break
|
63
|
-
rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
|
64
|
-
puts "LE: Unable to connect to Logentries"
|
65
|
-
end
|
66
|
-
root_delay *= 2
|
67
|
-
if root_delay >= 10 then
|
68
|
-
root_delay = 10
|
69
|
-
end
|
70
|
-
wait_for = (root_delay + rand(root_delay)).to_i
|
71
|
-
puts "LE: Waiting for " + wait_for.to_s + "ms"
|
72
|
-
sleep(wait_for)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def closeConnection
|
77
|
-
if @conn != nil
|
78
|
-
@conn.sysclose
|
79
|
-
@conn = nil
|
80
|
-
end
|
81
|
-
if @sock != nil
|
82
|
-
@sock.close
|
83
|
-
@sock = nil
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def run
|
88
|
-
begin
|
89
|
-
reopenConnection
|
90
|
-
|
91
|
-
while true
|
92
|
-
data = @queue.pop
|
93
|
-
while true
|
94
|
-
begin
|
95
|
-
@conn.write(data)
|
96
|
-
rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEOUT, EOFError => e
|
97
|
-
reopenConnection
|
98
|
-
next
|
99
|
-
end
|
100
|
-
break
|
101
|
-
end
|
102
|
-
end
|
103
|
-
rescue Interrupt
|
104
|
-
puts "LE: Asynchronous socket writer interrupted"
|
105
|
-
end
|
106
|
-
closeConnection
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|