Working usermacros based on config context

This commit is contained in:
Raymond Kuiper
2025-02-14 08:28:10 +01:00
parent cebefd681e
commit 6d4e250b23
2 changed files with 74 additions and 17 deletions

View File

@@ -2,11 +2,11 @@
"""
All of the Zabbix Usermacro related configuration
"""
from re import match
from logging import getLogger
from zabbix_utils import APIRequestError
from modules.exceptions import UsermacroError
from pprint import pprint
try:
@@ -37,11 +37,54 @@ class ZabbixUsermacros():
return self.__repr__()
def _setConfig(self):
if str(usermacro_sync) == "full":
if str(usermacro_sync).lower() == "full":
self.sync = True
self.force_sync = True
elif usermacro_sync:
self.sync = True
return True
def validate_macro(self, macro_name):
pattern = '\{\$[A-Z0-9\._]*(\:.*)?\}'
return match(pattern, macro_name)
def render_macro(self, macro_name, macro_properties):
macro={}
macrotypes={'text': 0, 'secret': 1, 'vault': 2}
if self.validate_macro(macro_name):
macro['macro'] = str(macro_name)
if isinstance(macro_properties, dict):
if not 'value' in macro_properties:
self.logger.error(f'Usermacro {macro_name} has no value, skipping.')
return False
else:
macro['value'] = macro_properties['value']
if 'type' in macro_properties and macro_properties['type'].lower() in macrotypes:
macro['type'] = str(macrotypes[macro_properties['type']])
else:
macro['type'] = str(0)
if 'description' in macro_properties and isinstance(macro_properties['description'], str):
macro['description'] = macro_properties['description']
else:
macro['description'] = ""
elif isinstance(macro_properties, str):
macro['value'] = macro_properties
macro['type'] = str(0)
macro['description'] = ""
else:
self.logger.error(f'Usermacro {macro_name} is not a valid usermacro name, skipping.')
return False
return macro
def generate(self):
macros=[]
if "zabbix" in self.context and "usermacros" in self.context['zabbix']:
for macro, properties in self.context['zabbix']['usermacros'].items():
m = self.render_macro(macro, properties)
pprint(m)
if m:
macros.append(m)
return macros