|
1 # copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
19 """looping task for dumping instance's stats in a file |
|
20 """ |
|
21 |
|
22 __docformat__ = "restructuredtext en" |
|
23 |
|
24 from datetime import datetime |
|
25 import json |
|
26 |
|
27 from cubicweb.server import hook |
|
28 |
|
29 class LogStatsStartHook(hook.Hook): |
|
30 """register task to regularly dump instance's stats in a file |
|
31 |
|
32 data are stored as one json entry per row |
|
33 """ |
|
34 __regid__ = 'cubicweb.hook.logstats.start' |
|
35 events = ('server_startup',) |
|
36 |
|
37 def __call__(self): |
|
38 interval = self.repo.config.get('logstat-interval', 0) |
|
39 if interval <= 0: |
|
40 return |
|
41 |
|
42 def dump_stats(repo): |
|
43 statsfile = repo.config.get('logstat-file') |
|
44 with repo.internal_cnx() as cnx: |
|
45 stats = cnx.call_service('repo_stats') |
|
46 gcstats = cnx.call_service('repo_gc_stats', nmax=5) |
|
47 |
|
48 allstats = {'resources': stats, |
|
49 'memory': gcstats, |
|
50 'timestamp': datetime.utcnow().isoformat(), |
|
51 } |
|
52 try: |
|
53 with open(statsfile, 'ab') as ofile: |
|
54 json.dump(allstats, ofile) |
|
55 ofile.write('\n') |
|
56 except IOError: |
|
57 repo.warning('Cannot open stats file for writing: %s', statsfile) |
|
58 |
|
59 self.repo.looping_task(interval, dump_stats, self.repo) |