summaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/ping.rb
blob: 4283d9b3773a85513f2f5bfae341f93d725fdce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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