]>
git.ipfire.org Git - oddments/collecty.git/blob - src/collecty/bus.py
947bf1029abe3a15bd31fa2a270c83bbcde0657e
2 ###############################################################################
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2015 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
23 import dbus
.mainloop
.glib
25 import gi
.repository
.GLib
26 import gi
.repository
.GObject
32 log
= logging
.getLogger("collecty.bus")
34 DOMAIN
= "org.ipfire.collecty1"
36 class Bus(threading
.Thread
):
37 def __init__(self
, collecty
):
38 threading
.Thread
.__init
__(self
)
41 self
.collecty
= collecty
43 # Initialise the main loop
44 gi
.repository
.GObject
.threads_init()
45 dbus
.mainloop
.glib
.threads_init()
46 dbus
.mainloop
.glib
.DBusGMainLoop(set_as_default
=True)
48 self
.loop
= gi
.repository
.GLib
.MainLoop()
50 # Register the GraphGenerator interface
51 self
.generator
= GraphGenerator(self
.collecty
)
54 log
.debug(_("Bus thread has started"))
59 except KeyboardInterrupt:
60 self
.collecty
.shutdown()
62 log
.debug(_("Bus thread has ended"))
65 log
.debug(_("Stopping bus thread"))
70 # Return when this thread has finished
74 class GraphGenerator(dbus
.service
.Object
):
75 def __init__(self
, collecty
):
76 bus_name
= dbus
.service
.BusName(DOMAIN
, bus
=dbus
.SystemBus())
77 dbus
.service
.Object
.__init
__(self
, bus_name
, "/%s" % self
.__class
__.__name
__)
79 self
.collecty
= collecty
81 @dbus.service
.method(DOMAIN
, in_signature
="s")
82 def Backup(self
, filename
):
83 self
.collecty
.backup(filename
)
85 @dbus.service
.method(DOMAIN
, in_signature
="sa{sv}", out_signature
="a{sv}")
86 def GenerateGraph(self
, template_name
, kwargs
):
88 Returns a graph generated from the given template and object.
90 graph
= self
.collecty
.generate_graph(template_name
, **kwargs
)
92 # Convert the graph back to normal Python format
94 graph
["image"] = dbus
.ByteArray(graph
["image"] or [])
98 @dbus.service
.method(DOMAIN
, in_signature
="", out_signature
="a{sv}")
99 def GraphInfo(self
, template_name
, kwargs
):
101 Returns a dictionary with information about the graph.
103 return self
.collecty
.graph_info(template_name
, **kwargs
)
105 @dbus.service
.method(DOMAIN
, in_signature
="sa{sv}", out_signature
="a{sv}")
106 def LastUpdate(self
, template_name
, kwargs
):
108 Returns a graph generated from the given template and object.
110 last_update
= self
.collecty
.last_update(template_name
, **kwargs
)
112 # Serialise datetime as string
114 last_update
["timestamp"] = last_update
["timestamp"].isoformat()
118 @dbus.service
.method(DOMAIN
, in_signature
="", out_signature
="as")
119 def ListTemplates(self
):
121 Returns a list of all available templates
123 return [t
.name
for t
in self
.collecty
.templates
]
125 @dbus.service
.method(DOMAIN
, in_signature
="", out_signature
="s")
127 return COLLECTY_VERSION