r7insight 2.7.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +52 -0
- data/Gemfile +3 -0
- data/LE.gemspec +29 -0
- data/LICENSE +21 -0
- data/README.md +82 -0
- data/Rakefile +16 -0
- data/lib/le/host/connection.rb +315 -0
- data/lib/le/host.rb +26 -0
- data/lib/le.rb +65 -0
- data/test/fixtures/log/.gitignore +1 -0
- data/test/host_spec.rb +23 -0
- data/test/http_spec.rb +33 -0
- data/test/le_spec.rb +142 -0
- data/test/region.rb +37 -0
- data/test/spec_helper.rb +2 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8a1f559d3e82a3236af427283f92106b8f10bb88
|
4
|
+
data.tar.gz: 2c8b419df892a83ec90094018f1132fa781e8902
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c7f636e0a73c2138d9128923425b7ac4a691ab80db107fbdda4f7aa18d1c6746ab52c841f4ed63d9476787cf70d6606ce3bd1d154c8ea4dc2e2f7d3efbc4356
|
7
|
+
data.tar.gz: c90280cedd4f3eb31cc3a4d5f6585af1d3c5772276382870e005cc956c859c323d90eba727cfd2518c9b01987b0c06b692d169afcb6866ee5aa5966f7bbcd736
|
data/.gitignore
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Created by .ignore support plugin (hsz.mobi)
|
2
|
+
### Ruby template
|
3
|
+
*.gem
|
4
|
+
*.rbc
|
5
|
+
/.config
|
6
|
+
/coverage/
|
7
|
+
/InstalledFiles
|
8
|
+
/pkg/
|
9
|
+
/spec/reports/
|
10
|
+
/spec/examples.txt
|
11
|
+
/test/tmp/
|
12
|
+
/test/version_tmp/
|
13
|
+
/tmp/
|
14
|
+
|
15
|
+
# Used by dotenv library to load environment variables.
|
16
|
+
# .env
|
17
|
+
|
18
|
+
## Specific to RubyMotion:
|
19
|
+
.dat*
|
20
|
+
.repl_history
|
21
|
+
build/
|
22
|
+
*.bridgesupport
|
23
|
+
build-iPhoneOS/
|
24
|
+
build-iPhoneSimulator/
|
25
|
+
|
26
|
+
## Specific to RubyMotion (use of CocoaPods):
|
27
|
+
#
|
28
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
29
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
30
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
31
|
+
#
|
32
|
+
# vendor/Pods/
|
33
|
+
|
34
|
+
## Documentation cache and generated files:
|
35
|
+
/.yardoc/
|
36
|
+
/_yardoc/
|
37
|
+
/doc/
|
38
|
+
/rdoc/
|
39
|
+
|
40
|
+
## Environment normalization:
|
41
|
+
/.bundle/
|
42
|
+
/vendor/bundle
|
43
|
+
/lib/bundler/man/
|
44
|
+
|
45
|
+
# for a library or gem, you might want to ignore these files since the code is
|
46
|
+
# intended to run in multiple environments; otherwise, check them in:
|
47
|
+
# Gemfile.lock
|
48
|
+
# .ruby-version
|
49
|
+
# .ruby-gemset
|
50
|
+
|
51
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
52
|
+
.rvmrc
|
data/Gemfile
ADDED
data/LE.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'le'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "r7insight"
|
8
|
+
gem.version = "2.7.5"
|
9
|
+
gem.date = Time.now
|
10
|
+
gem.summary = "InsightOps logging plugin"
|
11
|
+
gem.licenses = ["MIT"]
|
12
|
+
gem.description =<<EOD
|
13
|
+
|
14
|
+
|
15
|
+
EOD
|
16
|
+
|
17
|
+
gem.authors = ["Mark Lacomber", "Stephen Hynes"]
|
18
|
+
gem.email = "support@rapid7.com"
|
19
|
+
gem.homepage = "https://github.com/rapid7/r7insight_ruby"
|
20
|
+
gem.files = `git ls-files`.split($/)
|
21
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
22
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
23
|
+
gem.require_paths = ["lib"]
|
24
|
+
|
25
|
+
gem.add_development_dependency "bundler"
|
26
|
+
gem.add_development_dependency "rake"
|
27
|
+
gem.add_development_dependency 'minitest'
|
28
|
+
|
29
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2014 Logentries
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
Logging to InsightOps in Ruby
|
2
|
+
=============================
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/rapid7/r7insight_ruby.svg?branch=master)](https://travis-ci.org/rapid7/r7insight_ruby)
|
5
|
+
This is a Rapid7 library for logging from Ruby platforms to InsightOps, including Heroku.
|
6
|
+
|
7
|
+
It is available on github <https://github.com/rapid7/r7insight_ruby/> and rubygems
|
8
|
+
<http://rubygems.org/>.
|
9
|
+
|
10
|
+
|
11
|
+
Example
|
12
|
+
-------
|
13
|
+
|
14
|
+
Rails.logger.info("information message")
|
15
|
+
Rails.logger.warn("warning message")
|
16
|
+
Rails.logger.debug("debug message")
|
17
|
+
|
18
|
+
|
19
|
+
Howto
|
20
|
+
-----
|
21
|
+
|
22
|
+
You must first register your account details with Rapid7 InsightOps.
|
23
|
+
|
24
|
+
Once you have logged in to InsightOps, create a new host with a name of your choice.
|
25
|
+
Inside this host, create a new logfile, selecting `Token TCP` (or `Plain TCP/UDP` if using UDP)
|
26
|
+
as the source type.
|
27
|
+
|
28
|
+
Heroku
|
29
|
+
------
|
30
|
+
|
31
|
+
To install the gem you must edit the Gemfile in your local heroku environment
|
32
|
+
|
33
|
+
Add the following line:
|
34
|
+
|
35
|
+
gem 'r7insight'
|
36
|
+
|
37
|
+
Then from the cmd line run the following command:
|
38
|
+
|
39
|
+
bundle install
|
40
|
+
|
41
|
+
This will install the gem on your local environment.
|
42
|
+
|
43
|
+
The next step is to configure the default rails logger to use the insightops logger.
|
44
|
+
|
45
|
+
|
46
|
+
In your environment configuration file ( for production : `config/environments/production.rb`), add the following:
|
47
|
+
|
48
|
+
Rails.logger = Le.new('LOG_TOKEN', 'REGION')
|
49
|
+
|
50
|
+
If you want to keep logging locally in addition to sending logs to in, just add local parameter after the key.
|
51
|
+
By default, this will write to the standard Rails log or to STDOUT if not using Rails:
|
52
|
+
|
53
|
+
Rails.logger = Le.new('LOG_TOKEN', 'REGION', :local => true)
|
54
|
+
|
55
|
+
You may specify the local log device by providing a filename (String) or IO object (typically STDOUT, STDERR, or an open file):
|
56
|
+
|
57
|
+
Rails.logger = Le.new('LOG_TOKEN', 'REGION', :local => 'log/my_custom_log.log')
|
58
|
+
|
59
|
+
If you want the gem to use SSL when streaming logs to insightops, add the ssl parameter and set it to true:
|
60
|
+
|
61
|
+
Rails.logger = Le.new('LOG_TOKEN', 'REGION', :ssl => true)
|
62
|
+
|
63
|
+
If you want to print debug messages for the gem to a file called r7insightGem.log, add this:
|
64
|
+
|
65
|
+
Rails.logger = Le.new('LOG_TOKEN', 'REGION', :debug => true)
|
66
|
+
|
67
|
+
If you want to use ActiveSupport::TaggedLogging logging, add this:
|
68
|
+
|
69
|
+
Rails.logger = Le.new('LOG_TOKEN', 'REGION', :tag => true)
|
70
|
+
|
71
|
+
You can also specify the default level of the logger by adding a :
|
72
|
+
|
73
|
+
Rails.logger = Le.new('LOG_TOKEN', 'REGION', :log_level => Logger::<level>)
|
74
|
+
|
75
|
+
For the `LOG_TOKEN` argument, paste the token for the logfile you created earlier in the InsightOps UI or empty string for
|
76
|
+
a UDP connection.
|
77
|
+
|
78
|
+
For the `REGION` argument, provide the region of your account, e.g: 'eu', 'us'.
|
79
|
+
|
80
|
+
Additionally, when connecting via UDP, be sure to specify a port using the udp_port parameter:
|
81
|
+
|
82
|
+
Rails.logger = Le.new('', :udp_port => 13287)
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new do |t|
|
5
|
+
t.libs = ['lib','test']
|
6
|
+
t.test_files = Dir.glob("test/**/*_spec.rb").sort
|
7
|
+
t.verbose = true
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => [:test]
|
11
|
+
task :spec => [:test]
|
12
|
+
|
13
|
+
desc "Open an irb session preloaded with this library"
|
14
|
+
task :console do
|
15
|
+
sh "irb -rubygems -I lib -r le.rb"
|
16
|
+
end
|
@@ -0,0 +1,315 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'openssl'
|
3
|
+
require 'thread'
|
4
|
+
require 'timeout'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module Le
|
8
|
+
module Host
|
9
|
+
class CONNECTION
|
10
|
+
DATA_ENDPOINT = '.data.logs.insight.rapid7.com'
|
11
|
+
DATA_PORT_UNSECURE = 80
|
12
|
+
DATA_PORT_SECURE = 443
|
13
|
+
API_SSL_PORT = 20000
|
14
|
+
SHUTDOWN_COMMAND = "DIE!DIE!" # magic command string for async worker to shutdown
|
15
|
+
SHUTDOWN_MAX_WAIT = 10 # max seconds to wait for queue to clear on shutdown
|
16
|
+
SHUTDOWN_WAIT_STEP = 0.2 # sleep duration (seconds) while waiting to shutdown
|
17
|
+
|
18
|
+
|
19
|
+
include Le::Host::InstanceMethods
|
20
|
+
#! attr_accessor :token, :queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :dathub_ip, :datahub_port, :host_id, :custom_host, :host_name_enabled, :host_name
|
21
|
+
attr_accessor :token, :region,:queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :datahub_ip, :datahub_port, :datahub_endpoint, :host_id, :host_name_enabled, :host_name, :custom_host, :udp_port, :use_data_endpoint
|
22
|
+
|
23
|
+
|
24
|
+
def initialize(token, region, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint)
|
25
|
+
if local
|
26
|
+
device = if local.class <= TrueClass
|
27
|
+
if defined?(Rails)
|
28
|
+
Rails.root.join("log","#{Rails.env}.log")
|
29
|
+
else
|
30
|
+
STDOUT
|
31
|
+
end
|
32
|
+
else
|
33
|
+
local
|
34
|
+
end
|
35
|
+
@logger_console = Logger.new(device)
|
36
|
+
end
|
37
|
+
|
38
|
+
@region = region
|
39
|
+
@local = !!local
|
40
|
+
@debug= debug
|
41
|
+
@ssl = ssl
|
42
|
+
@udp_port = udp_port
|
43
|
+
@use_data_endpoint = use_data_endpoint
|
44
|
+
|
45
|
+
@datahub_endpoint = datahub_endpoint
|
46
|
+
if !@datahub_endpoint[0].empty?
|
47
|
+
@datahub_enabled=true
|
48
|
+
@datahub_ip="#{@datahub_endpoint[0]}"
|
49
|
+
@datahub_port=@datahub_endpoint[1]
|
50
|
+
else
|
51
|
+
@datahub_enabled=false
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
if (@datahub_enabled && @ssl)
|
56
|
+
puts ("\n\nYou Cannot have DataHub and SSL enabled at the same time. Please set SSL value to false in your environment.rb file or used Token-Based logging by leaving the Datahub IP address blank. Exiting application. \n\n")
|
57
|
+
exit
|
58
|
+
end
|
59
|
+
|
60
|
+
#check if region was specified
|
61
|
+
if region.empty?
|
62
|
+
puts ("\n\nYou need to specify a region. Options: eu, us")
|
63
|
+
end
|
64
|
+
|
65
|
+
#check if DataHub is enabled... if datahub is not enabled, set the token to the token's parameter. If DH is enabled, make the token empty.
|
66
|
+
if (!datahub_enabled)
|
67
|
+
@token = token
|
68
|
+
else
|
69
|
+
@token = ''
|
70
|
+
|
71
|
+
#! NOTE THIS @datahub_port conditional MAY NEED TO BE CHANGED IF SSL CAN'T WORK WITH DH
|
72
|
+
@datahub_port = @datahub_port.empty? ? API_SSL_PORT : datahub_port
|
73
|
+
@datahub_ip = datahub_ip
|
74
|
+
end
|
75
|
+
|
76
|
+
@host_name_enabled=custom_host[0];
|
77
|
+
@host_name= custom_host[1];
|
78
|
+
|
79
|
+
|
80
|
+
# Check if host_id is empty -- if not assign, if so, make it an empty string.
|
81
|
+
if !host_id.empty?
|
82
|
+
@host_id = host_id
|
83
|
+
@host_id = "host_id=#{host_id}"
|
84
|
+
else
|
85
|
+
@host_id=''
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
#assign host_name, if no host name is given and host_name_enabled = true... assign a host_name based on the machine name.
|
91
|
+
if @host_name_enabled
|
92
|
+
if host_name.empty?
|
93
|
+
@host_name=Socket.gethostname
|
94
|
+
end
|
95
|
+
|
96
|
+
@host_name="host_name=#{@host_name}"
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
@queue = Queue.new
|
101
|
+
@started = false
|
102
|
+
@thread = nil
|
103
|
+
|
104
|
+
if @debug
|
105
|
+
self.init_debug
|
106
|
+
end
|
107
|
+
at_exit { shutdown! }
|
108
|
+
end
|
109
|
+
|
110
|
+
def init_debug
|
111
|
+
filePath = "r7insightGem.log"
|
112
|
+
if File.exist?('log/')
|
113
|
+
filePath = "log/r7insightGem.log"
|
114
|
+
end
|
115
|
+
@debug_logger = Logger.new(filePath)
|
116
|
+
end
|
117
|
+
|
118
|
+
def dbg(message)
|
119
|
+
if @debug
|
120
|
+
@debug_logger.add(Logger::Severity::DEBUG, message)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def write(message)
|
125
|
+
|
126
|
+
if !host_id.empty?
|
127
|
+
message = "#{message} #{ host_id }"
|
128
|
+
end
|
129
|
+
|
130
|
+
if host_name_enabled
|
131
|
+
message="#{message} #{ host_name }"
|
132
|
+
end
|
133
|
+
|
134
|
+
if @local
|
135
|
+
@logger_console.add(Logger::Severity::UNKNOWN, message)
|
136
|
+
end
|
137
|
+
|
138
|
+
if message.scan(/\n/).empty?
|
139
|
+
@queue << "#{ @token } #{ message } \n"
|
140
|
+
else
|
141
|
+
@queue << "#{ message.gsub(/^/, "\1#{ @token } [#{ random_message_id }]") }\n"
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
if @started
|
146
|
+
check_async_thread
|
147
|
+
else
|
148
|
+
start_async_thread
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def start_async_thread
|
153
|
+
@thread = Thread.new { run() }
|
154
|
+
dbg "LE: Asynchronous socket writer started"
|
155
|
+
@started = true
|
156
|
+
end
|
157
|
+
|
158
|
+
def check_async_thread
|
159
|
+
if not(@thread && @thread.alive?)
|
160
|
+
@thread = Thread.new { run() }
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def close
|
165
|
+
dbg "LE: Closing asynchronous socket writer"
|
166
|
+
@started = false
|
167
|
+
end
|
168
|
+
|
169
|
+
def openConnection
|
170
|
+
dbg "LE: Reopening connection to Logentries API server"
|
171
|
+
|
172
|
+
|
173
|
+
if @use_data_endpoint
|
174
|
+
host = @region + DATA_ENDPOINT
|
175
|
+
|
176
|
+
if @ssl
|
177
|
+
port = DATA_PORT_SECURE
|
178
|
+
else
|
179
|
+
port = DATA_PORT_UNSECURE
|
180
|
+
end
|
181
|
+
else
|
182
|
+
if @udp_port
|
183
|
+
host = @region + DATA_ENDPOINT
|
184
|
+
port = @udp_port
|
185
|
+
elsif @datahub_enabled
|
186
|
+
host = @datahub_ip
|
187
|
+
port = @datahub_port
|
188
|
+
else
|
189
|
+
host = @region + DATA_ENDPOINT
|
190
|
+
port = @ssl ? DATA_PORT_SECURE: DATA_PORT_UNSECURE
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
if @udp_port
|
195
|
+
@conn = UDPSocket.new
|
196
|
+
@conn.connect(host, port)
|
197
|
+
else
|
198
|
+
socket = TCPSocket.new(host, port)
|
199
|
+
|
200
|
+
if @ssl
|
201
|
+
cert_store = OpenSSL::X509::Store.new
|
202
|
+
cert_store.set_default_paths
|
203
|
+
|
204
|
+
ssl_context = OpenSSL::SSL::SSLContext.new()
|
205
|
+
ssl_context.cert_store = cert_store
|
206
|
+
|
207
|
+
ssl_version_candidates = [:TLSv1_2, :TLSv1_1, :TLSv1]
|
208
|
+
ssl_version_candidates = ssl_version_candidates.select { |version| OpenSSL::SSL::SSLContext::METHODS.include? version }
|
209
|
+
if ssl_version_candidates.empty?
|
210
|
+
raise "Could not find suitable TLS version"
|
211
|
+
end
|
212
|
+
# currently we only set the version when we have no choice
|
213
|
+
ssl_context.ssl_version = ssl_version_candidates[0] if ssl_version_candidates.length == 1
|
214
|
+
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
215
|
+
ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
|
216
|
+
ssl_socket.hostname = host if ssl_socket.respond_to?(:hostname=)
|
217
|
+
ssl_socket.sync_close = true
|
218
|
+
Timeout::timeout(10) do
|
219
|
+
ssl_socket.connect
|
220
|
+
end
|
221
|
+
@conn = ssl_socket
|
222
|
+
else
|
223
|
+
@conn = socket
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
dbg "LE: Connection established"
|
228
|
+
end
|
229
|
+
|
230
|
+
def reopenConnection
|
231
|
+
closeConnection
|
232
|
+
root_delay = 0.1
|
233
|
+
loop do
|
234
|
+
begin
|
235
|
+
openConnection
|
236
|
+
break
|
237
|
+
rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError
|
238
|
+
dbg "LE: Unable to connect to Logentries due to timeout(#{ $! })"
|
239
|
+
rescue
|
240
|
+
dbg "LE: Got exception in reopenConnection - #{ $! }"
|
241
|
+
raise
|
242
|
+
end
|
243
|
+
root_delay *= 2
|
244
|
+
if root_delay >= 10
|
245
|
+
root_delay = 10
|
246
|
+
end
|
247
|
+
wait_for = (root_delay + rand(root_delay)).to_i
|
248
|
+
dbg "LE: Waiting for #{ wait_for }ms"
|
249
|
+
sleep(wait_for)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def closeConnection
|
254
|
+
begin
|
255
|
+
if @conn.respond_to?(:sysclose)
|
256
|
+
@conn.sysclose
|
257
|
+
elsif @conn.respond_to?(:close)
|
258
|
+
@conn.close
|
259
|
+
end
|
260
|
+
rescue
|
261
|
+
dbg "LE: couldn't close connection, close with exception - #{ $! }"
|
262
|
+
ensure
|
263
|
+
@conn = nil
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
def run
|
268
|
+
reopenConnection
|
269
|
+
|
270
|
+
loop do
|
271
|
+
data = @queue.pop
|
272
|
+
break if data == SHUTDOWN_COMMAND
|
273
|
+
loop do
|
274
|
+
begin
|
275
|
+
@conn.write(data)
|
276
|
+
rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError
|
277
|
+
dbg "LE: Connection timeout(#{ $! }), try to reopen connection"
|
278
|
+
reopenConnection
|
279
|
+
next
|
280
|
+
rescue
|
281
|
+
dbg("LE: Got exception in run loop - #{ $! }")
|
282
|
+
raise
|
283
|
+
end
|
284
|
+
|
285
|
+
break
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
dbg "LE: Closing Asynchronous socket writer"
|
290
|
+
|
291
|
+
closeConnection
|
292
|
+
end
|
293
|
+
|
294
|
+
private
|
295
|
+
def random_message_id
|
296
|
+
@random_message_id_sample_space ||= ('0'..'9').to_a.concat(('A'..'Z').to_a)
|
297
|
+
(0..5).map{ @random_message_id_sample_space.sample }.join
|
298
|
+
end
|
299
|
+
|
300
|
+
# at_exit handler.
|
301
|
+
# Attempts to clear the queue and terminate the async worker cleanly before process ends.
|
302
|
+
def shutdown!
|
303
|
+
return unless @started
|
304
|
+
dbg "LE: commencing shutdown, queue has #{queue.size} entries to clear"
|
305
|
+
queue << SHUTDOWN_COMMAND
|
306
|
+
SHUTDOWN_MAX_WAIT.div(SHUTDOWN_WAIT_STEP).times do
|
307
|
+
break if queue.empty?
|
308
|
+
sleep SHUTDOWN_WAIT_STEP
|
309
|
+
end
|
310
|
+
dbg "LE: shutdown complete, queue is #{queue.empty? ? '' : 'not '}empty with #{queue.size} entries"
|
311
|
+
end
|
312
|
+
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
data/lib/le/host.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Le
|
2
|
+
module Host
|
3
|
+
|
4
|
+
def self.new(token, region, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint)
|
5
|
+
Le::Host::CONNECTION.new(token, region, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint)
|
6
|
+
end
|
7
|
+
|
8
|
+
module InstanceMethods
|
9
|
+
def formatter
|
10
|
+
proc do |severity, datetime, _, msg|
|
11
|
+
message = "#{datetime} "
|
12
|
+
message << format_message(msg, severity)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def format_message(message_in, severity)
|
17
|
+
message_in = message_in.inspect unless message_in.is_a?(String)
|
18
|
+
|
19
|
+
"severity=#{severity}, #{message_in.lstrip}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require File.join(File.dirname(__FILE__), 'host', 'connection')
|
data/lib/le.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'le', 'host')
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module Le
|
6
|
+
|
7
|
+
def self.new(token, region, options={})
|
8
|
+
|
9
|
+
opt_local = options[:local] || false
|
10
|
+
opt_debug = options[:debug] || false
|
11
|
+
opt_ssl = !options.include?(:ssl) ? true : options[:ssl]
|
12
|
+
opt_tag = options[:tag] || false
|
13
|
+
opt_log_level = options[:log_level] || Logger::DEBUG
|
14
|
+
|
15
|
+
opt_datahub_enabled = options[:datahub_enabled] || false
|
16
|
+
opt_datahub_endpoint = options[:datahub_endpoint] || ['', 10000]
|
17
|
+
opt_datahub_ip = options[:datahub_ip] || ''
|
18
|
+
opt_datahub_port = options[:datahub_port] || 10000
|
19
|
+
opt_host_id = options[:host_id] || ''
|
20
|
+
opt_host_name_enabled = options[:host_name_enabled] || false
|
21
|
+
opt_host_name = options[:host_name] || ''
|
22
|
+
opt_custom_host = options[:custom_host] || [false, '']
|
23
|
+
|
24
|
+
opt_udp_port = options[:udp_port] || nil
|
25
|
+
opt_use_data_endpoint = options[:data_endpoint] || false
|
26
|
+
|
27
|
+
self.checkParams(token, region, opt_datahub_enabled, opt_udp_port)
|
28
|
+
|
29
|
+
|
30
|
+
host = Le::Host.new(token, region, opt_local, opt_debug, opt_ssl, opt_datahub_endpoint, opt_host_id, opt_custom_host, opt_udp_port, opt_use_data_endpoint)
|
31
|
+
|
32
|
+
if defined?(ActiveSupport::TaggedLogging) && opt_tag
|
33
|
+
logger = ActiveSupport::TaggedLogging.new(Logger.new(host))
|
34
|
+
elsif defined?(ActiveSupport::Logger)
|
35
|
+
logger = ActiveSupport::Logger.new(host)
|
36
|
+
logger.formatter = host.formatter if host.respond_to?(:formatter)
|
37
|
+
else
|
38
|
+
logger = Logger.new(host)
|
39
|
+
logger.formatter = host.formatter if host.respond_to?(:formatter)
|
40
|
+
end
|
41
|
+
|
42
|
+
logger.level = opt_log_level
|
43
|
+
|
44
|
+
logger
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.checkParams(token, region, opt_datahub_enabled, opt_udp_port)
|
48
|
+
# Check if the key is valid UUID format
|
49
|
+
|
50
|
+
if (!opt_datahub_enabled && !opt_udp_port) # test Token only when DataHub and UDP are not enabled
|
51
|
+
if (token =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) == nil
|
52
|
+
puts "\nLE: It appears the LOGENTRIES_TOKEN you entered is invalid!\n"
|
53
|
+
else
|
54
|
+
(token="")
|
55
|
+
end
|
56
|
+
|
57
|
+
# This checks if region is valid. So far we have Europe and US
|
58
|
+
if !region
|
59
|
+
puts "\nLE: You need to specify region. Options: eu, us"
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
*.log
|
data/test/host_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Le::Host do
|
5
|
+
|
6
|
+
let(:token) { '11111111-2222-3333-aaaa-bbbbbbbbbbbb' }
|
7
|
+
let(:local) { false }
|
8
|
+
let(:debug) { false }
|
9
|
+
let(:ssl) { true }
|
10
|
+
let(:udp) { nil }
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
let(:datahub_endpoint) { ["", 10000] }
|
15
|
+
let(:host_id) { ""}
|
16
|
+
let(:custom_host) { [false, ""]}
|
17
|
+
let(:data_endpoint) {true}
|
18
|
+
|
19
|
+
#let(:host) { Le::Host.new(token, local, debug, ssl) }
|
20
|
+
let(:host) { Le::Host::HTTP.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp, data_endpoint) }
|
21
|
+
specify { host.must_be_instance_of Le::Host::HTTP }
|
22
|
+
|
23
|
+
end
|
data/test/http_spec.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
describe Le::Host::HTTP do
|
5
|
+
|
6
|
+
let(:token) { '11111111-2222-3333-aaaa-bbbbbbbbbbbb' }
|
7
|
+
let(:local) { false }
|
8
|
+
let(:debug) { false }
|
9
|
+
let(:ssl) { false }
|
10
|
+
let(:udp) { nil }
|
11
|
+
|
12
|
+
let(:datahub_endpoint) { ["", 10000]}
|
13
|
+
let(:host_id) {""}
|
14
|
+
let(:custom_host) {[false, ""]}
|
15
|
+
let(:endpoint) {false}
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
# let(:host) { Le::Host::HTTP.new(token, local, debug, ssl) }
|
20
|
+
let(:host) { Le::Host::HTTP.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp, endpoint) }
|
21
|
+
|
22
|
+
let(:logger_console) { host.instance_variable_get(:@logger_console) }
|
23
|
+
let(:logger_console_dev) { logger_console.instance_variable_get(:@logdev).dev }
|
24
|
+
|
25
|
+
specify { host.must_be_instance_of Le::Host::HTTP }
|
26
|
+
specify { host.local.must_equal false }
|
27
|
+
specify { host.debug.must_equal false }
|
28
|
+
specify { host.ssl.must_equal false }
|
29
|
+
specify { host.udp_port.must_equal nil }
|
30
|
+
specify {host_id.must_equal ""}
|
31
|
+
specify {custom_host.must_equal [false, ""]}
|
32
|
+
|
33
|
+
end
|
data/test/le_spec.rb
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Le do
|
4
|
+
|
5
|
+
let(:token) { '11111111-2222-3333-aaaa-bbbbbbbbbbbb' }
|
6
|
+
let(:local) { false }
|
7
|
+
let(:logger) { Le.new(token, local: local) }
|
8
|
+
let(:logdev) { logger.instance_variable_get(:@logdev).dev }
|
9
|
+
let(:logger_console) { logdev.instance_variable_get(:@logger_console) }
|
10
|
+
let(:logger_console_dev) { logger_console.instance_variable_get(:@logdev).dev }
|
11
|
+
|
12
|
+
|
13
|
+
describe "when non-Rails environment" do
|
14
|
+
|
15
|
+
describe "when initialised with just a token" do
|
16
|
+
let(:logger) { Le.new(token) }
|
17
|
+
specify { logger.must_be_instance_of Logger }
|
18
|
+
specify { logdev.must_be_instance_of Le::Host::HTTP }
|
19
|
+
specify { logdev.local.must_equal false }
|
20
|
+
specify { logger_console.must_be_nil }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "and :local is false" do
|
24
|
+
specify { logdev.local.must_equal false }
|
25
|
+
specify { logger_console.must_be_nil }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "and :local is true" do
|
29
|
+
let(:local) { true }
|
30
|
+
|
31
|
+
specify { logdev.local.must_equal true }
|
32
|
+
specify { logger_console.must_be_instance_of Logger }
|
33
|
+
specify { logger_console_dev.must_be_instance_of IO }
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
describe "and :local => " do
|
38
|
+
let(:local_test_log) { Pathname.new(File.dirname(__FILE__)).join('fixtures','log','local_log.log') }
|
39
|
+
let(:local) { log_file }
|
40
|
+
|
41
|
+
describe "Pathname" do
|
42
|
+
let(:log_file) { local_test_log }
|
43
|
+
|
44
|
+
specify { logdev.must_be_instance_of Le::Host::HTTP }
|
45
|
+
specify { logdev.local.must_equal true }
|
46
|
+
specify { logger_console.must_be_instance_of Logger }
|
47
|
+
specify { logger_console_dev.must_be_instance_of File }
|
48
|
+
specify { logger_console_dev.path.must_match 'local_log.log' }
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "path string" do
|
52
|
+
let(:log_file) { local_test_log.to_s }
|
53
|
+
|
54
|
+
specify { logdev.must_be_instance_of Le::Host::HTTP }
|
55
|
+
specify { logdev.local.must_equal true }
|
56
|
+
specify { logger_console.must_be_instance_of Logger }
|
57
|
+
specify { logger_console_dev.must_be_instance_of File }
|
58
|
+
specify { logger_console_dev.path.must_match 'local_log.log' }
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "File" do
|
62
|
+
let(:log_file) { File.new(local_test_log, 'w') }
|
63
|
+
|
64
|
+
specify { logdev.must_be_instance_of Le::Host::HTTP }
|
65
|
+
specify { logdev.local.must_equal true }
|
66
|
+
specify { logger_console.must_be_instance_of Logger }
|
67
|
+
specify { logger_console_dev.must_be_instance_of File }
|
68
|
+
specify { logger_console_dev.path.must_match 'local_log.log' }
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "when Rails environment" do
|
76
|
+
before do
|
77
|
+
class Rails
|
78
|
+
def self.root
|
79
|
+
Pathname.new(File.dirname(__FILE__)).join('fixtures')
|
80
|
+
end
|
81
|
+
def self.env
|
82
|
+
'test'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
after do
|
87
|
+
Object.send(:remove_const, :Rails)
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "and :local is false" do
|
91
|
+
specify { logdev.local.must_equal false }
|
92
|
+
specify { logger_console.must_be_nil }
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "and :local is true" do
|
96
|
+
let(:local) { true }
|
97
|
+
|
98
|
+
specify { logdev.local.must_equal true }
|
99
|
+
specify { logger_console.must_be_instance_of Logger }
|
100
|
+
specify { logger_console_dev.must_be_instance_of File }
|
101
|
+
specify { logger_console_dev.path.must_match 'test.log' }
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "and :local => " do
|
105
|
+
let(:local_test_log) { Pathname.new(File.dirname(__FILE__)).join('fixtures','log','local_log.log') }
|
106
|
+
let(:local) { log_file }
|
107
|
+
|
108
|
+
describe "Pathname" do
|
109
|
+
let(:log_file) { local_test_log }
|
110
|
+
|
111
|
+
specify { logdev.must_be_instance_of Le::Host::HTTP }
|
112
|
+
specify { logdev.local.must_equal true }
|
113
|
+
specify { logger_console.must_be_instance_of Logger }
|
114
|
+
specify { logger_console_dev.must_be_instance_of File }
|
115
|
+
specify { logger_console_dev.path.must_match 'local_log.log' }
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "path string" do
|
119
|
+
let(:log_file) { local_test_log.to_s }
|
120
|
+
|
121
|
+
specify { logdev.must_be_instance_of Le::Host::HTTP }
|
122
|
+
specify { logdev.local.must_equal true }
|
123
|
+
specify { logger_console.must_be_instance_of Logger }
|
124
|
+
specify { logger_console_dev.must_be_instance_of File }
|
125
|
+
specify { logger_console_dev.path.must_match 'local_log.log' }
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "File" do
|
129
|
+
let(:log_file) { File.new(local_test_log, 'w') }
|
130
|
+
|
131
|
+
specify { logdev.must_be_instance_of Le::Host::HTTP }
|
132
|
+
specify { logdev.local.must_equal true }
|
133
|
+
specify { logger_console.must_be_instance_of Logger }
|
134
|
+
specify { logger_console_dev.must_be_instance_of File }
|
135
|
+
specify { logger_console_dev.path.must_match 'local_log.log' }
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
data/test/region.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
logger = Le.new('185a71d5-32a7-4007-a12d-3295c75a10e2', 'eu')
|
5
|
+
logger.info("ssl")
|
6
|
+
|
7
|
+
describe Le::Host::HTTP do
|
8
|
+
|
9
|
+
let(:token) { '11111111-2222-3333-aaaa-bbbbbbbbbbbb' }
|
10
|
+
let(:region) {'eu'}
|
11
|
+
let(:local) { false }
|
12
|
+
let(:debug) { false }
|
13
|
+
let(:ssl) { false }
|
14
|
+
let(:udp) { nil }
|
15
|
+
|
16
|
+
let(:datahub_endpoint) { ["", 10000]}
|
17
|
+
let(:host_id) {""}
|
18
|
+
let(:custom_host) {[false, ""]}
|
19
|
+
let(:endpoint) {false}
|
20
|
+
|
21
|
+
|
22
|
+
let(:host) { Le::Host::HTTP.new(token, region, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp, endpoint) }
|
23
|
+
|
24
|
+
let(:logger_console) { host.instance_variable_get(:@logger_console) }
|
25
|
+
let(:logger_console_dev) { logger_console.instance_variable_get(:@logdev).dev }
|
26
|
+
|
27
|
+
specify { host.must_be_instance_of Le::Host::HTTP }
|
28
|
+
specify {host.region.must_equal 'eu'}
|
29
|
+
specify { host.local.must_equal false }
|
30
|
+
specify { host.debug.must_equal false }
|
31
|
+
specify { host.ssl.must_equal false }
|
32
|
+
specify { host.udp_port.must_equal nil }
|
33
|
+
specify {host_id.must_equal ""}
|
34
|
+
specify {custom_host.must_equal [false, ""]}
|
35
|
+
|
36
|
+
|
37
|
+
end
|
data/test/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: r7insight
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.7.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Lacomber
|
8
|
+
- Stephen Hynes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-06-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: "\n\n"
|
57
|
+
email: support@rapid7.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LE.gemspec
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/le.rb
|
69
|
+
- lib/le/host.rb
|
70
|
+
- lib/le/host/connection.rb
|
71
|
+
- test/fixtures/log/.gitignore
|
72
|
+
- test/host_spec.rb
|
73
|
+
- test/http_spec.rb
|
74
|
+
- test/le_spec.rb
|
75
|
+
- test/region.rb
|
76
|
+
- test/spec_helper.rb
|
77
|
+
homepage: https://github.com/rapid7/r7insight_ruby
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.6.12
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: InsightOps logging plugin
|
101
|
+
test_files:
|
102
|
+
- test/fixtures/log/.gitignore
|
103
|
+
- test/host_spec.rb
|
104
|
+
- test/http_spec.rb
|
105
|
+
- test/le_spec.rb
|
106
|
+
- test/region.rb
|
107
|
+
- test/spec_helper.rb
|