Update mcollective.init script according to OSCI-969
[packages/precise/mcollective.git] / mcollective-2.3.3 / plugins / mcollective / application / ping.rb
1 # encoding: utf-8
2 module MCollective
3   class Application::Ping<Application
4     description "Ping all nodes"
5
6     option :graph,
7            :description    => "Shows a graph of ping distribution",
8            :arguments      => ["--graph", "-g"],
9            :default        => false,
10            :type           => :bool
11
12     # Convert the times structure into a array representing
13     # buckets of responses in 50 ms intervals.  Return a small
14     # sparkline graph using UTF8 characters
15     def spark(resp_times)
16       return "" unless configuration[:graph] || Config.instance.pluginconf["rpc.graph"]
17
18       ticks=%w[▁ ▂ ▃ ▄ ▅ ▆ ▇]
19
20       histo = {}
21
22       # round each time to its nearest 50ms
23       # and keep a count for each 50ms
24       resp_times.each do |time|
25         time = Integer(time + 50 - (time % 50))
26         histo[time] ||= 0
27         histo[time] += 1
28       end
29
30       # set the 50ms intervals that saw no traffic to 0
31       ((histo.keys.max - histo.keys.min) / 50).times do |i|
32         time = (i * 50) + histo.keys.min
33         histo[time] = 0 unless histo[time]
34       end
35
36       # get a numerically sorted list of times
37       histo = histo.keys.sort.map{|k| histo[k]}
38
39       range = histo.max - histo.min
40       scale = ticks.size - 1
41       distance = histo.max.to_f / scale
42
43       histo.map do |val|
44         tick = (val / distance).round
45         tick = 0 if tick < 0
46
47         ticks[tick]
48       end.join
49     end
50
51     def main
52       client = MCollective::Client.new(options[:config])
53       client.options = options
54
55       start = Time.now.to_f
56       times = []
57
58       client.req("ping", "discovery") do |resp|
59         times << (Time.now.to_f - start) * 1000
60
61         puts "%-40s time=%.2f ms" % [resp[:senderid], times.last]
62       end
63
64       puts("\n\n---- ping statistics ----")
65
66       if times.size > 0
67         sum = times.inject(0){|acc,i|acc +i}
68         avg = sum / times.length.to_f
69
70         puts "%d replies max: %.2f min: %.2f avg: %.2f %s" % [times.size, times.max, times.min, avg, spark(times)]
71       else
72         puts("No responses received")
73       end
74
75       halt client.stats
76     end
77   end
78 end