From 0b1e1740d3a0aeb5278bb15b0b71ac76e2de5d08 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 26 Jul 2017 21:22:50 -0400 Subject: initial commit --- .gitignore | 1 + systemd.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 .gitignore create mode 100755 systemd.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/systemd.py b/systemd.py new file mode 100755 index 0000000..9b85660 --- /dev/null +++ b/systemd.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python2 +# +# This should be both valid Python 2 and 3, but currently the collectd +# Python plugin is only Python 2. + +import dbus + +def get_property(dbus_iface, propname): + return dbus_iface.proxy_object.Get(dbus_iface.dbus_interface, propname, dbus_interface='org.freedesktop.DBus.Properties') + +def init(): + global system_manager + system_manager = dbus.Interface(dbus.SystemBus().get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1"), "org.freedesktop.systemd1.Manager") + +def read(): + global system_manager + dat = dict() + for unit in system_manager.ListUnits(): + key = ('count', 'N%sUnits' % unit[3].capitalize()) + dat[key] = dat.get(key, 0) + 1 + + for count in ['NJobs']: + dat[('count', count)] = int(get_property(system_manager, count)) + for counter in ['NInstalledJobs', 'NFailedJobs']: + dat[('counter', counter)] = int(get_property(system_manager, counter)) + + return dat + +def dump(dat): + for key, val in read().iteritems(): + if __name__ == '__main__': + print("%s => %s" % (key, val)) + else: + collectd.Values(plugin='systemd', type=key[0], type_instance=key[1], values=[val]).dispatch() + + +if __name__ == '__main__': + init() + dump(read()) +else: + import collectd + def _config(conf): + pass + def _read(): + dump(read()) + collectd.register_config(_config) + collectd.register_init(init) + collectd.register_read(_read) -- cgit v1.2.3