le 1.8.1 → 1.9

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.
data/LE.gemspec CHANGED
@@ -3,7 +3,7 @@ require File.expand_path(File.join(dir, 'lib', 'le'))
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "le"
6
- s.version = "1.8.1"
6
+ s.version = "1.9"
7
7
  s.date = Time.now
8
8
  s.summary = "Logentries plugin"
9
9
  s.description =<<EOD
data/lib/le.rb CHANGED
@@ -1,12 +1,3 @@
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
1
  require File.join(File.dirname(__FILE__), 'le', 'host')
11
2
 
12
3
  require 'logger'
@@ -20,19 +11,21 @@ module Le
20
11
  host = Le::Host.new(key, location, local)
21
12
  logger = Logger.new(host)
22
13
 
23
- logger.formatter = host.formatter
14
+ if host.respond_to?(:formatter)
15
+ logger.formatter = host.formatter
16
+ end
24
17
 
25
18
  logger
26
19
  end
27
20
 
28
21
  def self.checkParams(key, location)
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
22
+ if key == nil or location == nil
23
+ puts "\nLE: Incorrect parameters for Logentries Plugin!\n"
24
+ end
25
+
26
+ # Check if the key is valid UUID format
27
+ if (key =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) == nil
28
+ puts "\nLE: It appears the LOGENTRIES_ACCOUNT_KEY you entered is invalid!\n"
29
+ end
37
30
  end
38
31
  end
data/lib/le/host.rb CHANGED
@@ -1,43 +1,35 @@
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
1
  module Le
11
2
  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.
15
3
 
16
4
  def self.new(key, location, local)
17
5
 
18
- Le::Host::HTTPS.new(key, location, local)
6
+ Le::Host::HTTP.new(key, location, local)
19
7
 
20
8
  end
21
9
 
22
- module HelperMethods
23
-
10
+ module InstanceMethods
24
11
  def formatter
25
12
  proc do |severity, datetime, progname, msg|
26
- message = "#{datetime} "
27
- message << format_message(msg, severity)
28
- end
29
- end
13
+ message = "#{datetime} "
14
+ message << format_message(msg, severity)
15
+ end
16
+ end
30
17
 
31
- def format_message(msg_in, severity)
32
- msg_out = ""
33
- msg_out << "severity=#{severity}, "
18
+ def format_message(message_in, severity)
19
+ message_in = message_in.lstrip
34
20
 
35
- msg_out << msg_in
36
-
37
- msg_out
21
+ message_out = ""
22
+ message_out = "severity=#{severity}, "
23
+ case message_in
24
+ when String
25
+ message_out << message_in
26
+ else
27
+ message_out << message_in.inspect
28
+ end
29
+ message_out
38
30
  end
39
31
  end
40
32
  end
41
33
  end
42
34
 
43
- require File.join(File.dirname(__FILE__), 'host', 'https')
35
+ require File.join(File.dirname(__FILE__), 'host', 'http')
@@ -0,0 +1,110 @@
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
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: le
3
3
  version: !ruby/object:Gem::Version
4
- hash: 53
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 8
9
- - 1
10
- version: 1.8.1
8
+ - 9
9
+ version: "1.9"
11
10
  platform: ruby
12
11
  authors:
13
12
  - Mark Lacomber (Logentries)
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2012-07-17 00:00:00 Z
17
+ date: 2012-07-25 00:00:00 Z
19
18
  dependencies: []
20
19
 
21
20
  description: |
@@ -31,8 +30,7 @@ extra_rdoc_files: []
31
30
  files:
32
31
  - LE.gemspec
33
32
  - ./lib/le/host.rb
34
- - ./lib/le/host/https/tcp.rb
35
- - ./lib/le/host/https.rb
33
+ - ./lib/le/host/http.rb
36
34
  - ./lib/le.rb
37
35
  homepage:
38
36
  licenses: []
data/lib/le/host/https.rb DELETED
@@ -1,45 +0,0 @@
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
@@ -1,74 +0,0 @@
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
- module Le
11
- module Host
12
- class HTTPS
13
-
14
- class TCPSOCKET
15
-
16
- attr_accessor :sock, :conn, :key, :location
17
- def initialize(key, location)
18
-
19
- @key = key
20
- @location = location
21
- begin
22
- createSocket(key, location)
23
- rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
24
- $stderr.puts "WARNING: #{e.class} creating the connection to Logentries. #{e.message}"
25
- end
26
- end
27
-
28
- def createSocket(key, location)
29
-
30
- addr = sprintf('/%s/hosts/%s/?realtime=1', key, location)
31
-
32
- # Open the TCP connection to the Logentries Server
33
- @sock = TCPSocket.new('api.logentries.com', 443)
34
-
35
- @conn = OpenSSL::SSL::SSLSocket.new(@sock, OpenSSL::SSL::SSLContext.new())
36
- @conn.sync_close = true
37
- @conn.connect
38
-
39
- # Set up connection with Logentries API to receive messages in chunks, i.e, logs
40
- request = sprintf("PUT %s HTTP/1.1\r\n", addr)
41
- @conn.print(request)
42
- @conn.print("Accept-Encoding: identity\r\n")
43
- @conn.print("Transfer_Encoding: chunked\r\n\r\n")
44
-
45
- end
46
-
47
- def deliver(message)
48
-
49
- if @conn == nil
50
- begin
51
- createSocket(@key, @location)
52
- rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
53
- $stderr.puts "WARNING: #{e.class} Could not write log. No connection to Logentries #{e.message}"
54
- return
55
- end
56
- end
57
- # Sends the log to the Logentries Server
58
- begin
59
- @conn.print(message + "\r\n")
60
- rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ENOTCONN, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
61
- $stderr.puts "WARNING: #{e.class} sending log #{e.message}"
62
- begin
63
- createSocket(@key, @location)
64
- rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
65
- $stderr.puts "WARNING: #{e.class} creating the connection to Logentries. #{e.message}"
66
- end
67
- end
68
- end
69
-
70
- end
71
-
72
- end
73
- end
74
- end