13
13
import signal
14
14
import itertools
15
15
import queue
16
- from typing import Dict , List , TYPE_CHECKING , Optional
16
+ from typing import Dict , List , TYPE_CHECKING , Optional , Set
17
17
18
+ from packaging .version import Version
18
19
import psycopg2
19
20
from pydantic import ValidationError
20
21
import pyzabbix
@@ -604,6 +605,9 @@ def __init__(self, name, state, db_uri, settings: models.Settings):
604
605
os .path .join (self .config .map_dir , "siteadmin_hostgroup_map.txt" )
605
606
)
606
607
608
+ ver = self .api .apiinfo .version ()
609
+ self .zabbix_version = Version (ver )
610
+
607
611
def work (self ):
608
612
start_time = time .time ()
609
613
logging .info ("Zabbix update starting" )
@@ -983,8 +987,8 @@ def clear_templates(self, templates, host):
983
987
logging .debug ("DRYRUN: Clearing templates on host: '%s'" , host ["host" ])
984
988
985
989
def set_templates (self , templates , host ):
986
- logging .debug ("Setting templates on host: '%s'" , host ["host" ])
987
990
if not self .config .dryrun :
991
+ logging .debug ("Setting templates on host: '%s'" , host ["host" ])
988
992
try :
989
993
templates = [{"templateid" : template_id } for _ , template_id in templates .items ()]
990
994
self .api .host .update (hostid = host ["hostid" ], templates = templates )
@@ -1064,24 +1068,29 @@ def set_hostgroups(self, hostgroups, host):
1064
1068
else :
1065
1069
logging .debug ("DRYRUN: Setting hostgroups on host: '%s'" , host ["host" ])
1066
1070
1067
- def create_hostgroup (self , hostgroup_name ):
1068
- if not self .config .dryrun :
1069
- logging .debug ("Creating hostgroup: '%s'" , hostgroup_name )
1070
- try :
1071
- result = self .api .hostgroup .create (name = hostgroup_name )
1072
- return result ["groupids" ][0 ]
1073
- except pyzabbix .ZabbixAPIException as e :
1074
- logging .error ("Error when creating hostgroups '%s': %s" , hostgroup_name , e .args )
1075
- else :
1071
+ def create_hostgroup (self , hostgroup_name : str ) -> Optional [str ]:
1072
+ if self .config .dryrun :
1076
1073
logging .debug ("DRYRUN: Creating hostgroup: '%s'" , hostgroup_name )
1077
- return "-1"
1074
+ return None
1075
+
1076
+ logging .debug ("Creating hostgroup: '%s'" , hostgroup_name )
1077
+ try :
1078
+ result = self .api .hostgroup .create (name = hostgroup_name )
1079
+ groupid = result ["groupids" ][0 ]
1080
+ logging .info ("Created host group '%s' (%s)" , hostgroup_name , groupid )
1081
+ return groupid
1082
+ except pyzabbix .ZabbixAPIException as e :
1083
+ logging .error (
1084
+ "Error when creating hostgroups '%s': %s" , hostgroup_name , e .args
1085
+ )
1086
+ return None
1078
1087
1079
1088
def create_extra_hostgroups (
1080
1089
self , existing_hostgroups : List [Dict [str , str ]]
1081
1090
) -> None :
1082
1091
"""Creates additonal host groups based on the prefixes specified
1083
1092
in the config file. These host groups are not assigned hosts by ZAC."""
1084
- hostgroup_names = [ h ["name" ] for h in existing_hostgroups ]
1093
+ hostgroup_names = set ( h ["name" ] for h in existing_hostgroups )
1085
1094
1086
1095
for prefix in self .config .extra_siteadmin_hostgroup_prefixes :
1087
1096
mapping = utils .mapping_values_with_prefix (
@@ -1094,6 +1103,74 @@ def create_extra_hostgroups(
1094
1103
continue
1095
1104
self .create_hostgroup (hostgroup )
1096
1105
1106
+ def create_templategroup (self , templategroup_name : str ) -> Optional [str ]:
1107
+ if self .config .dryrun :
1108
+ logging .debug ("DRYRUN: Creating template group: '%s'" , templategroup_name )
1109
+ return None
1110
+
1111
+ logging .debug ("Creating template group: '%s'" , templategroup_name )
1112
+ try :
1113
+ result = self .api .templategroup .create (name = templategroup_name )
1114
+ groupid = result ["groupids" ][0 ]
1115
+ logging .info ("Created template group '%s' (%s)" , templategroup_name , groupid )
1116
+ return groupid
1117
+ except pyzabbix .ZabbixAPIException as e :
1118
+ logging .error (
1119
+ "Error when creating template group '%s': %s" ,
1120
+ templategroup_name ,
1121
+ e .args ,
1122
+ )
1123
+ return None
1124
+
1125
+ def create_templategroups (self , existing_hostgroups : List [Dict [str , str ]]) -> None :
1126
+ """Creates template groups for each host group in the siteadmin
1127
+ mapping file with the configured template group prefix.
1128
+
1129
+ For Zabbix <6.2, host groups are created instead of template groups."""
1130
+ # Construct a set of all template group names from siteadmin mapping file
1131
+ # by replacing the host group prefix with the template group prefix
1132
+ tgroups = set (
1133
+ utils .with_prefix (tg , self .config .templategroup_prefix )
1134
+ for tg in itertools .chain .from_iterable (
1135
+ self .siteadmin_hostgroup_map .values ()
1136
+ )
1137
+ )
1138
+ if self .zabbix_version .release >= (6 , 2 , 0 ):
1139
+ logging .debug ("Zabbix version is %s. Creating template groups." , self .zabbix_version )
1140
+ self ._create_templategroups (tgroups )
1141
+ else :
1142
+ logging .debug ("Zabbix version is %s. Creating template groups as host groups." , self .zabbix_version )
1143
+ self ._create_templategroups_pre_62_compat (tgroups , existing_hostgroups )
1144
+
1145
+
1146
+ def _create_templategroups (self , tgroups : Set [str ]) -> None :
1147
+ """Create the given template groups if they don't exist.
1148
+
1149
+ Args:
1150
+ tgroups: A set of template group names to create.
1151
+ """
1152
+ res = self .api .templategroup .get (output = ["name" , "groupid" ])
1153
+ existing_tgroups = set (tg ["name" ] for tg in res )
1154
+ for tgroup in tgroups :
1155
+ if tgroup in existing_tgroups :
1156
+ continue
1157
+ self .create_templategroup (tgroup )
1158
+
1159
+ def _create_templategroups_pre_62_compat (self , tgroups : Set [str ], existing_hostgroups : List [Dict [str , str ]]) -> None :
1160
+ """Compatibility method for creating template groups on Zabbix <6.2.
1161
+
1162
+ Because template groups do not exist in <6.2, we instead create
1163
+ host groups with the given names.
1164
+
1165
+ Args:
1166
+ tgroups: A set of template group names to create.
1167
+ """
1168
+ existing_hgroup_names = set (h ["name" ] for h in existing_hostgroups )
1169
+ for tgroup in tgroups :
1170
+ if tgroup in existing_hgroup_names :
1171
+ continue
1172
+ self .create_hostgroup (tgroup )
1173
+
1097
1174
def do_update (self ):
1098
1175
managed_hostgroup_names = set (
1099
1176
itertools .chain .from_iterable (self .property_hostgroup_map .values ())
@@ -1107,6 +1184,10 @@ def do_update(self):
1107
1184
# Create extra host groups if necessary
1108
1185
if self .config .extra_siteadmin_hostgroup_prefixes :
1109
1186
self .create_extra_hostgroups (existing_hostgroups )
1187
+
1188
+ # Create template groups if enabled
1189
+ if self .config .create_templategroups :
1190
+ self .create_templategroups (existing_hostgroups )
1110
1191
1111
1192
zabbix_hostgroups = {}
1112
1193
for zabbix_hostgroup in existing_hostgroups :
0 commit comments