add python checkservice

This commit is contained in:
vincent 2020-07-14 16:53:07 +02:00
parent 32c56e3f09
commit e79464f51d

View File

@ -0,0 +1,76 @@
import subprocess
import time
services=("syncthing@vincent",
"mariadb",
"nginx",
"php-fpm",
"gitea",
"tt-rss",
"gunicorn-chainetv.socket",
"gunicorn-supysonic",
"rsyncd.service",
"fail2ban",
"supysonic-daemon",
"home-assistant",
"radicale")
containers=("pihole")
class Service(object):
def __init__(self, serviceName):
self.name=serviceName
def is_active(self):
cmd="/usr/bin/systemctl"
proc=subprocess.Popen([cmd,'is-active',self.name],stdout=subprocess.PIPE)
proc.communicate()
rc=proc.returncode
if rc == 0:
return True
else:
return False
def get_last_log(self):
cmd="/usr/bin/journalctl"
proc=subprocess.check_output([cmd,'--lines=10','-q','-u',self.name])
return proc.decode('UTF-8').split('\n')
class Container(object):
def __init__(self, containerName):
self.name=containerName
def is_active (self):
cmd="/usr/bin/docker"
try:
proc=subprocess.check_output([cmd,'ps'],stderr=subprocess.STDOUT)
except:
proc=""
pass
if (proc.find(self.name) != -1):
return True
else:
return False
if __name__ == '__main__':
time.sleep(60)
for serviceName in services:
serviceObj=Service("serviceName")
if (serviceObj.is_active()):
print (f"{serviceObj.name} is active")
else:
print (f"{serviceObj.name} is inactive")
for line in serviceObj.get_last_log():
print(line)
for containerName in containers:
containerObj=Container(containerName)
if containerObj.is_active():
print (f"{containerObj.name} is active")
else:
print (f"{containerObj.name} is inactive")