sensu-transport 6.0.1 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ba806650db7e6211a83b2040a4260ac820c8aea
4
- data.tar.gz: e5bce79221bc9c26b3e1ec71b62a79f735c1c22b
3
+ metadata.gz: c4f60d201d69a505cbadc8730f1303997608a15a
4
+ data.tar.gz: 74ef946054dea6c1c7a8df3df623efb08100a6d3
5
5
  SHA512:
6
- metadata.gz: 39af5f193e627206dc98e7bba305ca8251c9773933bd55dee2625709593e9601c388a83fc52f067dfb326381810c82d9649231cfff08a307177d90e53f071747
7
- data.tar.gz: 2bd0a84d1ede40661f886570fa7a27c76bcb45553dba4d1412c79df1ab547dde7540d0fb5c7e4655931cabfda211ed550dc58dba9ac828ef1237b5b62b64f8ae
6
+ metadata.gz: b3726aa45930ae5562e476bc376ea003ee8bf5ec31e87a8da8d331907175a20875637b1fb748b9b77623c7dff6f6aa07582de23bee941aed580a9e57f1f7348a
7
+ data.tar.gz: 5e748dd5d6ce62e622a3f255977407ba2f0c31b5126d921ea173a87c00933f7ca77b87cac53a4b619ba47341e054dd07a798ac5aee0f5a2235bd6935241499a4
@@ -1,4 +1,6 @@
1
1
  require "eventmachine"
2
+ require "ipaddr"
3
+ require "socket"
2
4
 
3
5
  module Sensu
4
6
  module Transport
@@ -134,6 +136,65 @@ module Sensu
134
136
  yield(info) if block_given?
135
137
  end
136
138
 
139
+ # Determine if a host is an IP address (or DNS hostname).
140
+ #
141
+ # @param host [String]
142
+ # @return [TrueClass, FalseClass]
143
+ def ip_address?(host)
144
+ begin
145
+ ip_address = IPAddr.new(host)
146
+ ip_address.ipv4? || ip_address.ipv6?
147
+ rescue IPAddr::InvalidAddressError
148
+ false
149
+ end
150
+ end
151
+
152
+ # Resolve a hostname to an IP address for a host. This method
153
+ # will return `nil` to the provided callback when the hostname
154
+ # cannot be resolved to an IP address.
155
+ #
156
+ # @param host [String]
157
+ # @param callback [Proc] called with the result of the DNS
158
+ # query (IP address).
159
+ def resolve_hostname(host, &callback)
160
+ resolve = Proc.new do
161
+ begin
162
+ info = case RUBY_PLATFORM
163
+ when /linux/
164
+ flags = Socket::AI_NUMERICSERV | Socket::AI_ADDRCONFIG
165
+ Socket.getaddrinfo(host, nil, Socket::AF_UNSPEC, nil, nil, flags)
166
+ else
167
+ Socket.getaddrinfo(host, nil)
168
+ end
169
+ info.first.nil? ? nil : info.first[2]
170
+ rescue => error
171
+ @logger.error("transport connection error", {
172
+ :reason => "unable to resolve hostname",
173
+ :error => error.to_s
174
+ }) if @logger
175
+ nil
176
+ end
177
+ end
178
+ EM.defer(resolve, callback)
179
+ end
180
+
181
+ # Resolve a hostname to an IP address for a host. This method
182
+ # will return the provided host to the provided callback if it
183
+ # is already an IP address. This method will return `nil` to the
184
+ # provided callback when the hostname cannot be resolved to an
185
+ # IP address.
186
+ #
187
+ # @param host [String]
188
+ # @param callback [Proc] called with the result of the DNS
189
+ # query (IP address).
190
+ def resolve_host(host, &callback)
191
+ if ip_address?(host)
192
+ yield host
193
+ else
194
+ resolve_hostname(host, &callback)
195
+ end
196
+ end
197
+
137
198
  # Discover available transports (Subclasses)
138
199
  def self.descendants
139
200
  ObjectSpace.each_object(Class).select do |klass|
@@ -26,6 +26,7 @@ module Sensu
26
26
  def reconnect(force=false)
27
27
  unless @reconnecting
28
28
  @reconnecting = true
29
+ @logger.debug("transport reconnecting...")
29
30
  @before_reconnect.call
30
31
  reset
31
32
  periodically_reconnect
@@ -57,11 +58,16 @@ module Sensu
57
58
  # callback/block.
58
59
  # @yieldparam info [Hash] contains publish information.
59
60
  def publish(type, pipe, message, options={})
60
- catch_errors do
61
- @channel.method(type.to_sym).call(pipe, options).publish(message) do
62
- info = {}
63
- yield(info) if block_given?
61
+ if connected?
62
+ catch_errors do
63
+ @channel.method(type.to_sym).call(pipe, options).publish(message) do
64
+ info = {}
65
+ yield(info) if block_given?
66
+ end
64
67
  end
68
+ else
69
+ info = {:error => "Transport is not connected"}
70
+ yield(info) if block_given?
65
71
  end
66
72
  end
67
73
 
@@ -176,27 +182,57 @@ module Sensu
176
182
  end
177
183
  end
178
184
 
179
- def next_connection_options
185
+ def next_connection_options(&callback)
180
186
  if @eligible_options.nil? || @eligible_options.empty?
181
187
  @eligible_options = @connection_options.shuffle
182
188
  end
183
- @eligible_options.shift
189
+ options = @eligible_options.shift
190
+ if options.is_a?(Hash) && options[:host]
191
+ resolve_host(options[:host]) do |ip_address|
192
+ if ip_address.nil?
193
+ EM::Timer.new(3) do
194
+ next_connection_options(&callback)
195
+ end
196
+ else
197
+ yield options.merge(:host => ip_address)
198
+ end
199
+ end
200
+ else
201
+ yield options
202
+ end
184
203
  end
185
204
 
186
205
  def setup_connection(options={})
187
206
  reconnect_callback = Proc.new { reconnect }
207
+ on_possible_auth_failure = Proc.new {
208
+ @logger.warn("transport connection error", {
209
+ :reason => "possible authentication failure. wrong credentials?",
210
+ :user => options[:user]
211
+ })
212
+ reconnect
213
+ }
188
214
  @connection = AMQP.connect(options, {
189
215
  :on_tcp_connection_failure => reconnect_callback,
190
- :on_possible_authentication_failure => reconnect_callback
216
+ :on_possible_authentication_failure => on_possible_auth_failure
191
217
  })
192
218
  @connection.logger = @logger
193
219
  @connection.on_open do
220
+ @logger.debug("transport connection open")
194
221
  @connection_timeout.cancel
195
222
  succeed
196
223
  yield if block_given?
197
224
  end
198
- @connection.on_tcp_connection_loss(&reconnect_callback)
199
- @connection.on_skipped_heartbeats(&reconnect_callback)
225
+ @connection.on_tcp_connection_loss do
226
+ @logger.warn("transport connection error", :reason => "tcp connection lost")
227
+ reconnect
228
+ end
229
+ @connection.on_skipped_heartbeats do
230
+ @logger.warn("transport connection error", :reason => "skipped heartbeats")
231
+ reconnect
232
+ end
233
+ @connection.on_closed do
234
+ @logger.debug("transport connection closed")
235
+ end
200
236
  end
201
237
 
202
238
  def setup_channel(options={})
@@ -214,9 +250,10 @@ module Sensu
214
250
  end
215
251
 
216
252
  def connect_with_eligible_options(&callback)
217
- options = next_connection_options
218
- setup_connection(options, &callback)
219
- setup_channel(options)
253
+ next_connection_options do |options|
254
+ setup_connection(options, &callback)
255
+ setup_channel(options)
256
+ end
220
257
  end
221
258
 
222
259
  def periodically_reconnect(delay=2)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "sensu-transport"
5
- spec.version = "6.0.1"
5
+ spec.version = "7.0.0"
6
6
  spec.authors = ["Sean Porter"]
7
7
  spec.email = ["portertech@gmail.com"]
8
8
  spec.summary = "The Sensu transport abstraction library"
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec"
26
26
  spec.add_development_dependency "codeclimate-test-reporter" unless RUBY_VERSION < "1.9"
27
+ spec.add_development_dependency "sensu-logger"
27
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.1
4
+ version: 7.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Porter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-12 00:00:00.000000000 Z
11
+ date: 2016-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sensu-logger
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  description: The Sensu transport abstraction library
126
140
  email:
127
141
  - portertech@gmail.com