67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
import yaml
|
|
import uuid
|
|
import sys
|
|
|
|
def fix_uuids(file_path, target_group_uuid):
|
|
print(f"Processing {file_path}...")
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
data = yaml.safe_load(f)
|
|
|
|
# 1. Fix Group UUID
|
|
if 'template_groups' in data:
|
|
for group in data['template_groups']:
|
|
if group['name'] == 'Templates/Applications':
|
|
print(f"Updating Group UUID for {group['name']} to {target_group_uuid}")
|
|
group['uuid'] = target_group_uuid
|
|
|
|
# 2. Regenerate all other UUIDs and clean tags
|
|
count = 0
|
|
generated_uuids = set()
|
|
|
|
def regenerate(node):
|
|
nonlocal count
|
|
if isinstance(node, dict):
|
|
# Clean unsupported tags
|
|
if 'wizard_ready' in node:
|
|
del node['wizard_ready']
|
|
if 'readme' in node:
|
|
del node['readme']
|
|
if 'config' in node: # Often found in macros in Zabbix 8.0
|
|
del node['config']
|
|
|
|
if 'uuid' in node:
|
|
# Skip if it's the group UUID we just fixed manually
|
|
if node['uuid'] == target_group_uuid:
|
|
pass
|
|
else:
|
|
new_uuid = uuid.uuid4().hex
|
|
# Ensure uniqueness (paranoid check)
|
|
while new_uuid in generated_uuids:
|
|
new_uuid = uuid.uuid4().hex
|
|
|
|
node['uuid'] = new_uuid
|
|
generated_uuids.add(new_uuid)
|
|
count += 1
|
|
|
|
for k, v in list(node.items()): # Use list() to avoid runtime error during iteration if keys are deleted
|
|
regenerate(v)
|
|
elif isinstance(node, list):
|
|
for item in node:
|
|
regenerate(item)
|
|
|
|
regenerate(data)
|
|
print(f"Regenerated {count} UUIDs and cleaned tags.")
|
|
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
yaml.dump(data, f, sort_keys=False, indent=2, width=float("inf"), allow_unicode=True)
|
|
|
|
if __name__ == "__main__":
|
|
import argparse
|
|
parser = argparse.ArgumentParser(description="Fix UUIDs in Zabbix Template")
|
|
parser.add_argument("file", help="Path to the YAML template file")
|
|
parser.add_argument("--group-uuid", default="a571c0d144b14fd4a87a9d9b2aa9fcd6", help="Target Group UUID")
|
|
|
|
args = parser.parse_args()
|
|
fix_uuids(args.file, args.group_uuid)
|