update check service

This commit is contained in:
vincent 2022-02-13 10:36:02 +01:00
parent d10c49d3bd
commit 7fa72f8c19

View File

@ -2,7 +2,8 @@ import subprocess
import time import time
import sys import sys
services = ("syncthing@vincent", services = (
"syncthing@vincent",
"postgresql", "postgresql",
"nginx", "nginx",
"php-fpm", "php-fpm",
@ -17,7 +18,8 @@ services = ("syncthing@vincent",
"radicale", "radicale",
"chisel-server", "chisel-server",
"JDownloader", "JDownloader",
"vaultwarden") "vaultwarden",
)
containers = ("pihole",) containers = ("pihole",)
@ -28,8 +30,7 @@ class Service(object):
def is_active(self): def is_active(self):
cmd = "/usr/bin/systemctl" cmd = "/usr/bin/systemctl"
proc = subprocess.Popen( proc = subprocess.Popen([cmd, "is-active", self.name], stdout=subprocess.PIPE)
[cmd, 'is-active', self.name], stdout=subprocess.PIPE)
proc.communicate() proc.communicate()
rc = proc.returncode rc = proc.returncode
if rc == 0: if rc == 0:
@ -39,9 +40,8 @@ class Service(object):
def get_last_log(self): def get_last_log(self):
cmd = "/usr/bin/journalctl" cmd = "/usr/bin/journalctl"
proc = subprocess.check_output( proc = subprocess.check_output([cmd, "--lines=10", "-q", "-u", self.name])
[cmd, '--lines=10', '-q', '-u', self.name]) return proc.decode("UTF-8").split("\n")
return proc.decode('UTF-8').split('\n')
class Container(object): class Container(object):
@ -51,30 +51,29 @@ class Container(object):
def is_active(self): def is_active(self):
cmd = "/usr/bin/docker" cmd = "/usr/bin/docker"
try: try:
proc = subprocess.check_output( proc = subprocess.check_output([cmd, "ps"], stderr=subprocess.STDOUT)
[cmd, 'ps'], stderr=subprocess.STDOUT)
except: except:
proc = b'' proc = b""
pass pass
proc = proc.decode('UTF-8') proc = proc.decode("UTF-8")
if (proc.find(self.name) != -1): if proc.find(self.name) != -1:
return True return True
else: else:
return False return False
if __name__ == '__main__': if __name__ == "__main__":
wait = True wait = True
if len(sys.argv) > 1: if len(sys.argv) > 1:
if sys.argv[1] == "nowait": if sys.argv[1] == "nowait":
wait = False wait = False
if (wait): if wait:
time.sleep(15) time.sleep(15)
for serviceName in services: for serviceName in services:
serviceObj = Service(serviceName) serviceObj = Service(serviceName)
if (serviceObj.is_active()): if serviceObj.is_active():
print(f"{serviceObj.name} is active") print(f"{serviceObj.name} is active")
else: else:
print(f"{serviceObj.name} is inactive") print(f"{serviceObj.name} is inactive")