From fe351811305fa4e5f9a955f97e1721f27fe3e2a6 Mon Sep 17 00:00:00 2001 From: Joseph Hall Date: Wed, 20 Jul 2016 14:54:49 -0600 Subject: [PATCH] List running systemd services --- salt/modules/systemd.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/salt/modules/systemd.py b/salt/modules/systemd.py index 9f1c6761c3..bbda6b8322 100644 --- a/salt/modules/systemd.py +++ b/salt/modules/systemd.py @@ -329,6 +329,45 @@ def systemctl_reload(): return True +def get_running(): + ''' + Return a list of all running services, so far as systemd is concerned + + CLI Example: + + .. code-block:: bash + + salt '*' service.get_running + ''' + ret = set() + # Get running systemd units + out = __salt__['cmd.run']( + _systemctl_cmd('--full --no-legend --no-pager'), + python_shell=False, + ignore_retcode=True, + ) + for line in salt.utils.itertools.split(out, '\n'): + try: + comps = line.strip().split() + fullname = comps[0] + if len(comps) > 3: + active_state = comps[3] + except ValueError as exc: + log.error(exc) + continue + else: + if active_state != 'running': + continue + try: + unit_name, unit_type = fullname.rsplit('.', 1) + except ValueError: + continue + if unit_type in VALID_UNIT_TYPES: + ret.add(unit_name if unit_type == 'service' else fullname) + + return sorted(ret) + + def get_enabled(): ''' Return a list of all enabled services