Class: MCollective::Application::Ping

Inherits:
MCollective::Application show all
Defined in:
lib/mcollective/application/ping.rb

Instance Method Summary collapse

Methods inherited from MCollective::Application

[], []=, #application_cli_arguments, #application_description, #application_failure, #application_options, application_options, #application_parse_options, #application_usage, #clioptions, #configuration, description, #disconnect, exclude_argument_sections, #halt, #halt_code, #help, intialize_application_options, option, #options, #rpcclient, #run, usage, #validate_cli_options, #validate_option

Methods included from RPC

const_missing, discovered, #empty_filter?, #printrpc, #printrpcstats, #rpcclient, #rpcoptions, stats

Instance Method Details

#mainObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mcollective/application/ping.rb', line 51

def main
  # If the user did not override the default timeout include the discovery timeout
  if options[:timeout] == 5
    discovery_timeout = options[:disctimeout] || Config.instance.discovery_timeout || 0
    options[:timeout] = options[:timeout] + discovery_timeout
  end
  client = MCollective::Client.new(options)

  start = Time.now.to_f
  times = []

  client.req("ping", "discovery") do |resp|
    times << (Time.now.to_f - start) * 1000

    puts "%-40s time=%.2f ms" % [resp[:senderid], times.last]
  end

  puts("\n\n---- ping statistics ----")

  if times.size > 0
    sum = times.inject(0){|acc,i|acc +i}
    avg = sum / times.length.to_f

    puts "%d replies max: %.2f min: %.2f avg: %.2f %s" % [times.size, times.max, times.min, avg, spark(times)]
  else
    puts("No responses received")
  end

  halt client.stats
end

#spark(resp_times) ⇒ Object

Convert the times structure into a array representing buckets of responses in 50 ms intervals. Return a small sparkline graph using UTF8 characters



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
# File 'lib/mcollective/application/ping.rb', line 15

def spark(resp_times)
  return "" unless configuration[:graph] || Config.instance.pluginconf["rpc.graph"]

  ticks=%w[      ]

  histo = {}

  # round each time to its nearest 50ms
  # and keep a count for each 50ms
  resp_times.each do |time|
    time = Integer(time + 50 - (time % 50))
    histo[time] ||= 0
    histo[time] += 1
  end

  # set the 50ms intervals that saw no traffic to 0
  ((histo.keys.max - histo.keys.min) / 50).times do |i|
    time = (i * 50) + histo.keys.min
    histo[time] = 0 unless histo[time]
  end

  # get a numerically sorted list of times
  histo = histo.keys.sort.map{|k| histo[k]}

  range = histo.max - histo.min
  scale = ticks.size - 1
  distance = histo.max.to_f / scale

  histo.map do |val|
    tick = (val / distance).round
    tick = 0 if tick < 0

    ticks[tick]
  end.join
end