summaryrefslogtreecommitdiff
path: root/systemd.py
blob: 702410343ae2457c5d38485ad1c6ab0462591871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/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 = {
        # These are the values of the
        # basic/unit-name.h:UnitActiveStates enum.
        'NActiveUnits': 0,
        'NReloadingUnits': 0,
        'NInactiveUnits': 0,
        'NFailedUnits': 0,
        'NActivatingUnits': 0,
        'NDeactivatingUnits': 0
    }
    for unit in system_manager.ListUnits():
        key = str('N%sUnits' % unit[3].capitalize())
        dat[key] = dat[key] + 1

    for prop in ['NJobs', 'NInstalledJobs', 'NFailedJobs']:
        dat[prop] = int(get_property(system_manager, prop))

    return dat

def dump(dat):
    collectd.info("---- dump() ----")
    for key, val in read().iteritems():
        if __name__ == '__main__':
            print("%s => %s" % (key, val))
        else:
            collectd.info("%s => %s" % (key, val))
            collectd.Values(
                plugin='systemd',
                type='count',
                type_instance=key
            ).dispatch(
                values=[val]
            )


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)