]> git.ipfire.org Git - collecty.git/blob - src/collecty/bus.py
Add dbus interface
[collecty.git] / src / collecty / bus.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2015 IPFire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import dbus
23 import dbus.mainloop.glib
24 import dbus.service
25 import gobject
26 import threading
27
28 from constants import *
29 from i18n import _
30
31 import logging
32 log = logging.getLogger("collecty.bus")
33 log.propagate = 1
34
35 # Initialise the glib main loop
36 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
37 dbus.mainloop.glib.threads_init()
38
39 class Bus(threading.Thread):
40 def __init__(self, collecty):
41 threading.Thread.__init__(self)
42 self.daemon = True
43
44 self.collecty = collecty
45
46 # Initialise the main loop
47 gobject.threads_init()
48 self.loop = gobject.MainLoop()
49
50 # Register the GraphGenerator interface
51 self.generator = GraphGenerator(self.collecty)
52
53 def run(self):
54 log.debug(_("Bus thread has started"))
55
56 # Run the main loop
57 self.loop.run()
58
59 def shutdown(self):
60 log.debug(_("Stopping bus thread"))
61
62 # End the main loop
63 self.loop.quit()
64
65 # Return when this thread has finished
66 return self.join()
67
68
69 class GraphGenerator(dbus.service.Object):
70 def __init__(self, collecty):
71 bus_name = dbus.service.BusName(BUS_DOMAIN, bus=dbus.SystemBus())
72 dbus.service.Object.__init__(self, bus_name, "/%s" % self.__class__.__name__)
73
74 self.collecty = collecty
75
76 @dbus.service.method(BUS_DOMAIN, in_signature="sa{sv}", out_signature="ay")
77 def GenerateGraph(self, template_name, kwargs):
78 """
79 Returns a graph generated from the given template and object.
80 """
81 graph = self.collecty.generate_graph(template_name, **kwargs)
82
83 return dbus.ByteArray(graph or [])
84
85 @dbus.service.method(BUS_DOMAIN, in_signature="", out_signature="as")
86 def ListTemplates(self):
87 """
88 Returns a list of all available templates
89 """
90 return [t.name for t in self.collecty.templates]