82 lines
3.1 KiB
Python
82 lines
3.1 KiB
Python
import yaml
|
|
import uuid
|
|
import sys
|
|
|
|
def extract_nginx_config(input_file, output_file):
|
|
print(f"Reading {input_file}...")
|
|
with open(input_file, 'r', encoding='utf-8') as f:
|
|
data = yaml.safe_load(f)
|
|
|
|
if 'zabbix_export' not in data or 'hosts' not in data['zabbix_export']:
|
|
print("No hosts found in export file (checked under zabbix_export['hosts']).")
|
|
return
|
|
|
|
# We assume we are extracting from the first host or all hosts,
|
|
# but usually an export has one specific host of interest.
|
|
# The user mentioned "srvproxy001".
|
|
|
|
extracted_discovery_rules = []
|
|
extracted_items = []
|
|
|
|
for host in data['zabbix_export']['hosts']:
|
|
print(f"Processing host: {host['host']}")
|
|
|
|
# Extract Discovery Rules related to Nginx
|
|
if 'discovery_rules' in host:
|
|
for dr in host['discovery_rules']:
|
|
# Filter by name or key containing 'nginx'
|
|
if 'nginx' in dr['key'].lower() or 'nginx' in dr['name'].lower() or 'insight' in dr['name'].lower():
|
|
print(f" Found Discovery Rule: {dr['name']} ({dr['key']})")
|
|
extracted_discovery_rules.append(dr)
|
|
|
|
# Extract Items related to Nginx (directly attached to host, not in LLD)
|
|
if 'items' in host:
|
|
for item in host['items']:
|
|
if 'nginx' in item['key'].lower() or 'nginx' in item['name'].lower():
|
|
print(f" Found Item: {item['name']} ({item['key']})")
|
|
extracted_items.append(item)
|
|
|
|
if not extracted_discovery_rules and not extracted_items:
|
|
print("No Nginx configurations found.")
|
|
return
|
|
|
|
# Create Template Structure
|
|
template_uuid = str(uuid.uuid4())
|
|
group_uuid = "7df96b18c230490a9a0a9e2307226338" # Generic uuid for Templates group
|
|
|
|
template_export = {
|
|
'zabbix_export': {
|
|
'version': data['zabbix_export']['version'],
|
|
'template_groups': [
|
|
{
|
|
'uuid': group_uuid,
|
|
'name': 'Templates/Applications'
|
|
}
|
|
],
|
|
'templates': [
|
|
{
|
|
'uuid': template_uuid,
|
|
'template': 'Template Nginx Custom Insight',
|
|
'name': 'Template Nginx Custom Insight',
|
|
'description': 'Template extracted from host configuration containing custom Nginx Insight rules.',
|
|
'groups': [
|
|
{'name': 'Templates/Applications'}
|
|
],
|
|
'items': extracted_items,
|
|
'discovery_rules': extracted_discovery_rules
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
print(f"Writing to {output_file}...")
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
yaml.dump(template_export, f, sort_keys=False, allow_unicode=True)
|
|
print("Done.")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 3:
|
|
print("Usage: python extract_nginx_config.py <input_yaml> <output_yaml>")
|
|
else:
|
|
extract_nginx_config(sys.argv[1], sys.argv[2])
|