]> git.ipfire.org Git - collecty.git/blame - src/collecty/bus.py
dbus: Move bus domain into bus submodule
[collecty.git] / src / collecty / bus.py
CommitLineData
f37913e8 1#!/usr/bin/python3
c968f6d9
MT
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
22import dbus
23import dbus.mainloop.glib
24import dbus.service
f37913e8
MT
25import gi.repository.GLib
26import gi.repository.GObject
16b84672 27import logging
c968f6d9
MT
28import threading
29
f37913e8 30from .i18n import _
a031204a 31from .constants import *
c968f6d9 32
c968f6d9 33log = logging.getLogger("collecty.bus")
c968f6d9 34
44cc4f59
MT
35DOMAIN = "org.ipfire.collecty1"
36
c968f6d9
MT
37class Bus(threading.Thread):
38 def __init__(self, collecty):
39 threading.Thread.__init__(self)
40 self.daemon = True
41
42 self.collecty = collecty
43
44 # Initialise the main loop
f37913e8
MT
45 gi.repository.GObject.threads_init()
46 dbus.mainloop.glib.threads_init()
47 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
48
49 self.loop = gi.repository.GLib.MainLoop()
c968f6d9
MT
50
51 # Register the GraphGenerator interface
52 self.generator = GraphGenerator(self.collecty)
53
54 def run(self):
55 log.debug(_("Bus thread has started"))
56
57 # Run the main loop
f37913e8
MT
58 try:
59 self.loop.run()
60 except KeyboardInterrupt:
61 self.collecty.shutdown()
62
63 log.debug(_("Bus thread has ended"))
c968f6d9
MT
64
65 def shutdown(self):
66 log.debug(_("Stopping bus thread"))
67
68 # End the main loop
69 self.loop.quit()
70
71 # Return when this thread has finished
72 return self.join()
73
74
75class GraphGenerator(dbus.service.Object):
76 def __init__(self, collecty):
44cc4f59 77 bus_name = dbus.service.BusName(DOMAIN, bus=dbus.SystemBus())
c968f6d9
MT
78 dbus.service.Object.__init__(self, bus_name, "/%s" % self.__class__.__name__)
79
80 self.collecty = collecty
81
44cc4f59 82 @dbus.service.method(DOMAIN, in_signature="s")
6d7f3cac
MT
83 def Backup(self, filename):
84 self.collecty.backup(filename)
85
44cc4f59 86 @dbus.service.method(DOMAIN, in_signature="sa{sv}", out_signature="a{sv}")
c968f6d9
MT
87 def GenerateGraph(self, template_name, kwargs):
88 """
89 Returns a graph generated from the given template and object.
90 """
91 graph = self.collecty.generate_graph(template_name, **kwargs)
92
a3864812
MT
93 # Convert the graph back to normal Python format
94 if graph:
95 graph["image"] = dbus.ByteArray(graph["image"] or [])
96
97 return graph
98
44cc4f59 99 @dbus.service.method(DOMAIN, in_signature="", out_signature="a{sv}")
a3864812
MT
100 def GraphInfo(self, template_name, kwargs):
101 """
102 Returns a dictionary with information about the graph.
103 """
104 return self.collecty.graph_info(template_name, **kwargs)
c968f6d9 105
44cc4f59 106 @dbus.service.method(DOMAIN, in_signature="sa{sv}", out_signature="a{sv}")
8ee5a71a
MT
107 def LastUpdate(self, template_name, kwargs):
108 """
109 Returns a graph generated from the given template and object.
110 """
111 last_update = self.collecty.last_update(template_name, **kwargs)
112
113 # Serialise datetime as string
114 if last_update:
115 last_update["timestamp"] = last_update["timestamp"].isoformat()
116
117 return last_update
118
44cc4f59 119 @dbus.service.method(DOMAIN, in_signature="", out_signature="as")
c968f6d9
MT
120 def ListTemplates(self):
121 """
122 Returns a list of all available templates
123 """
124 return [t.name for t in self.collecty.templates]
73241420 125
44cc4f59 126 @dbus.service.method(DOMAIN, in_signature="", out_signature="s")
73241420
MT
127 def Version(self):
128 return COLLECTY_VERSION