7 from collections import deque
9 from twisted.internet import reactor, defer
10 from twisted.application import service
11 from twisted.application.internet import TimerService
12 from zope.interface import implements
14 from foolscap.logging.gatherer import get_local_ip_for
15 from twisted.internet.error import ConnectionDone, ConnectionLost
16 from foolscap import DeadReferenceError
18 from allmydata.util import log
19 from allmydata.interfaces import RIStatsProvider, RIStatsGatherer, IStatsProducer
21 class LoadMonitor(service.MultiService):
22 implements(IStatsProducer)
27 def __init__(self, provider, warn_if_delay_exceeds=1):
28 service.MultiService.__init__(self)
29 self.provider = provider
30 self.warn_if_delay_exceeds = warn_if_delay_exceeds
36 def startService(self):
39 self.timer = reactor.callLater(self.loop_interval, self.loop)
40 service.MultiService.startService(self)
42 def stopService(self):
47 return service.MultiService.stopService(self)
54 if self.last is not None:
55 delay = now - self.last - self.loop_interval
56 if delay > self.warn_if_delay_exceeds:
57 log.msg(format='excessive reactor delay (%ss)', args=(delay,),
59 self.stats.append(delay)
60 while len(self.stats) > self.num_samples:
64 self.timer = reactor.callLater(self.loop_interval, self.loop)
68 avg = sum(self.stats) / len(self.stats)
72 return { 'load_monitor.avg_load': avg,
73 'load_monitor.max_load': m_x, }
75 class StatsProvider(foolscap.Referenceable, service.MultiService):
76 implements(RIStatsProvider)
78 def __init__(self, node, gatherer_furl):
79 service.MultiService.__init__(self)
81 self.gatherer_furl = gatherer_furl
84 self.stats_producers = []
86 self.load_monitor = LoadMonitor(self)
87 self.load_monitor.setServiceParent(self)
88 self.register_producer(self.load_monitor)
90 def startService(self):
92 d = self.node.when_tub_ready()
94 nickname = self.node.get_config('nickname')
95 self.node.tub.connectTo(self.gatherer_furl, self._connected, nickname)
96 d.addCallback(connect)
97 service.MultiService.startService(self)
99 def count(self, name, delta):
100 val = self.counters.setdefault(name, 0)
101 self.counters[name] = val + delta
103 def register_producer(self, stats_producer):
104 self.stats_producers.append(IStatsProducer(stats_producer))
106 def remote_get_stats(self):
108 for sp in self.stats_producers:
109 stats.update(sp.get_stats())
110 return { 'counters': self.counters, 'stats': stats }
112 def _connected(self, gatherer, nickname):
113 gatherer.callRemoteOnly('provide', self, nickname or '')
115 class StatsGatherer(foolscap.Referenceable, service.MultiService):
116 implements(RIStatsGatherer)
120 def __init__(self, tub, basedir):
121 service.MultiService.__init__(self)
123 self.basedir = basedir
128 def startService(self):
129 # the Tub must have a location set on it by now
130 service.MultiService.startService(self)
131 self.timer = TimerService(self.poll_interval, self.poll)
132 self.timer.setServiceParent(self)
133 self.registerGatherer()
138 def registerGatherer(self):
139 furl_file = os.path.join(self.basedir, "stats_gatherer.furl")
140 self.my_furl = self.tub.registerReference(self, furlFile=furl_file)
142 def get_tubid(self, rref):
143 return foolscap.SturdyRef(rref.tracker.getURL()).getTubRef().getTubID()
145 def remote_provide(self, provider, nickname):
146 tubid = self.get_tubid(provider)
147 if tubid == '<unauth>':
148 print "WARNING: failed to get tubid for %s (%s)" % (provider, nickname)
149 # don't add to clients to poll (polluting data) don't care about disconnect
151 self.clients[tubid] = provider
152 self.nicknames[tubid] = nickname
155 for tubid,client in self.clients.items():
156 nickname = self.nicknames.get(tubid)
157 d = client.callRemote('get_stats')
158 d.addCallbacks(self.got_stats, self.lost_client,
159 callbackArgs=(tubid, nickname),
160 errbackArgs=(tubid,))
161 d.addErrback(self.log_client_error, tubid)
163 def lost_client(self, f, tubid):
164 # this is called lazily, when a get_stats request fails
165 del self.clients[tubid]
166 del self.nicknames[tubid]
167 f.trap(DeadReferenceError, ConnectionDone, ConnectionLost)
169 def log_client_error(self, f, tubid):
170 log.msg("StatsGatherer: error in get_stats(), peerid=%s" % tubid,
171 level=log.UNUSUAL, failure=f)
173 def got_stats(self, stats, tubid, nickname):
174 raise NotImplementedError()
176 class StdOutStatsGatherer(StatsGatherer):
178 def remote_provide(self, provider, nickname):
179 tubid = self.get_tubid(provider)
181 print 'connect "%s" [%s]' % (nickname, tubid)
182 provider.notifyOnDisconnect(self.announce_lost_client, tubid)
183 StatsGatherer.remote_provide(self, provider, nickname)
185 def announce_lost_client(self, tubid):
186 print 'disconnect "%s" [%s]:' % (self.nicknames[tubid], tubid)
188 def got_stats(self, stats, tubid, nickname):
189 print '"%s" [%s]:' % (nickname, tubid)
192 class PickleStatsGatherer(StdOutStatsGatherer):
193 # inherit from StdOutStatsGatherer for connect/disconnect notifications
195 def __init__(self, tub, basedir=".", verbose=True):
196 self.verbose = verbose
197 StatsGatherer.__init__(self, tub, basedir)
198 self.picklefile = os.path.join(basedir, "stats.pickle")
200 if os.path.exists(self.picklefile):
201 f = open(self.picklefile, 'rb')
202 self.gathered_stats = pickle.load(f)
205 self.gathered_stats = {}
207 def got_stats(self, stats, tubid, nickname):
208 s = self.gathered_stats.setdefault(tubid, {})
209 s['timestamp'] = time.time()
210 s['nickname'] = nickname
214 def dump_pickle(self):
215 tmp = "%s.tmp" % (self.picklefile,)
217 pickle.dump(self.gathered_stats, f)
219 if os.path.exists(self.picklefile):
220 os.unlink(self.picklefile)
221 os.rename(tmp, self.picklefile)
223 class GathererApp(object):
226 d.addCallback(self._tub_ready)
229 self._tub = foolscap.Tub(certFile="stats_gatherer.pem")
230 self._tub.setOption("logLocalFailures", True)
231 self._tub.setOption("logRemoteFailures", True)
232 self._tub.startService()
233 portnumfile = "portnum"
235 portnum = int(open(portnumfile, "r").read())
236 except (EnvironmentError, ValueError):
238 self._tub.listenOn("tcp:%d" % portnum)
239 d = defer.maybeDeferred(get_local_ip_for)
240 d.addCallback(self._set_location)
241 d.addCallback(lambda res: self._tub)
244 def _set_location(self, local_address):
245 if local_address is None:
246 local_addresses = ["127.0.0.1"]
248 local_addresses = [local_address, "127.0.0.1"]
249 l = self._tub.getListeners()[0]
250 portnum = l.getPortnum()
251 portnumfile = "portnum"
252 open(portnumfile, "w").write("%d\n" % portnum)
253 local_addresses = [ "%s:%d" % (addr, portnum,)
254 for addr in local_addresses ]
255 assert len(local_addresses) >= 1
256 location = ",".join(local_addresses)
257 self._tub.setLocation(location)
259 def _tub_ready(self, tub):
260 sg = PickleStatsGatherer(tub, ".")
261 sg.setServiceParent(tub)
263 print '\nStatsGatherer: %s\n' % (sg.get_furl(),)
269 if __name__ == '__main__':