Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions rootfs/standard/var/pynode/systemctl_info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import subprocess
from utilities import *

Expand All @@ -11,28 +10,38 @@ def clear_service_enabled_cache():
global service_enabled_cache
service_enabled_cache = {}

def run_systemctl_status_command(command, service_name):
return subprocess.run(
["systemctl", command, "--no-pager", "--", str(service_name)],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).returncode

def is_service_enabled(service_name, force_refresh=False):
global service_enabled_cache

if service_name in service_enabled_cache and force_refresh == False:
return service_enabled_cache[service_name]

code = os.system("systemctl is-enabled {} > /dev/null 2>&1".format(service_name))
code = subprocess.run(
["systemctl", "is-enabled", "--", str(service_name)],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).returncode
if code == 0:
service_enabled_cache[service_name] = True
return True
service_enabled_cache[service_name] = False
return False

def get_service_status_code(service_name):
code = os.system("systemctl status {} --no-pager > /dev/null 2>&1".format(service_name))
return code
return run_systemctl_status_command("status", service_name)

def get_service_status_basic_text(service_name):
if not is_service_enabled(service_name):
return "Disabled"

code = os.system("systemctl status {} --no-pager > /dev/null 2>&1".format(service_name))
code = run_systemctl_status_command("status", service_name)
if code == 0:
return "Running"
return "Error"
Expand All @@ -41,14 +50,18 @@ def get_service_status_color(service_name):
if not is_service_enabled(service_name):
return "gray"

code = os.system("systemctl status {} --no-pager > /dev/null 2>&1".format(service_name))
code = run_systemctl_status_command("status", service_name)
if code == 0:
return "green"
return "red"

def get_journalctl_log(service_name):
try:
log = to_string(subprocess.check_output("journalctl -r --unit={} --no-pager | head -n 300".format(service_name), shell=True).decode("utf8"))
log = to_string(
subprocess.check_output(
["journalctl", "-r", "-n", "300", f"--unit={service_name}", "--no-pager"]
).decode("utf8")
)
except:
log = "ERROR"
return log