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

#blocktimeObject

Returns the value of attribute blocktime.



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

def blocktime
  @blocktime
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

#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



44
45
46
47
48
# File 'lib/mcollective/rpc/stats.rb', line 44

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

#client_stats=(stats) ⇒ Object

Re-initializes the object with stats from the basic client



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

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

#discovered_agents(agents) ⇒ Object

Update discovered and discovered_nodes based on discovery results



102
103
104
105
# File 'lib/mcollective/rpc/stats.rb', line 102

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

#failObject

increment the count of failed hosts



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

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

#finish_requestObject

Helper to calculate total time etc



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mcollective/rpc/stats.rb', line 108

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/mcollective/rpc/stats.rb', line 167

def no_response_report
  result_text = []

  if @noresponsefrom.size > 0
    result_text << Helpers.colorize(:red, "\nNo response from:\n")

    @noresponsefrom.each_with_index do |c,i|
      result_text << "" if i % 4 == 0
      result_text << "%30s" % [c]
    end

    result_text << ""
  end

  result_text.join("\n")
end

#node_responded(node) ⇒ Object

Helper to keep track of who we received responses from



121
122
123
124
125
# File 'lib/mcollective/rpc/stats.rb', line 121

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

#okObject

increment the count of ok hosts



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

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

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

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



129
130
131
132
133
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
# File 'lib/mcollective/rpc/stats.rb', line 129

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

  if verbose
    result_text << Helpers.colorize(:yellow, "---- #{caption} ----")

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

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

    result_text << "     Pass / Fail: %s / %s" % [Helpers.colorize(color, @okcount), Helpers.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

      result_text << "Finished processing %s / %s hosts in %.2f ms" % [Helpers.colorize(color, @responses), Helpers.colorize(color, @discovered), @blocktime * 1000]
    else
      result_text << "Finished processing %s hosts in %.2f ms" % [Helpers.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



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

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 = []
end

#time_block_execution(action) ⇒ Object

helper to time block execution time



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mcollective/rpc/stats.rb', line 88

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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mcollective/rpc/stats.rb', line 75

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



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mcollective/rpc/stats.rb', line 29

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