mynode/rootfs/standard/var/pynode/systemctl_info.py

55 lines
1.7 KiB
Python
Raw Normal View History

2020-10-08 02:59:44 +00:00
import os
import subprocess
2022-02-03 04:31:05 +00:00
from utilities import *
2020-10-08 02:59:44 +00:00
service_enabled_cache = {}
2020-10-08 02:59:44 +00:00
#==================================
# Service Status, Enabled, Logs, etc...
#==================================
def clear_service_enabled_cache():
global service_enabled_cache
service_enabled_cache = {}
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]
2022-04-21 04:09:09 +00:00
code = os.system("systemctl is-enabled {} > /dev/null 2>&1".format(service_name))
if code == 0:
service_enabled_cache[service_name] = True
2020-10-08 02:59:44 +00:00
return True
service_enabled_cache[service_name] = False
2020-10-08 02:59:44 +00:00
return False
def get_service_status_code(service_name):
2022-04-21 04:09:09 +00:00
code = os.system("systemctl status {} --no-pager > /dev/null 2>&1".format(service_name))
2020-10-08 02:59:44 +00:00
return code
def get_service_status_basic_text(service_name):
if not is_service_enabled(service_name):
return "Disabled"
2022-04-21 04:09:09 +00:00
code = os.system("systemctl status {} --no-pager > /dev/null 2>&1".format(service_name))
2020-10-08 02:59:44 +00:00
if code == 0:
return "Running"
return "Error"
def get_service_status_color(service_name):
if not is_service_enabled(service_name):
return "gray"
2022-04-21 04:09:09 +00:00
code = os.system("systemctl status {} --no-pager > /dev/null 2>&1".format(service_name))
2020-10-08 02:59:44 +00:00
if code == 0:
return "green"
return "red"
def get_journalctl_log(service_name):
try:
2022-01-30 19:58:00 +00:00
log = to_string(subprocess.check_output("journalctl -r --unit={} --no-pager | head -n 300".format(service_name), shell=True).decode("utf8"))
2020-10-08 02:59:44 +00:00
except:
log = "ERROR"
return log