From a94e45a7c7cf75e1f157bb4e276592fbc2dec9de Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 28 Jun 2021 16:44:26 +0200 Subject: [PATCH] fixed compatibility with newer systemd versions --- pandora_agents/unix/plugins/autodiscover | 35 ++++++++++++++---------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/pandora_agents/unix/plugins/autodiscover b/pandora_agents/unix/plugins/autodiscover index 78a70cddba..93c579b085 100644 --- a/pandora_agents/unix/plugins/autodiscover +++ b/pandora_agents/unix/plugins/autodiscover @@ -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 \"\"")