diff --git a/example/zabbix_template.json b/example/zabbix_template.json index 1021f3e..00b65c7 100644 --- a/example/zabbix_template.json +++ b/example/zabbix_template.json @@ -1,48 +1,78 @@ { "zabbix_export": { "version": "6.0", - "date": "2023-04-29T09:18:29Z", + "date": "2024-02-24T09:18:29Z", "groups": [ { - "uuid": "7df96b18c230490a9a0a9e2307226338", - "name": "Templates" + "uuid": "a571c0d144b14fd4a87a9d9b2aa9fcd6", + "name": "Templates/Applications" } ], "templates": [ { "uuid": "937de537759d4357861d1ddb1d9de173", - "template": "Custom - PROM2ZABBIX_TEMPLATE_NAME", - "name": "Custom - PROM2ZABBIX_TEMPLATE_NAME", + "template": "PROM2ZABBIX_TEMPLATE_NAME", + "name": "PROM2ZABBIX_TEMPLATE_NAME", "groups": [ { - "name": "Templates" + "name": "Templates/Applications" } ], "items": [ { "uuid": "bd5b4f1462264d3a95ace4008e09b767", + "name": "raw data", + "type": "HTTP_AGENT", + "key": "prom2zabbix", + "trends": "0", + "status": "DISABLED", + "value_type": "TEXT", + "description": "Prometheus endpoint raw data", + "url": "{$PROM2ZABBIX_ENDPOINT}/metrics", + "tags": [ + { + "tag": "Application", + "value": "PROM2ZABBIX_TAG" + } + ] + }, + { + "uuid": "7eae05fb4e2a4c13a091bd3b84f4e60c", "name": "PROM2ZABBIX_ITEM_NAME", - "type": "EXTERNAL", + "type": "DEPENDENT", "key": "prom2zabbix[PROM2ZABBIX_ITEM_KEY]", "delay": "2m", "history": "7d", "trends": "90d", "status": "DISABLED", "value_type": "FLOAT", - "description": "Template", + "description": "Prometheus item", + "preprocessing": [ + { + "type": "PROMETHEUS_PATTERN", + "parameters": [ + "PROM2ZABBIX_ITEM_KEY", + "value", + "" + ] + } + ], + "master_item": { + "key": "prom2zabbix" + }, "tags": [ { - "tag": "PROM2ZABBIX_TAG", - "value": "prometheus" + "tag": "Application", + "value": "PROM2ZABBIX_TAG" } ] } ], "macros": [ { - "macro": "{$PROM2ZABBIX_MACRO}", - "value": "yolo", - "description": "yolo" + "macro": "{$PROM2ZABBIX_ENDPOINT}", + "value": "http://exporter-endpoint.url:666", + "description": "Prometheus exporter endpoint URL" } ] } diff --git a/prometheus_zabbix_template_generator/parser.py b/prometheus_zabbix_template_generator/parser.py index 891c6cd..3cc3a89 100644 --- a/prometheus_zabbix_template_generator/parser.py +++ b/prometheus_zabbix_template_generator/parser.py @@ -35,10 +35,15 @@ def __str__(self): def _get_item_definition(self, item_template: dict, name: str, template_uuid: str) -> dict: new_item = copy.deepcopy(item_template) - new_item["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_DNS, name + template_uuid)).replace("-", "") + new_item["uuid"] = str(uuid.uuid4()).replace("-", "") new_item["description"] = self.help new_item["key"] = new_item["key"].replace("PROM2ZABBIX_ITEM_KEY", self.label) new_item["name"] = new_item["name"].replace("PROM2ZABBIX_ITEM_NAME", self.label) + if "preprocessing" in new_item: + for i in range(len(new_item["preprocessing"])): + for j in range(len(new_item["preprocessing"][i]["parameters"])): + new_item["preprocessing"][i]["parameters"][j] = \ + new_item["preprocessing"][i]["parameters"][j].replace("PROM2ZABBIX_ITEM_KEY", self.label) return new_item @@ -86,7 +91,7 @@ def parse(self): def generate_template(self, name: str | None): file_name = None if name: - template_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, name)).replace("-", "") + template_uuid = str(uuid.uuid4()).replace("-", "") file_name = f"{name}.json".replace(" ", "_") else: template_uuid = str(uuid.uuid4()).replace("-", "") @@ -101,9 +106,35 @@ def generate_template(self, name: str | None): else: name = "DEFAULT" - item_template = new_template["zabbix_export"]["templates"][0]["items"][0] + master_item = None + for _item in self.example_template["zabbix_export"]["templates"][0]["items"]: + if _item["type"] == "HTTP_AGENT": + master_item = copy.deepcopy(_item) + break + item_template = None + if master_item: + for _item in self.example_template["zabbix_export"]["templates"][0]["items"]: + if _item["type"] == "DEPENDENT": + item_template = copy.deepcopy(_item) + break + else: + item_template = self.example_template["zabbix_export"]["templates"][0]["items"][0] + if item_template is None: + # TODO: proper error handling + print("ERROR: no dependent items in the example template") + exit(1) new_template["zabbix_export"]["templates"][0]["items"] = [] + if master_item: + item = ZabbixItem() + item.label = "" + item.help = master_item["description"] or "" + new_template["zabbix_export"]["templates"][0]["items"].append( + item._get_item_definition( + item_template=master_item, + name=name, + template_uuid=template_uuid) + ) for item in self.collected_items.values(): new_template["zabbix_export"]["templates"][0]["items"].append( item._get_item_definition(