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