1
1
mirror of https://gitlab.archlinux.org/archlinux/infrastructure.git synced 2025-01-18 08:06:16 +01:00
infrastructure/roles/zabbix_agent/files/systemd-discover-accounting-units.py
Sven-Hendrik Haase a636f8a597 Remove arch-boxes stuff (fixes #107)
This is now built enitrely in GitLab CI in the arch-boxes repo so this is no longer required.
2020-08-28 20:05:24 +00:00

75 lines
2.2 KiB
Python

#!/usr/bin/python
import dbus
import json
import re
# regex are ancored to beginning and end of string already
unit_whitelist_regexes = [
r'archive-uploader.service',
r'borg-backup.service',
r'createlinks.service',
r'dovecot.service',
r'fcgiwrap-.*.service',
r'grafana.service',
r'mailman.service',
r'mariadb.service',
r'matrix-appservice-irc.service',
r'mysqld.service',
r'nginx.service',
r'nginx-zabbix.service',
r'opendkim.service',
r'php-fpm.service',
r'php-fpm@.*.service',
r'postfix.service',
r'postfwd.service',
r'postgresql.service',
r'quassel.service',
r'security-tracker-update.service',
r'syslog-ng@.*.service',
r'spampd.service',
r'sshd.service',
r'svnserve.service',
r'synapse.service',
r'system-rsyncd.slice',
r'unbound.service',
r'uwsgi@.*.service',
r'zabbix-agent.service',
r'zabbix-server-pgsql.service',
r'fail2ban.service',
]
bus = dbus.SystemBus()
systemd1 = bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
systemd1_manager = dbus.Interface(systemd1, dbus_interface='org.freedesktop.systemd1.Manager')
units = systemd1_manager.ListUnits()
discovered = []
def get_unit_prop_interface(obj, interface, propname):
try:
return obj.Get(interface, propname)
except dbus.exceptions.DBusException as err:
if err.get_dbus_name() == 'org.freedesktop.DBus.Error.UnknownProperty':
return None
else:
raise
def get_unit_prop(obj, propname):
interfaces = ['org.freedesktop.systemd1.Service', 'org.freedesktop.systemd1.Slice']
for interface in interfaces:
ret = get_unit_prop_interface(obj, interface, propname)
if ret:
return ret
return None
combined = '|'.join(unit_whitelist_regexes)
unit_whitelist_compiled = re.compile('^('+combined+')$')
for u in units:
if unit_whitelist_compiled.match(u[0]):
discovered.append({"{#UNITNAME}": u[0].replace("@", "--AT--")})
print(json.dumps({"data": discovered}));