le 2.0 → 2.1

Sign up to get free protection for your applications and to get access to all the features.
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 = "2.0"
6
+ s.version = "2.1"
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,17 @@ module Le
20
11
  host = Le::Host.new(token, 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(token)
29
- if token == nil
30
- puts "\nLE: Incorrect token parameter for Logentries Plugin!\n"
31
- end
32
-
33
- # Check if the key is valid UUID format
34
- if (token =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) == nil
35
- puts "\nLE: It appears the LOGENTRIES_TOKEN you entered is invalid!\n"
36
- end
22
+ # Check if the key is valid UUID format
23
+ if (token =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) == nil
24
+ puts "\nLE: It appears the LOGENTRIES_TOKEN you entered is invalid!\n"
25
+ end
37
26
  end
38
27
  end
@@ -1,48 +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(token, local)
17
5
 
18
- Le::Host::HTTPS.new(token, local)
6
+ Le::Host::HTTP.new(token, 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_in = msg_in.lstrip
33
- msg_out = ""
34
- msg_out << "severity=#{severity}, "
18
+ def format_message(message_in, severity)
19
+ message_in = message_in.lstrip
35
20
 
36
- case msg_in
37
- when String
38
- msg_out << msg_in
39
- else
40
- msg_out << msg_in.inspect
41
- end
42
- 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
43
30
  end
44
31
  end
45
32
  end
46
33
  end
47
34
 
48
- require File.join(File.dirname(__FILE__), 'host', 'https')
35
+ require File.join(File.dirname(__FILE__), 'host', 'http')
@@ -0,0 +1,103 @@
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 :token, :queue, :started, :thread, :conn, :local
11
+
12
+ def initialize(token, local)
13
+ @token = token
14
+ @local = local
15
+ @queue = Queue.new
16
+ @started = false
17
+ end
18
+
19
+ def write(message)
20
+ if @local then
21
+ puts message
22
+ return
23
+ end
24
+
25
+ @queue << "#{@token}#{message}\n"
26
+
27
+ if not @started then
28
+ puts "LE: Starting asynchronous socket writer"
29
+ @thread = Thread.new{run()}
30
+ @started = true
31
+ end
32
+ end
33
+
34
+ def close
35
+ puts "LE: Closing asynchronous socket writer"
36
+ @thread.raise Interrupt
37
+ end
38
+
39
+ def openConnection
40
+ puts "LE: Reopening connection to Logentries API server"
41
+ @conn = TCPSocket.new('api.logentries.com', 10000)
42
+
43
+ #@conn = OpenSSL::SSL::SSLSocket.new(@sock, OpenSSL::SSL::SSLContext.new())
44
+ #@conn.connect
45
+
46
+ puts "LE: Connection established"
47
+ end
48
+
49
+ def reopenConnection
50
+ closeConnection
51
+ root_delay = 0.1
52
+ while true
53
+ begin
54
+ openConnection
55
+ break
56
+ rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
57
+ puts "LE: Unable to connect to Logentries"
58
+ end
59
+ root_delay *= 2
60
+ if root_delay >= 10 then
61
+ root_delay = 10
62
+ end
63
+ wait_for = (root_delay + rand(root_delay)).to_i
64
+ puts "LE: Waiting for " + wait_for.to_s + "ms"
65
+ sleep(wait_for)
66
+ end
67
+ end
68
+
69
+ def closeConnection
70
+ if @conn != nil
71
+ @conn.sysclose
72
+ @conn = nil
73
+ end
74
+ #if @sock != nil
75
+ # @sock.close
76
+ # @sock = nil
77
+ #end
78
+ end
79
+
80
+ def run
81
+ begin
82
+ reopenConnection
83
+
84
+ while true
85
+ data = @queue.pop
86
+ while true
87
+ begin
88
+ @conn.write(data)
89
+ rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEOUT, EOFError => e
90
+ reopenConnection
91
+ next
92
+ end
93
+ break
94
+ end
95
+ end
96
+ rescue Interrupt
97
+ puts "LE: Asynchronous socket writer interrupted"
98
+ end
99
+ closeConnection
100
+ end
101
+ end
102
+ end
103
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: le
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.0'
4
+ version: '2.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-28 00:00:00.000000000 Z
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! '
15
15
 
@@ -22,8 +22,7 @@ files:
22
22
  - LE.gemspec
23
23
  - ./lib/le.rb
24
24
  - ./lib/le/host.rb
25
- - ./lib/le/host/https.rb
26
- - ./lib/le/host/https/tcp.rb
25
+ - ./lib/le/host/http.rb
27
26
  homepage:
28
27
  licenses: []
29
28
  post_install_message:
@@ -1,42 +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 File.join(File.dirname(__FILE__), 'https', 'tcp')
11
-
12
- module Le
13
- module Host
14
- class HTTPS
15
- include Le::Host::HelperMethods
16
-
17
- attr_reader :deliverer, :local_bool
18
-
19
- def initialize(token, local)
20
- @local_bool = local
21
- if not local
22
- @deliverer = Le::Host::HTTPS::TCPSOCKET.new(token)
23
- end
24
- end
25
-
26
- def write(message)
27
-
28
- if @local_bool
29
- puts message
30
- else
31
- # Deliver the message to logentries via TCP
32
- @deliverer.deliver(message)
33
- end
34
- end
35
-
36
- def close
37
- nil
38
- end
39
-
40
- end
41
- end
42
- end
@@ -1,64 +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
- module Le
14
- module Host
15
- class HTTPS
16
-
17
- class TCPSOCKET
18
-
19
- attr_accessor :conn, :token
20
- def initialize(token)
21
-
22
- @token = token
23
- begin
24
- createSocket()
25
- rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
26
- $stderr.puts "WARNING: #{e.class} Could not create the connection to Logentries. #{e.message}"
27
- end
28
- end
29
-
30
- def createSocket()
31
-
32
- # Open the TCP connection to the Logentries Server
33
- @conn = TCPSocket.new('api.logentries.com', 10000)
34
-
35
- end
36
-
37
- def deliver(message)
38
-
39
- if @conn == nil
40
- begin
41
- createSocket()
42
- rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
43
- $stderr.puts "WARNING: #{e.class} Could not write log. No connection to Logentries #{e.message}"
44
- return
45
- end
46
- end
47
- # Sends the log to the Logentries Server
48
- begin
49
- @conn.puts(@token + message)
50
- rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ENOTCONN, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
51
- $stderr.puts "WARNING: #{e.class} Could not send log to Logentries #{e.message}"
52
- begin
53
- createSocket()
54
- rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
55
- $stderr.puts "WARNING: #{e.class} Could not create the connection to Logentries. #{e.message}"
56
- end
57
- end
58
- end
59
-
60
- end
61
-
62
- end
63
- end
64
- end