diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2017-07-26 21:22:50 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2017-07-26 21:22:50 -0400 |
commit | 0b1e1740d3a0aeb5278bb15b0b71ac76e2de5d08 (patch) | |
tree | 11951bf9ef5626dc38508591c840c302902aee57 |
initial commit
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | systemd.py | 48 |
2 files changed, 49 insertions, 0 deletions
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) |