elbping 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/elbping/cli.rb CHANGED
@@ -52,6 +52,22 @@ module ElbPing
52
52
  # Main entry point of the program
53
53
  def self.main
54
54
  PARSER.parse!(ARGV) rescue usage
55
+ run = true
56
+
57
+ # Set up summary objects
58
+ total_summary = {
59
+ :reqs_attempted => 0,
60
+ :reqs_completed => 0,
61
+ :latencies => [],
62
+ }
63
+ node_summary = {}
64
+
65
+ # Catch ctrl-c
66
+ trap("SIGINT") {
67
+ #ElbPing::Display.summary(total_summary, node_summary)
68
+ #exit!
69
+ run = false
70
+ }
55
71
 
56
72
  if ARGV.size < 1
57
73
  usage
@@ -70,23 +86,10 @@ module ElbPing
70
86
  exit(false)
71
87
  end
72
88
 
73
- # Set up summary objects
74
- total_summary = {
75
- :reqs_attempted => 0,
76
- :reqs_completed => 0,
77
- :latencies => [],
78
- }
79
- node_summary = {}
80
89
  nodes.each { |node| node_summary[node] = total_summary.clone }
81
90
 
82
- # Catch ctrl-c
83
- trap("INT") {
84
- ElbPing::Display.summary(total_summary, node_summary)
85
- exit
86
- }
87
-
88
91
  iteration = 0
89
- while OPTIONS[:count] < 1 || iteration < OPTIONS[:count]
92
+ while (OPTIONS[:count] < 1 || iteration < OPTIONS[:count]) && run
90
93
  sleep OPTIONS[:wait] if iteration > 0
91
94
 
92
95
  nodes.map { |node|
@@ -6,8 +6,10 @@ module ElbPing
6
6
  node = status[:node]
7
7
  code = status[:code]
8
8
  duration = status[:duration]
9
+ exc = status[:exception]
10
+ exc_display = exc ? 'exception=#{exception}' : ''
9
11
 
10
- puts "Response from #{node}: code=#{code.to_s} time=#{duration} ms"
12
+ puts "Response from #{node}: code=#{code.to_s} time=#{duration} ms #{exc_display}"
11
13
  end
12
14
 
13
15
  # Display summary of results (in aggregate and per-node)
@@ -17,7 +19,11 @@ module ElbPing
17
19
  loss = (1 - (responses.to_f/requests)) * 100
18
20
 
19
21
  latencies = total_summary[:latencies]
20
- avg_latency = (latencies.inject { |sum, el| sum + el }.to_f / latencies.size).to_i # ms
22
+ avg_latency = 0
23
+ unless latencies.size == 0
24
+ sum_latency = latencies.inject { |sum, el| sum + el} || 0
25
+ avg_latency = (sum_latency.to_f / latencies.size).to_i # ms
26
+ end
21
27
 
22
28
  node_summary.each { |node, summary|
23
29
  requests = summary[:reqs_attempted]
@@ -25,7 +31,11 @@ module ElbPing
25
31
  loss = (1 - (responses.to_f/requests)) * 100
26
32
 
27
33
  latencies = summary[:latencies]
28
- avg_latency = (latencies.inject { |sum, el| sum + el }.to_f / latencies.size).to_i # ms
34
+ avg_latency = 0
35
+ unless latencies.size == 0
36
+ sum_latency = latencies.inject { |sum, el| sum + el} || 0
37
+ avg_latency = (sum_latency.to_f / latencies.size).to_i # ms
38
+ end
29
39
 
30
40
  puts "--- #{node} statistics ---"
31
41
  puts "#{requests} requests, #{responses} responses, #{loss.to_i}% loss"
@@ -20,9 +20,20 @@ module ElbPing
20
20
  http.ssl_timeout = timeout # untested
21
21
 
22
22
  error = nil
23
- response = http.request(ping_request.new(path)) rescue error = :timeout
23
+ exc = nil
24
+ begin
25
+ response = http.request(ping_request.new(path))
26
+ rescue Errno::ECONNREFUSED
27
+ error = :econnrefused
28
+ rescue Timeout::Error
29
+ error = :timeout
30
+ rescue StandardError => e
31
+ exc = e # because I don't understand scope in ruby yet
32
+ error = :exception
33
+ end
24
34
 
25
35
  {:code => error || response.code,
36
+ :exception => exc,
26
37
  :node => node,
27
38
  :duration => ((Time.now.getutc - start) * 1000).to_i} # returns in ms
28
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elbping
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: