# -*- coding: utf-8 -*- """Shared helper for the L2 collectors: read the module config, resolve the selected templates to template ids, and list the hosts that should be collected. """ import os import json DEFAULT_PATHS = [ os.environ.get('L2_CONFIG') or '', '/opt/zabbix/zabbix-server/web-modules/WecomCenter/assets/l2topology/config.json', '/usr/share/zabbix/modules/WecomCenter/assets/l2topology/config.json', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.json'), ] def config(): for p in DEFAULT_PATHS: if p and os.path.isfile(p): try: with open(p, encoding='utf-8') as f: return json.load(f) except Exception: pass return {} def template_ids(call): """Resolve configured template names -> [templateid]. Empty list => no filter.""" cfg = config() names = [str(n).strip() for n in (cfg.get('templates') or []) if str(n).strip()] if not names: return [] try: tpls = call('template.get', {'output': ['templateid', 'name']})['result'] except Exception: return [] want = {n.lower() for n in names} return [t['templateid'] for t in tpls if t.get('name', '').strip().lower() in want] def hosts(call, output=None, interfaces='extend'): """host.get restricted to the configured templates (when resolvable).""" p = {'output': output or ['hostid', 'name'], 'selectInterfaces': interfaces} tids = template_ids(call) if tids: p['templateids'] = tids return call('host.get', p)['result']