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