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
|
|
|
|
2021-04-11 00:51:26 +00:00
|
|
|
service_enabled_cache = {}
|
|
|
|
|
2020-10-08 02:59:44 +00:00
|
|
|
#==================================
|
|
|
|
# Service Status, Enabled, Logs, etc...
|
|
|
|
#==================================
|
2021-04-11 00:51:26 +00:00
|
|
|
def clear_service_enabled_cache():
|
|
|
|
global service_enabled_cache
|
|
|
|
service_enabled_cache = {}
|
|
|
|
|
2022-02-25 18:54:42 +00:00
|
|
|
def is_service_enabled(service_name, force_refresh=False):
|
2021-04-11 00:51:26 +00:00
|
|
|
global service_enabled_cache
|
|
|
|
|
2022-02-25 18:54:42 +00:00
|
|
|
if service_name in service_enabled_cache and force_refresh == False:
|
2021-04-11 00:51:26 +00:00
|
|
|
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))
|
2021-04-11 00:51:26 +00:00
|
|
|
if code == 0:
|
|
|
|
service_enabled_cache[service_name] = True
|
2020-10-08 02:59:44 +00:00
|
|
|
return True
|
2021-04-11 00:51:26 +00:00
|
|
|
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
|