337c969bc056eafd2fddee056eaa057c54d0b502
[packages/precise/mcollective.git] / lib / mcollective / runnerstats.rb
1 module MCollective
2   # Class to store stats about the mcollectived, it should live in the PluginManager
3   # so that agents etc can get hold of it and return the stats to callers
4   class RunnerStats
5     def initialize
6       @starttime = Time.now.to_i
7       @validated = 0
8       @unvalidated = 0
9       @filtered = 0
10       @passed = 0
11       @total = 0
12       @replies = 0
13       @ttlexpired = 0
14
15       @mutex = Mutex.new
16     end
17
18     # Records a message that failed TTL checks
19     def ttlexpired
20       Log.debug("Incrementing ttl expired stat")
21       @ttlexpired += 1
22     end
23
24     # Records a message that passed the filters
25     def passed
26       Log.debug("Incrementing passed stat")
27       @passed += 1
28     end
29
30     # Records a message that didnt pass the filters
31     def filtered
32       Log.debug("Incrementing filtered stat")
33       @filtered += 1
34     end
35
36     # Records a message that validated ok
37     def validated
38       Log.debug("Incrementing validated stat")
39       @validated += 1
40     end
41
42     def unvalidated
43       Log.debug("Incrementing unvalidated stat")
44       @unvalidated += 1
45     end
46
47     # Records receipt of a message
48     def received
49       Log.debug("Incrementing total stat")
50       @total += 1
51     end
52
53     # Records sending a message
54     def sent
55       @mutex.synchronize do
56         Log.debug("Incrementing replies stat")
57         @replies += 1
58       end
59     end
60
61     # Returns a hash with all stats
62     def to_hash
63       stats = {:validated => @validated,
64         :unvalidated => @unvalidated,
65         :passed => @passed,
66         :filtered => @filtered,
67         :starttime => @starttime,
68         :total => @total,
69         :ttlexpired => @ttlexpired,
70         :replies => @replies}
71
72       reply = {:stats => stats,
73         :threads => [],
74         :pid => Process.pid,
75         :times => {} }
76
77       ::Process.times.each_pair{|k,v|
78         k = k.to_sym
79         reply[:times][k] = v
80       }
81
82       Thread.list.each do |t|
83         reply[:threads] << "#{t.inspect}"
84       end
85
86       reply[:agents] = Agents.agentlist
87       reply
88     end
89   end
90 end