Skip to content

Commit

Permalink
Add Zabbix <6.4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan committed Nov 3, 2023
1 parent f980647 commit 394c8f3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
3 changes: 3 additions & 0 deletions zabbix_auto_config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ class ZabbixVersion(NamedTuple):
minor: int
patch: int

def __str__(self) -> str:
return f"{self.major}.{self.minor}.{self.patch}"

@classmethod
def from_version_string(cls, version_string: str) -> ZabbixVersion:
"""Constructs a ZabbixVersion from a semantic version string from the API.
Expand Down
61 changes: 39 additions & 22 deletions zabbix_auto_config/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,28 +1027,44 @@ def create_templategroup(self, templategroup_name: str) -> Optional[str]:
)
return None

def create_templategroups(self) -> None:
""">=6.4 ONLY: Creates template groups for each host group in
the mapping file."""
if not self.config.create_templategroups:
logging.debug("Skipping template group creation. Feature is disabled.")
return
elif self.zabbix_version < (6, 4, 0):
logging.info("Skipping template group creation. Feature requires Zabbix >= 6.4.0.")
return
def create_templategroups(self, managed_hostgroup_names: Set[str], existing_hostgroups: List[Dict[str, str]]) -> None:
"""Creates template groups for each managed host group.
For Zabbix <6.4, host groups with the configured template group prefix
are created instead."""
tgroups = set(utils.with_prefix(hg, self.config.templategroup_prefix) for hg in managed_hostgroup_names)
if self.zabbix_version >= (6, 4, 0):
logging.debug("Zabbix version is %s. Creating template groups.", self.zabbix_version)
self._create_templategroups(tgroups)
else:
logging.debug("Zabbix version is %s. Creating template groups as host groups.", self.zabbix_version)
self._create_templategroups_pre_64_compat(tgroups, existing_hostgroups)

tgroups = self.api.templategroup.get(output=["name", "groupid"])
templategroup_names = set(h["name"] for h in tgroups)

def _create_templategroups(self, tgroups: Set[str]) -> None:
"""Zabbix >=6.4 template group creation method."""
res = self.api.templategroup.get(output=["name", "groupid"])
existing_tgroups = set(tg["name"] for tg in res)

# for templategroups in mapping.values():
for tgroup in tgroups:
if tgroup in existing_tgroups:
continue
self.create_templategroup(tgroup)

mapping = utils.mapping_values_with_prefix(
self.siteadmin_hostgroup_map,
prefix=self.config.templategroup_prefix,
)
for templategroups in mapping.values():
for templategroup in templategroups:
if templategroup in templategroup_names:
continue
self.create_templategroup(templategroup)
def _create_templategroups_pre_64_compat(self, tgroups: Set[str], existing_hostgroups: List[Dict[str, str]]) -> None:
"""Zabbix <6.4 template group compatibility fallback method.
Template groups don't exist in Zabbix <6.4, so we create host
groups to fulfill the same purpose.
Creates template host groups for all groups in the siteadmin
mapping file with the configured template group prefix."""
existing_hgroup_names = set(h["name"] for h in existing_hostgroups)

for tgroup in tgroups:
if tgroup in existing_hgroup_names:
continue
self.create_hostgroup(tgroup)

def do_update(self):
managed_hostgroup_names = set(
Expand All @@ -1064,8 +1080,9 @@ def do_update(self):
if self.config.extra_siteadmin_hostgroup_prefixes:
self.create_extra_hostgroups(existing_hostgroups)

# Create template groups if necessary
self.create_templategroups()
# Create template groups if enabled
if self.config.create_templategroups:
self.create_templategroups(managed_hostgroup_names, existing_hostgroups)

zabbix_hostgroups = {}
for zabbix_hostgroup in existing_hostgroups:
Expand Down

0 comments on commit 394c8f3

Please sign in to comment.