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.



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

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

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

def client_stats=(stats)
  @noresponsefrom = stats[:noresponsefrom]
  @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



109
110
111
112
# File 'lib/mcollective/rpc/stats.rb', line 109

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



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mcollective/rpc/stats.rb', line 115

def finish_request
  @totaltime = @blocktime + @discoverytime

  # figures out who we had no responses from
  dhosts = @discovered_nodes.clone
  @responsesfrom.each {|r| dhosts.delete(r)}
  @noresponsefrom = dhosts
rescue
  @totaltime = 0
  @noresponsefrom = []
end

#no_response_reportObject

Returns a blob of text indicating what nodes did not respond



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/mcollective/rpc/stats.rb', line 236

def no_response_report
  result_text = StringIO.new

  if @noresponsefrom.size > 0
    result_text.puts
    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

    result_text.puts
  end

  result_text.string
end

#node_responded(node) ⇒ Object

Helper to keep track of who we received responses from



128
129
130
131
132
# File 'lib/mcollective/rpc/stats.rb', line 128

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



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

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

  if no_response_report != ""
    result_text << "" << no_response_report
  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 = []
  @responsesfrom = []
  @responses = 0
  @starttime = Time.now.to_f
  @discoverytime = 0 unless @discoverytime
  @blocktime = 0
  @totaltime = 0
  @discovered = 0
  @discovered_nodes = []
  @okcount = 0
  @failcount = 0
  @noresponsefrom = []
  @requestid = nil
  @aggregate_summary = []
  @aggregate_failures = []
end

#text_for_aggregatesObject



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

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



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

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



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

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

def to_hash
  {:noresponsefrom    => @noresponsefrom,
   :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