Skip to content
Snippets Groups Projects
Commit ebae3805 authored by Stéphane Diemer's avatar Stéphane Diemer
Browse files

Use systemctl if dbus is not installed to get fail2ban state.

parent 798c7b3b
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,6 @@ Checks the current state of the fail2ban service.
from pathlib import Path
import sys
# pylint: disable=E0401
import dbus
sys.path.append(str(Path(__file__).parents[1].resolve()))
......@@ -25,22 +23,43 @@ def get_service_state(name: str) -> tuple:
:rtype: tuple
"""
bus = dbus.SystemBus()
systemd = bus.get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
manager = dbus.Interface(systemd, "org.freedesktop.systemd1.Manager")
unit = manager.LoadUnit("{}.service".format(name))
proxy = bus.get_object("org.freedesktop.systemd1", str(unit))
active = proxy.Get(
"org.freedesktop.systemd1.Unit",
"ActiveState",
dbus_interface="org.freedesktop.DBus.Properties",
)
state = proxy.Get(
"org.freedesktop.systemd1.Unit",
"SubState",
dbus_interface="org.freedesktop.DBus.Properties",
)
try:
# pylint: disable=E0401
import dbus
except ImportError:
u.log("Using systemctl to get current state.")
returncode, output = u.exec_cmd(
"systemctl status fail2ban | grep 'Active:'", log_output=False
)
if returncode != 0:
active = "no"
state = "not installed"
else:
info = output.replace("Active:", "").strip().split(" ")
if len(info) > 1:
active = info[0]
state = info[1].strip("()")
else:
active = "no"
state = "unknown"
else:
u.log("Using dbus to get current state.")
bus = dbus.SystemBus()
systemd = bus.get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
manager = dbus.Interface(systemd, "org.freedesktop.systemd1.Manager")
unit = manager.LoadUnit("{}.service".format(name))
proxy = bus.get_object("org.freedesktop.systemd1", str(unit))
active = proxy.Get(
"org.freedesktop.systemd1.Unit",
"ActiveState",
dbus_interface="org.freedesktop.DBus.Properties",
)
state = proxy.Get(
"org.freedesktop.systemd1.Unit",
"SubState",
dbus_interface="org.freedesktop.DBus.Properties",
)
return str(active), str(state)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment