From a7c4bb1daae4af2282e544e29ae20ea02bafa42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Dallidet?= Date: Fri, 17 May 2024 10:16:13 +0200 Subject: [PATCH] Fixes #24873: Python lint error in the zabbix plugin --- zabbix/bin/zabbix-plugin.py | 121 ++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 62 deletions(-) diff --git a/zabbix/bin/zabbix-plugin.py b/zabbix/bin/zabbix-plugin.py index ba57afe00..3030cdea1 100755 --- a/zabbix/bin/zabbix-plugin.py +++ b/zabbix/bin/zabbix-plugin.py @@ -54,13 +54,13 @@ def addZabbixHost(zapi, node): if validateIPAddr(ip): nodeip = ip break - zapi.host.create(host=node["hostname"], groups=[{"groupid":zabbixGroupID(zapi, "Rudder nodes")}], interfaces=[{"type":"1", "main":"1", "useip":"1", "ip":nodeip, "dns":"", "port":"10050"}],description=node["id"]) + zapi.host.create(host=node["hostname"], groups=[{"groupid":zabbixGroupID(zapi, "Rudder nodes")}], interfaces=[{"type":"1", "main":"1", "useip":"1", "ip":nodeip, "dns":"", "port":"10050"}],description=node["id"]) def update(conf, register, zapi): zhosts = zapi.host.get() zmacros = zapi.do_request("usermacro.get", params=["selectHosts"]) rnodes = getRequestToRudderAPI(conf, "/nodes")["data"]["nodes"] - + # Add all nodes from Rudder to Zabbix and to the register upon addition for node in rnodes: if node["hostname"] not in (h["host"] for h in zhosts): @@ -76,7 +76,7 @@ def update(conf, register, zapi): zapi.host.delete(zabbixHostID(host["host"], zhosts)) register.remove_section(host["host"]) - + # The ConfigParser lowercases all keys and section names, and Zabbix uppercases macro keys, hence this overriding to keep things nice class MyConfigParser(configparser.ConfigParser): optionxform = lambda self, optionstr : optionstr @@ -91,13 +91,13 @@ class MyConfigParser(configparser.ConfigParser): register = MyConfigParser() register.read(registerFile) urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) - + try: zapi = ZabbixAPI(conf['ZABBIX']['ZabbixWebserviceURL']) zapi.login(conf['ZABBIX']['username'], conf['ZABBIX']['password']) - print("Connected to Zabbix API Version %s" % zapi.api_version()) + print("Connected to Zabbix API Version %s" % zapi.api_version()) ztemplates = zapi.template.get() - + except: print(traceback.print_exc()) print("[!] Unable to connect to the Zabbix API. Check your zabbix.conf") @@ -111,10 +111,10 @@ class MyConfigParser(configparser.ConfigParser): # adding new rudder node in zabbix hosts elif args["hook"]: - if args["addHost"]: + if args["addHost"]: node = getRequestToRudderAPI(conf, "/nodes/" + args[""])["data"]["nodes"][0] zhosts = zapi.host.get() - if node["hostname"] not in (h["host"] for h in zhosts): + if node["hostname"] not in (h["host"] for h in zhosts): addZabbixHost(zapi, node) print("[ ] Adding node " + node["hostname"] +" to Zabbix...") if not register.has_section(node["hostname"]): @@ -124,54 +124,51 @@ class MyConfigParser(configparser.ConfigParser): elif args["rmHost"]: zhosts = zapi.host.get() rnodes = getRequestToRudderAPI(conf, "/nodes")["data"]["nodes"] - for host in zapi.host.get(output="extend"): + for host in zapi.host.get(output="extend"): if host['host'] not in (node["hostname"] for node in rnodes): zapi.host.delete(zabbixHostID(host["host"], zhosts)) - print("[ ] Removing node " + host["host"] + " from Zabbix...") + print("[ ] Removing node " + host["host"] + " from Zabbix...") if register.has_section(host["host"]): register.remove_section(host["host"]) - # apply-configuration ( hosts - Templates - Macros) + # apply-configuration ( hosts - Templates - Macros) elif args["apply-configuration"]: rootPath = '/var/rudder/shared-files/root/files' zabbixCsv = '/var/rudder/shared-files' zhosts = zapi.host.get() rnodes = getRequestToRudderAPI(conf, "/nodes")["data"]["nodes"] ztemplates = zapi.template.get() - + # save all rudder nodes related templates to a csv file " /var/rudder/shared-files/rudderMonitor.csv " os.chdir('/var/rudder/shared-files') with open('rudderMonitor.csv', 'wb') as myfile: - if os.path.exists('rudderMonitor.csv'): - c = csv.writer(open("rudderMonitor.csv", "wb")) - else: - print('[!] No rudder monitoring config file, considering it empty...') - - for host in zapi.host.get(output="extend"): - if host['host'] in (node["hostname"] for node in rnodes): - rtemplates = zapi.do_request("host.get", params={"output":"extend", "selectParentTemplates": ["templateid", "name"], "hostids": host['hostid'] }) - rmacros = zapi.do_request("host.get", params={"output":"extend", "selectMacros": ["hostmacroid", "macro", "value"], "hostids": host['hostid'] }) - ztemplates = zapi.template.get() - - for result in rtemplates["result"]: - for parentTemplate in result['parentTemplates']: - c.writerow([host['host'],parentTemplate["name"]]) - if parentTemplate["name"] in (template["name"] for template in ztemplates): - data = zapi.do_request("template.get",params={"output":"extend", "filter": { "host": parentTemplate["name"] }}) - - for result in rmacros['result']: - for m in result['macros']: - if result['host'] in (node["hostname"] for node in rnodes): - c.writerow([result['host'],m['macro']]) - else: - print('[!] Node ' + result['host'] + ' does not exist among rudder nodes') + monitorwriter = csv.writer(open("rudderMonitor.csv", "wb")) + + for host in zapi.host.get(output="extend"): + if host['host'] in (node["hostname"] for node in rnodes): + rtemplates = zapi.do_request("host.get", params={"output":"extend", "selectParentTemplates": ["templateid", "name"], "hostids": host['hostid'] }) + rmacros = zapi.do_request("host.get", params={"output":"extend", "selectMacros": ["hostmacroid", "macro", "value"], "hostids": host['hostid'] }) + ztemplates = zapi.template.get() + + for result in rtemplates["result"]: + for parentTemplate in result['parentTemplates']: + monitorwriter.writerow([host['host'],parentTemplate["name"]]) + if parentTemplate["name"] in (template["name"] for template in ztemplates): + data = zapi.do_request("template.get",params={"output":"extend", "filter": { "host": parentTemplate["name"] }}) + + for result in rmacros['result']: + for m in result['macros']: + if result['host'] in (node["hostname"] for node in rnodes): + monitorwriter.writerow([result['host'],m['macro']]) + else: + print('[!] Node ' + result['host'] + ' does not exist among rudder nodes') if os.path.exists(rootPath): for name in os.listdir(rootPath): if name in (node["id"] for node in rnodes): print('[!] ' + name + ' is an id for Rudder node applying configuration ... ') - + for dir in next(os.walk(rootPath))[1]: csvfile = rootPath + '/' + dir + '/rudder_monitoring.csv' if os.path.exists(csvfile): @@ -181,7 +178,7 @@ class MyConfigParser(configparser.ConfigParser): confcsv = csv.reader(fd) conversionID = getRequestToRudderAPI(conf, "/nodes")["data"]["nodes"] if name in (node["id"] for node in conversionID): - node = getRequestToRudderAPI(conf, "/nodes/" + name) + node = getRequestToRudderAPI(conf, "/nodes/" + name) for data in node["data"]["nodes"]: hostname = data["hostname"] if hostname in (h["host"] for h in zhosts): @@ -189,8 +186,8 @@ class MyConfigParser(configparser.ConfigParser): for result in host["result"]: hostid = result["hostid"] print('-------------------------------------------') - - + + print('[ ] Applying conf to node ' + hostname + '...') rudderListTmp = [] @@ -198,13 +195,13 @@ class MyConfigParser(configparser.ConfigParser): rudderListTmpMacros = [] # In RUDDER, we can edit technique editors, assign them to a node via rule and eventually monitor these directives by zabbix. - + for row in confcsv: # Templates if (row[0] == 'template'): template = zapi.do_request("template.get",params={"output":"extend", "filter": { "host": [row[1]]}}) rudderListTmp.append(row[1]) - + # When monitoring template is added to the node, we link this zabbix template to the host zabbix. for result in rtemplates['result']: if not any(parentTemplate["name"] == row[1] for parentTemplate in result['parentTemplates']): @@ -212,13 +209,13 @@ class MyConfigParser(configparser.ConfigParser): templateidADD = result["templateid"] linkTemplate = zapi.do_request("template.massadd", params={"output":"extend", "templates": [{ "templateid": templateidADD }], "hosts":[{"hostid": hostid }]}) print('[ ] Applying conf : the host ' + hostid + ' is linked to the template ' + templateidADD + ' ...') - + # Macros if (row[0] == 'param'): rudderListTmp.append(row[1]) rudderListTmpMacros=rudderListTmp rudderListTmpMacros[-1] = "{$"+rudderListTmpMacros[-1].upper()+"}" - + # When monitoring parameter is added to the node, we create a host macro in zabbix for result in rmacros['result']: if not any(m['macro'] == row[1] for m in result['macros']): @@ -227,39 +224,39 @@ class MyConfigParser(configparser.ConfigParser): print('[ ] Applying conf : the host ' + hostid + ' has a new parameter ' + "{$" + row[1] + "}" + ' ...') except: print('[ ] Macro ' + row[1] + ' already added to node ' + result['host'] + ' ...') - + for result in rtemplates["result"]: for parentTemplate in result['parentTemplates']: nameT = parentTemplate["name"] - + for result in rmacros["result"]: for m in result['macros']: nameM = m['macro'] - - - zabbixListTmp.append(nameT) + + + zabbixListTmp.append(nameT) zabbixListTmp.append(nameM) - - - + + + print("--------------------------------") resTemplates = [item for item in zabbixListTmp if not item in rudderListTmp] resMacros = [item for item in zabbixListTmp if not item in rudderListTmpMacros] template = zapi.do_request("template.get",params={"output":"extend", "filter": { "host": resTemplates }}) - - - # When a monitoring template is removed from rudder node, we unlike this template from zabbix host + + + # When a monitoring template is removed from rudder node, we unlike this template from zabbix host listnames = [] for result in template["result"]: templateidRM = result["templateid"] if(resTemplates): - unlinkTemplate = zapi.do_request("template.massremove", params={"output":"extend", "templateids": templateidRM , "hostids": hostid }) + unlinkTemplate = zapi.do_request("template.massremove", params={"output":"extend", "templateids": templateidRM , "hostids": hostid }) print('[ ] Applying conf: the host ' + hostid + ' is unlinked to the template ' + templateidRM + ' ...') - - # When a monitoring parameter is removed from rudder node, we remove macro from zabbix host + + # When a monitoring parameter is removed from rudder node, we remove macro from zabbix host for result in rmacros['result']: for m in result['macros']: - listnames.append(m) + listnames.append(m) for line in listnames: for item in resMacros: if (line['macro']==item): @@ -267,11 +264,11 @@ class MyConfigParser(configparser.ConfigParser): if(resMacros): deleteUserMacros = zapi.do_request("usermacro.delete", params=[ macroid ]) print('[ ] Applying conf : Macro ' + item + ' is removed to the host ' + result['host'] + ' ...' ) - - print("[+] Done.") + + print("[+] Done.") else: print('[!] Node ' + name + ' has no rudder monitoring config file, considering it empty...') - -register.write(open(registerFile, "w")) + +register.write(open(registerFile, "w"))