pingly 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pingly.rb +30 -9
- metadata +1 -1
data/lib/pingly.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
1
3
|
class Pingly
|
2
|
-
VERSION = '0.
|
4
|
+
VERSION = '0.4.0'
|
3
5
|
|
4
|
-
attr_accessor :host, :timeout
|
6
|
+
attr_accessor :host, :timeout
|
5
7
|
|
6
8
|
def self.ping_loop(host, timeout = 5)
|
7
9
|
while true do
|
8
10
|
p = new(host)
|
9
11
|
p.ping!
|
10
12
|
|
11
|
-
if p.
|
12
|
-
yield if block_given?
|
13
|
-
end
|
13
|
+
yield if block_given? && !p.successful?
|
14
14
|
|
15
15
|
puts p.response
|
16
16
|
end
|
@@ -25,7 +25,7 @@ class Pingly
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def ping!
|
28
|
-
|
28
|
+
perform_ping
|
29
29
|
end
|
30
30
|
|
31
31
|
def packet_loss
|
@@ -45,14 +45,35 @@ class Pingly
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def response
|
48
|
-
"#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} -
|
48
|
+
"#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - " + (successful? ? successful_response : failed_response)
|
49
|
+
end
|
50
|
+
|
51
|
+
def successful?
|
52
|
+
ping! unless ping_performed
|
53
|
+
|
54
|
+
response_status.success? && packet_loss < 25
|
49
55
|
end
|
50
56
|
|
51
57
|
private
|
52
58
|
|
59
|
+
attr_accessor :ping_performed, :response_stdout, :response_stderr, :response_status
|
60
|
+
|
61
|
+
def successful_response
|
62
|
+
"#{host}(#{ip_address}) - Sent: #{packets_sent} Received: #{packets_received} Loss: #{packet_loss}%"
|
63
|
+
end
|
64
|
+
|
65
|
+
def failed_response
|
66
|
+
response_stderr.to_s
|
67
|
+
end
|
68
|
+
|
69
|
+
def perform_ping
|
70
|
+
self.response_stdout, self.response_stderr, self.response_status = Open3.capture3(build_ping_string)
|
71
|
+
self.ping_performed = true
|
72
|
+
end
|
73
|
+
|
53
74
|
def response_regex(regex)
|
54
|
-
ping! unless
|
55
|
-
|
75
|
+
ping! unless ping_performed
|
76
|
+
response_stdout =~ regex
|
56
77
|
$1
|
57
78
|
end
|
58
79
|
|