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.



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

def initialize
  reset
end

Instance Attribute Details

#aggregate_failuresObject

Returns the value of attribute aggregate_failures.



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

def aggregate_failures
  @aggregate_failures
end

#aggregate_summaryObject

Returns the value of attribute aggregate_summary.



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

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.



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

def ddl
  @ddl
end

#discoveredObject

Returns the value of attribute discovered.



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

def discovered
  @discovered
end

#discovered_nodesObject

Returns the value of attribute discovered_nodes.



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

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.



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

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.



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

def okcount
  @okcount
end

#requestidObject

Returns the value of attribute requestid.



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

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.



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

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.



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

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



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

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

#client_stats=(stats) ⇒ Object

Re-initializes the object with stats from the basic client



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

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



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

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

#failObject

increment the count of failed hosts



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

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

#finish_requestObject

Helper to calculate total time etc



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

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/mcollective/rpc/stats.rb', line 245

def no_response_report
  result_text = StringIO.new

  unless @noresponsefrom.empty?
    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



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

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

#okObject

increment the count of ok hosts



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

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



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
# File 'lib/mcollective/rpc/stats.rb', line 191

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

  if verbose
    if !@aggregate_summary.empty? && 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]
  elsif @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

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

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

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

  result_text << "" if no_response_r || unexpected_response_r

  result_text.join("\n")
end

#resetObject

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



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

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

#text_for_aggregatesObject



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
187
# File 'lib/mcollective/rpc/stats.rb', line 139

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
    if aggregate_report == ""
      result.puts Util.colorize(:yellow, "     No aggregate summary could be computed")
    else
      result.puts aggregate.to_s.split("\n").map {|x| "   #{x}"}.join("\n")
    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
108
# File 'lib/mcollective/rpc/stats.rb', line 97

def time_block_execution(action)
  case action
  when :start
    @block_start = Time.now.to_f
  when :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



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

def time_discovery(action)
  case action
  when :start
    @discovery_start = Time.now.to_f
  when :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



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

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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/mcollective/rpc/stats.rb', line 265

def unexpected_response_report
  result_text = StringIO.new

  unless @unexpectedresponsefrom.empty?
    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