From 2db685da759db4525d5762b978ce786110cfea95 Mon Sep 17 00:00:00 2001 From: pederhan Date: Fri, 23 Aug 2024 11:07:54 +0200 Subject: [PATCH] Add configurable group prefix separator --- CHANGELOG.md | 3 +++ config.sample.toml | 5 +++++ zabbix_auto_config/models.py | 2 ++ zabbix_auto_config/processing.py | 12 ++++++++++-- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c45892..8a7d1ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Zabbix 7 compatibility +- Configuration option for setting group prefix separator. + - `[zabbix]` + - `prefix_separator`: Separator for group prefixes. Default is `-`. - Configuration options for each process. - `[zac.process.garbage_collector]` - `enabled`: Enable automatic garbage collection. diff --git a/config.sample.toml b/config.sample.toml index 1cb8b28..9965d96 100644 --- a/config.sample.toml +++ b/config.sample.toml @@ -80,10 +80,15 @@ hostgroup_source_prefix = "Source-" hostgroup_importance_prefix = "Importance-" # Template group creation +# If we have a host group named `Siteadmin-my-hosts`, ZAC creates a +# template group named `Templates-my-hosts` # NOTE: will create host groups if enabled on Zabbix <6.2 create_templategroups = true templategroup_prefix = "Templates-" +# Separator used for group name prefixes +prefix_separator = "-" + extra_siteadmin_hostgroup_prefixes = [] [source_collectors.mysource] diff --git a/zabbix_auto_config/models.py b/zabbix_auto_config/models.py index 76f931a..f19a89c 100644 --- a/zabbix_auto_config/models.py +++ b/zabbix_auto_config/models.py @@ -84,6 +84,8 @@ class ZabbixSettings(ConfigBaseModel): # These groups are not managed by ZAC beyond their creation. extra_siteadmin_hostgroup_prefixes: Set[str] = set() + prefix_separator: str = "-" + @field_validator("timeout") @classmethod def _validate_timeout(cls, v: Optional[int]) -> Optional[int]: diff --git a/zabbix_auto_config/processing.py b/zabbix_auto_config/processing.py index aaded8e..b9f3698 100644 --- a/zabbix_auto_config/processing.py +++ b/zabbix_auto_config/processing.py @@ -1627,6 +1627,7 @@ def create_extra_hostgroups(self, existing_hostgroups: List[HostGroup]) -> None: mapping = utils.mapping_values_with_prefix( self.siteadmin_hostgroup_map, # this is copied in the function prefix=prefix, + separator=self.config.prefix_separator, ) for hostgroups in mapping.values(): for hostgroup in hostgroups: @@ -1658,9 +1659,16 @@ def create_templategroups(self, existing_hostgroups: List[HostGroup]) -> None: For Zabbix <6.2, host groups are created instead of template groups.""" # Construct a set of all template group names from siteadmin mapping file - # by replacing the host group prefix with the template group prefix + # by replacing the host group prefix with the template group prefix. + # The prefix is determined by the separator defined in the config file. + # If we use the template group prefix `Templates-`, we go from + # `Siteadmin-bob-hosts` to `Templates-bob-hosts`. tgroups = set( - utils.with_prefix(tg, self.config.templategroup_prefix) + utils.with_prefix( + tg, + self.config.templategroup_prefix, + separator=self.config.prefix_separator, + ) for tg in itertools.chain.from_iterable( self.siteadmin_hostgroup_map.values() )