summaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/ping.rb
diff options
context:
space:
mode:
authorMicah Anderson <micah@riseup.net>2017-11-28 11:35:01 -0500
committerMicah Anderson <micah@riseup.net>2017-11-28 11:35:01 -0500
commit0d251e2ceddd3e02ed8bba8725830689dbdd1397 (patch)
tree37d7096d9e458ca1e6431dff8a2f571553011c44 /lib/leap_cli/commands/ping.rb
parent93a181d44e2d8163ae44945aac1b6477e268170d (diff)
parentbf6c56d86c7ba45e7ca766d990a9e9162025e5ac (diff)
Merge tag 'refs/tags/0.10.0' into stable
Release 0.10.0
Diffstat (limited to 'lib/leap_cli/commands/ping.rb')
-rw-r--r--lib/leap_cli/commands/ping.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/leap_cli/commands/ping.rb b/lib/leap_cli/commands/ping.rb
new file mode 100644
index 00000000..4283d9b3
--- /dev/null
+++ b/lib/leap_cli/commands/ping.rb
@@ -0,0 +1,58 @@
+module LeapCli; module Commands
+
+ desc "Ping nodes to see if they are alive."
+ long_desc "Attempts to ping each node in the FILTER set."
+ arg_name "FILTER"
+ command :ping do |c|
+ c.flag 'timeout', :arg_name => "TIMEOUT",
+ :default_value => 2, :desc => 'Wait at most TIMEOUT seconds.'
+ c.flag 'count', :arg_name => "COUNT",
+ :default_value => 2, :desc => 'Ping COUNT times.'
+ c.action do |global, options, args|
+ do_ping(global, options, args)
+ end
+ end
+
+ private
+
+ def do_ping(global, options, args)
+ assert_bin!('ping')
+
+ timeout = [options[:timeout].to_i, 1].max
+ count = [options[:count].to_i, 1].max
+ nodes = nil
+
+ if args && args.any?
+ node = manager.disabled_node(args.first)
+ if node
+ nodes = Config::ObjectList.new
+ nodes.add(node.name, node)
+ end
+ end
+
+ nodes ||= manager.filter! args
+
+ threads = []
+ nodes.each_node do |node|
+ threads << Thread.new do
+ cmd = "ping -i 0.2 -n -q -W #{timeout} -c #{count} #{node.ip_address} 2>&1"
+ log(2, cmd)
+ output = `#{cmd}`
+ if $?.success?
+ last = output.split("\n").last
+ times = last.split('=').last.strip
+ min, avg, max, mdev = times.split('/')
+ log("ping #{min} ms", host: node.name, color: :green)
+ else
+ log(:failed, "to ping #{node.ip_address}", host: node.name)
+ end
+ end
+ end
+ threads.map(&:join)
+
+ log("done")
+ end
+
+end; end
+
+