Class: MCollective::RPC::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/rpc/stats.rb

Overview

Class to wrap all the stats and to keep track of some timings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



9
10
11
# File 'lib/mcollective/rpc/stats.rb', line 9

def initialize
  reset
end

Instance Attribute Details

#aggregate_failuresObject

Returns the value of attribute aggregate_failures.



7
8
9
# File 'lib/mcollective/rpc/stats.rb', line 7

def aggregate_failures
  @aggregate_failures
end

#aggregate_summaryObject

Returns the value of attribute aggregate_summary.



7
8
9
# File 'lib/mcollective/rpc/stats.rb', line 7

def aggregate_summary
  @aggregate_summary
end

#blocktimeObject

Returns the value of attribute blocktime.



5
6
7
# File 'lib/mcollective/rpc/stats.rb', line 5

def blocktime
  @blocktime
end

#ddlObject

Returns the value of attribute ddl.



7
8
9
# File 'lib/mcollective/rpc/stats.rb', line 7

def ddl
  @ddl
end

#discoveredObject

Returns the value of attribute discovered.



6
7
8
# File 'lib/mcollective/rpc/stats.rb', line 6

def discovered
  @discovered
end

#discovered_nodesObject

Returns the value of attribute discovered_nodes.



6
7
8
# File 'lib/mcollective/rpc/stats.rb', line 6

def discovered_nodes
  @discovered_nodes
end

#discoverytimeObject

Returns the value of attribute discoverytime.



5
6
7
# File 'lib/mcollective/rpc/stats.rb', line 5

def discoverytime
  @discoverytime
end

#failcountObject

Returns the value of attribute failcount.



6
7
8
# File 'lib/mcollective/rpc/stats.rb', line 6

def failcount
  @failcount
end

#noresponsefromObject

Returns the value of attribute noresponsefrom.



5
6
7
# File 'lib/mcollective/rpc/stats.rb', line 5

def noresponsefrom
  @noresponsefrom
end

#okcountObject

Returns the value of attribute okcount.



6
7
8
# File 'lib/mcollective/rpc/stats.rb', line 6

def okcount
  @okcount
end

#requestidObject

Returns the value of attribute requestid.



7
8
9
# File 'lib/mcollective/rpc/stats.rb', line 7

def requestid
  @requestid
end

#responsesObject

Returns the value of attribute responses.



5
6
7
# File 'lib/mcollective/rpc/stats.rb', line 5

def responses
  @responses
end

#responsesfromObject

Returns the value of attribute responsesfrom.



7
8
9
# File 'lib/mcollective/rpc/stats.rb', line 7

def responsesfrom
  @responsesfrom
end

#starttimeObject

Returns the value of attribute starttime.



5
6
7
# File 'lib/mcollective/rpc/stats.rb', line 5

def starttime
  @starttime
end

#totaltimeObject

Returns the value of attribute totaltime.



6
7
8
# File 'lib/mcollective/rpc/stats.rb', line 6

def totaltime
  @totaltime
end

#unexpectedresponsefromObject

Returns the value of attribute unexpectedresponsefrom.



5
6
7
# File 'lib/mcollective/rpc/stats.rb', line 5

def unexpectedresponsefrom
  @unexpectedresponsefrom
end

Instance Method Details

#[](key) ⇒ Object

Fake hash access to keep things backward compatible



51
52
53
54
55
# File 'lib/mcollective/rpc/stats.rb', line 51

def [](key)
  to_hash[key]
rescue
  nil
end

#client_stats=(stats) ⇒ Object

Re-initializes the object with stats from the basic client



72
73
74
75
76
77
78
79
80
81
# File 'lib/mcollective/rpc/stats.rb', line 72

def client_stats=(stats)
  @noresponsefrom = stats[:noresponsefrom]
  @unexpectedresponsefrom = stats[:unexpectedresponsefrom]
  @responses = stats[:responses]
  @starttime = stats[:starttime]
  @blocktime = stats[:blocktime]
  @totaltime = stats[:totaltime]
  @requestid = stats[:requestid]
  @discoverytime = stats[:discoverytime] if @discoverytime == 0
end

#discovered_agents(agents) ⇒ Object

Update discovered and discovered_nodes based on discovery results



111
112
113
114
# File 'lib/mcollective/rpc/stats.rb', line 111

def discovered_agents(agents)
  @discovered_nodes = agents
  @discovered = agents.size
end

#failObject

increment the count of failed hosts



65
66
67
68
69
# File 'lib/mcollective/rpc/stats.rb', line 65

def fail
  @failcount += 1
rescue
  @failcount = 1
end

#finish_requestObject

Helper to calculate total time etc



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mcollective/rpc/stats.rb', line 117

def finish_request
  @totaltime = @blocktime + @discoverytime

  # figure out who responded unexpectedly
  @unexpectedresponsefrom = @responsesfrom - @discovered_nodes

  # figure out who we had no responses from
  @noresponsefrom = @discovered_nodes - @responsesfrom
rescue
  @totaltime = 0
  @noresponsefrom = []
  @unexpectedresponsefrom = []
end

#no_response_reportObject

Returns a blob of text indicating what nodes did not respond



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/mcollective/rpc/stats.rb', line 254

def no_response_report
  result_text = StringIO.new

  if @noresponsefrom.size > 0
    result_text.puts Util.colorize(:red, "No response from:")
    result_text.puts

    field_size = Util.field_size(@noresponsefrom, 30)
    fields_num = Util.field_number(field_size)
    format = "   " + ( " %-#{field_size}s" * fields_num )

    @noresponsefrom.sort.in_groups_of(fields_num) do |c|
      result_text.puts format % c
    end
  end

  result_text.string
end

#node_responded(node) ⇒ Object

Helper to keep track of who we received responses from



132
133
134
135
136
# File 'lib/mcollective/rpc/stats.rb', line 132

def node_responded(node)
  @responsesfrom << node
rescue
  @responsesfrom = [node]
end

#okObject

increment the count of ok hosts



58
59
60
61
62
# File 'lib/mcollective/rpc/stats.rb', line 58

def ok
  @okcount += 1
rescue
  @okcount = 1
end

#report(caption = "rpc stats", summarize = true, verbose = false) ⇒ Object

Returns a blob of text representing the request status based on the stats contained in this class



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/mcollective/rpc/stats.rb', line 190

def report(caption = "rpc stats", summarize = true, verbose = false)
  result_text = []

  if verbose
      if @aggregate_summary.size > 0 && summarize
        result_text << text_for_aggregates
      else
        result_text << ""
      end

    result_text << Util.colorize(:yellow, "---- #{caption} ----")

    if @discovered
      @responses < @discovered ? color = :red : color = :reset
      result_text << "           Nodes: %s / %s" % [ Util.colorize(color, @discovered), Util.colorize(color, @responses) ]
    else
      result_text << "           Nodes: #{@responses}"
    end

    @failcount < 0 ? color = :red : color = :reset

    result_text << "     Pass / Fail: %s / %s" % [Util.colorize(color, @okcount), Util.colorize(color, @failcount) ]
    result_text << "      Start Time: %s"      % [Time.at(@starttime)]
    result_text << "  Discovery Time: %.2fms"  % [@discoverytime * 1000]
    result_text << "      Agent Time: %.2fms"  % [@blocktime * 1000]
    result_text << "      Total Time: %.2fms"  % [@totaltime * 1000]
  else
    if @discovered
      @responses < @discovered ? color = :red : color = :green

      if @aggregate_summary.size + @aggregate_failures.size > 0 && summarize
        result_text << text_for_aggregates
      else
        result_text << ""
      end

      result_text << "Finished processing %s / %s hosts in %.2f ms" % [Util.colorize(color, @responses), Util.colorize(color, @discovered), @blocktime * 1000]
    else
      result_text << "Finished processing %s hosts in %.2f ms" % [Util.colorize(:bold, @responses), @blocktime * 1000]
    end
  end

  no_response_r = no_response_report
  unexpected_response_r = unexpected_response_report
  if no_response_r || unexpected_response_r
    result_text << ""
  end

  if no_response_r != ""
    result_text << "" << no_response_r
  end

  if unexpected_response_r != ""
    result_text << "" << unexpected_response_r
  end

  if no_response_r || unexpected_response_r
    result_text << ""
  end

  result_text.join("\n")
end

#resetObject

Resets stats, if discovery time is set we keep it as it was



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mcollective/rpc/stats.rb', line 14

def reset
  @noresponsefrom = []
  @unexpectedresponsefrom = []
  @responsesfrom = []
  @responses = 0
  @starttime = Time.now.to_f
  @discoverytime = 0 unless @discoverytime
  @blocktime = 0
  @totaltime = 0
  @discovered = 0
  @discovered_nodes = []
  @okcount = 0
  @failcount = 0
  @requestid = nil
  @aggregate_summary = []
  @aggregate_failures = []
end

#text_for_aggregatesObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mcollective/rpc/stats.rb', line 138

def text_for_aggregates
  result = StringIO.new

  @aggregate_summary.each do |aggregate|
    output_item = aggregate.result[:output]

    begin
      action_interface = @ddl.action_interface(aggregate.action)
      display_as = action_interface[:output][output_item][:display_as]
    rescue
      display_as = output_item
    end

    if aggregate.is_a?(Aggregate::Result::Base)
      aggregate_report = aggregate.to_s
    else
      next
    end

    result.puts Util.colorize(:bold, "Summary of %s:" % display_as)
    result.puts
    unless aggregate_report == ""
      result.puts aggregate.to_s.split("\n").map{|x| "   " + x}.join("\n")
    else
      result.puts Util.colorize(:yellow, "     No aggregate summary could be computed")
    end
    result.puts
  end

  @aggregate_failures.each do |failed|
    case(failed[:type])
    when :startup
      message = "exception raised while processing startup hook"
    when :create
      message = "unspecified output '#{failed[:name]}' for the action"
    when :process_result
      message = "exception raised while processing result data"
    when :summarize
      message = "exception raised while summarizing"
    end

    result.puts Util.colorize(:bold, "Summary of %s:" % failed[:name])
    result.puts
    result.puts Util.colorize(:yellow, "     Could not compute summary - %s" % message)
    result.puts
  end

  result.string
end

#time_block_execution(action) ⇒ Object

helper to time block execution time



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mcollective/rpc/stats.rb', line 97

def time_block_execution(action)
  if action == :start
    @block_start = Time.now.to_f
  elsif action == :end
    @blocktime += Time.now.to_f - @block_start
  else
    raise("Uknown block action #{action}")
  end
rescue
  @blocktime = 0
end

#time_discovery(action) ⇒ Object

Utility to time discovery from :start to :end



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mcollective/rpc/stats.rb', line 84

def time_discovery(action)
  if action == :start
    @discovery_start = Time.now.to_f
  elsif action == :end
    @discoverytime = Time.now.to_f - @discovery_start
  else
    raise("Uknown discovery action #{action}")
  end
rescue
  @discoverytime = 0
end

#to_hashObject

returns a hash of our stats



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mcollective/rpc/stats.rb', line 33

def to_hash
  {:noresponsefrom    => @noresponsefrom,
   :unexpectedresponsefrom => @unexpectedresponsefrom,
   :starttime         => @starttime,
   :discoverytime     => @discoverytime,
   :blocktime         => @blocktime,
   :responses         => @responses,
   :totaltime         => @totaltime,
   :discovered        => @discovered,
   :discovered_nodes  => @discovered_nodes,
   :okcount           => @okcount,
   :requestid         => @requestid,
   :failcount         => @failcount,
   :aggregate_summary => @aggregate_summary,
   :aggregate_failures => @aggregate_failures}
end

#unexpected_response_reportObject

Returns a blob of text indicating what nodes responded but weren’t discovered



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/mcollective/rpc/stats.rb', line 274

def unexpected_response_report
  result_text = StringIO.new

  if @unexpectedresponsefrom.size > 0
    result_text.puts Util.colorize(:red, "Unexpected response from:")
    result_text.puts

    field_size = Util.field_size(@unexpectedresponsefrom, 30)
    fields_num = Util.field_number(field_size)
    format = "   " + ( " %-#{field_size}s" * fields_num )

    @unexpectedresponsefrom.sort.in_groups_of(fields_num) do |c|
      result_text.puts format % c
    end
  end

  result_text.string
end