fixed compatibility with newer systemd versions

This commit is contained in:
Kevin 2021-06-28 16:44:26 +02:00
parent 4e45885de3
commit a94e45a7c7
1 changed files with 21 additions and 14 deletions

View File

@ -12,9 +12,15 @@
#
###################################################
from sys import argv, path, stderr, exit
from sys import argv
from sys import path
from sys import stderr
from sys import exit
from subprocess import Popen
from subprocess import PIPE
from subprocess import DEVNULL
from subprocess import getstatusoutput
import psutil
from subprocess import *
global module_list
module_list = []
@ -116,7 +122,7 @@ def service_module(name, value, parent=None):
"value" : value,
"module_parent" : parent,
}]
#print ("service_module END "+str(now(0,1)))
#print ("service_module END "+str(now(0,1)))
return (module)
def get_memcpu (process, servicename):
@ -169,7 +175,7 @@ def win_service(servicelist, option=False, memcpu=False):
if output != None and output["modules"]:
modules += PSCheck.check_service(srvc.strip(), option, memcpu)["modules"]
module_list.append(srvc)
winprocess = output["name"]
#winprocess = output["name"]
#if memcpu == True:
# modules += get_memcpu(winprocess) ## Only available for parent service ATM.
else:
@ -189,18 +195,19 @@ def lnx_service(services_list, memcpu=False):
status = None
if sysctl == 0:
### Systemd available
syscall = Popen(["systemctl", "is-active", srvc], stdout=PIPE,
syscall = Popen(["systemctl", "show", "-pLoadState", "-pActiveState", srvc], stdout=PIPE,
stdin=DEVNULL, universal_newlines=True)
result = syscall.communicate()
result = result[0].strip().lower()
if result == "active":
modules += service_module(srvc, 1)
status = 1
elif result == "inactive":
modules += service_module(srvc, 0)
status = 0
elif result == "unknown":
srvstatus= result[0].strip().lower().split("\n")
if srvstatus[0] == "loadstate=not-found":
next
else:
if srvstatus[1] == "activestate=active":
modules += service_module(srvc, 1)
status = 1
elif srvstatus[1] == "activestate=inactive":
modules += service_module(srvc, 0)
status = 0
elif sysctl != 0 and servic == 0:
### Systemd not available, switch to service command
syscall = Popen(["service", srvc, "status"], stdout=PIPE,
@ -382,7 +389,7 @@ def discover(osyst, servicelist):
print ("--help")
print ("\tPrints this help screen")
print ("--default")
print ("\tRuns this tool with default monitoring.".format(argv[0]))
print ("\tRuns this tool with default monitoring.")
print ("\tServices monitored by default for {}:".format(osyst))
print ("\t",", ".join(servicelist))
print ("--list \"<srvc1,srvc2,srvc3>\"")