summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2017-08-19 18:01:24 -0400
committerLuke Shumaker <lukeshu@lukeshu.com>2017-08-19 18:01:24 -0400
commit314b0af75c06b9f2b36332d481c3b9e75b514c7c (patch)
tree5eb307354c841d3e2f325c132784b479045d4465
parentb5a38954e3a005812c08210eefce628eb2efa9fa (diff)
clean up, make it work with python3
-rwxr-xr-xsystemd.py51
1 files changed, 30 insertions, 21 deletions
diff --git a/systemd.py b/systemd.py
index 7024103..219bb16 100755
--- a/systemd.py
+++ b/systemd.py
@@ -1,13 +1,29 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
#
-# This should be both valid Python 2 and 3, but currently the collectd
-# Python plugin is only Python 2.
+# This should be both valid Python 2 and 3, as the collectd Python
+# plugin can be either Python 2 or 3 (decided at compile time).
+#
+# Oddly, the Arch 'collectd' package chose Python 2, despite using
+# Python 3 as the default system Python. I assume they have a good
+# reason for it, though I can't find it discussed anywhere.
+import sys
import dbus
+# For compatibility with Python 2 & 3
+if sys.version_info.major is 2:
+ def iteritems(d):
+ return d.iteritems()
+else:
+ def iteritems(d):
+ return d.items()
+
+# DBus utility function
def get_property(dbus_iface, propname):
return dbus_iface.proxy_object.Get(dbus_iface.dbus_interface, propname, dbus_interface='org.freedesktop.DBus.Properties')
+# The main program
+
def init():
global system_manager
system_manager = dbus.Interface(dbus.SystemBus().get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1"), "org.freedesktop.systemd1.Manager")
@@ -34,31 +50,24 @@ def read():
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())
+ for (key, val) in iteritems(read()):
+ print("%s => %s" % (key, val))
else:
import collectd
def _config(conf):
pass
def _read():
- dump(read())
+ for (key, val) in iteritems(read()):
+ collectd.info("---- dump() ----")
+ collectd.Values(
+ plugin='systemd',
+ type='count',
+ type_instance=key
+ ).dispatch(
+ values=[val]
+ )
collectd.register_config(_config)
collectd.register_init(init)
collectd.register_read(_read)