change: added legacy scripts for working with 5.0 and lower
This commit is contained in:
127
config-mover/config_exporter_legacy.py
Normal file
127
config-mover/config_exporter_legacy.py
Normal file
@@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Legacy Zabbix Configuration Exporter
|
||||
====================================
|
||||
Uses username/password authentication instead of Bearer tokens.
|
||||
Note: This script is designed for Zabbix 5.0 and older versions!
|
||||
Please do not use with Zabbix 6.0 and newer! Use token-based method instead.
|
||||
"""
|
||||
|
||||
import os
|
||||
import xml.etree.ElementTree as ET
|
||||
from zabbix_utils import ZabbixAPI
|
||||
|
||||
# Configuration from environment variables
|
||||
ZABBIX_URL = os.environ.get("ZABBIX_URL")
|
||||
ZABBIX_USER = os.environ.get("ZABBIX_USER")
|
||||
ZABBIX_PASSWORD = os.environ.get("ZABBIX_PASSWORD")
|
||||
HOST_IDS = os.environ.get("HOST_IDS")
|
||||
OUTPUT_DIR = os.environ.get("OUTPUT_DIR", "/opt/python/export")
|
||||
|
||||
|
||||
def get_template_names(xml_data):
|
||||
"""Extract template names from host XML."""
|
||||
try:
|
||||
root = ET.fromstring(xml_data)
|
||||
return [name.text for name in root.findall('.//hosts/host/templates/template/name')]
|
||||
except ET.ParseError:
|
||||
return []
|
||||
|
||||
|
||||
def export_templates(zapi, template_names, output_dir):
|
||||
"""Export templates to XML files."""
|
||||
if not template_names:
|
||||
return
|
||||
|
||||
templates = zapi.template.get(output=['templateid', 'host'], filter={'host': template_names})
|
||||
|
||||
for template in templates:
|
||||
name = template['host']
|
||||
template_id = template['templateid']
|
||||
|
||||
xml_data = zapi.configuration.export(options={'templates': [template_id]}, format='xml')
|
||||
|
||||
if xml_data:
|
||||
safe_name = "".join(c for c in name if c.isalnum() or c in (' ', '_', '-')).strip()
|
||||
filename = f"template_{safe_name}.xml"
|
||||
filepath = os.path.join(output_dir, filename)
|
||||
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
f.write(xml_data)
|
||||
|
||||
|
||||
def export_host(zapi, host_id, base_dir):
|
||||
"""Export single host and its templates."""
|
||||
host_dir = os.path.join(base_dir, str(host_id))
|
||||
os.makedirs(host_dir, exist_ok=True)
|
||||
|
||||
# Export host
|
||||
host_xml = zapi.configuration.export(options={'hosts': [host_id]}, format='xml')
|
||||
if not host_xml:
|
||||
return False
|
||||
|
||||
# Save host XML
|
||||
host_file = os.path.join(host_dir, f"host_{host_id}.xml")
|
||||
with open(host_file, 'w', encoding='utf-8') as f:
|
||||
f.write(host_xml)
|
||||
|
||||
# Export templates
|
||||
template_names = get_template_names(host_xml)
|
||||
if template_names:
|
||||
export_templates(zapi, template_names, host_dir)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
# Check required environment variables
|
||||
if not ZABBIX_USER or not ZABBIX_PASSWORD or not HOST_IDS:
|
||||
print("Error: ZABBIX_USER, ZABBIX_PASSWORD, and HOST_IDS must be set")
|
||||
return
|
||||
|
||||
host_ids = [h.strip() for h in HOST_IDS.split(',') if h.strip()]
|
||||
if not host_ids:
|
||||
print("Error: No valid HOST_IDS provided")
|
||||
return
|
||||
|
||||
# Connect to Zabbix
|
||||
try:
|
||||
zapi = ZabbixAPI(url=ZABBIX_URL)
|
||||
zapi.login(user=ZABBIX_USER, password=ZABBIX_PASSWORD)
|
||||
print(f"Connected to Zabbix. Processing {len(host_ids)} hosts...")
|
||||
except Exception as e:
|
||||
print(f"Failed to connect: {e}")
|
||||
return
|
||||
|
||||
# Create output directory
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
|
||||
# Export hosts
|
||||
successful = 0
|
||||
failed = 0
|
||||
|
||||
for i, host_id in enumerate(host_ids, 1):
|
||||
try:
|
||||
if export_host(zapi, host_id, OUTPUT_DIR):
|
||||
successful += 1
|
||||
else:
|
||||
failed += 1
|
||||
except Exception:
|
||||
failed += 1
|
||||
|
||||
# Progress indicator for large batches
|
||||
if i % 50 == 0 or i == len(host_ids):
|
||||
print(f"Progress: {i}/{len(host_ids)} ({successful} ok, {failed} failed)")
|
||||
|
||||
print(f"Export complete: {successful} successful, {failed} failed")
|
||||
print(f"Results in: {OUTPUT_DIR}")
|
||||
|
||||
# Logout
|
||||
try:
|
||||
zapi.logout()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
72
config-mover/get_host_ids_legacy.py
Normal file
72
config-mover/get_host_ids_legacy.py
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Legacy ID Retriever
|
||||
===============================
|
||||
Uses username/password authentication instead of tokens.
|
||||
Note: This script is designed for Zabbix 5.0 and older versions!
|
||||
Please do not use with Zabbix 6.0 and newer! Use token-based get_host_ids.py instead.
|
||||
"""
|
||||
|
||||
import os
|
||||
from datetime import datetime
|
||||
from zabbix_utils import ZabbixAPI
|
||||
|
||||
# Configuration from environment variables
|
||||
ZABBIX_URL = os.environ.get("ZABBIX_URL", "http://localhost/api_jsonrpc.php")
|
||||
ZABBIX_USER = os.environ.get("ZABBIX_USER")
|
||||
ZABBIX_PASSWORD = os.environ.get("ZABBIX_PASSWORD")
|
||||
|
||||
|
||||
def main():
|
||||
# Check required environment variables
|
||||
if not ZABBIX_USER or not ZABBIX_PASSWORD:
|
||||
print("Error: ZABBIX_USER and ZABBIX_PASSWORD environment variables must be set")
|
||||
return
|
||||
|
||||
# Connect to Zabbix using username/password
|
||||
try:
|
||||
zapi = ZabbixAPI(url=ZABBIX_URL)
|
||||
zapi.login(user=ZABBIX_USER, password=ZABBIX_PASSWORD)
|
||||
print(f"Connected to Zabbix at {ZABBIX_URL}")
|
||||
print(f"Authenticated as user: {ZABBIX_USER}")
|
||||
except Exception as e:
|
||||
print(f"Failed to connect to Zabbix: {e}")
|
||||
return
|
||||
|
||||
# Get all host IDs
|
||||
try:
|
||||
hosts = zapi.host.get(output=['hostid', 'host'])
|
||||
|
||||
if not hosts:
|
||||
print("No hosts found")
|
||||
return
|
||||
|
||||
# Extract host IDs
|
||||
host_ids = [host['hostid'] for host in hosts]
|
||||
host_ids.sort(key=int) # Sort numerically
|
||||
|
||||
print(f"Found {len(host_ids)} hosts")
|
||||
|
||||
# Generate filename with current date
|
||||
current_date = datetime.now().strftime("%Y%m%d")
|
||||
filename = f"{current_date}_host_ids_legacy.txt"
|
||||
|
||||
# Write host IDs to file (comma-separated on single line)
|
||||
with open(filename, 'w') as f:
|
||||
f.write(','.join(host_ids))
|
||||
|
||||
print(f"Host IDs saved to: {filename}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error retrieving host IDs: {e}")
|
||||
|
||||
finally:
|
||||
# Logout from Zabbix
|
||||
try:
|
||||
zapi.logout()
|
||||
print("Logged out from Zabbix")
|
||||
except:
|
||||
pass # Ignore logout errors
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
16
config-mover/run_export_legacy.sh
Executable file
16
config-mover/run_export_legacy.sh
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Legacy script to run the Zabbix configuration exporter for older Zabbix versions
|
||||
# Replace the values below with your actual Zabbix configuration
|
||||
|
||||
# Set environment variables
|
||||
export ZABBIX_URL="https://your.zabbix/api_jsonrpc.php"
|
||||
export HOST_IDS="10084,10584,10591,10595" # Comma-separated list of host IDs
|
||||
export OUTPUT_DIR="/opt/python/export"
|
||||
export ZABBIX_USER="your_username"
|
||||
export ZABBIX_PASSWORD="your_password"
|
||||
|
||||
# Activate virtual environment and run the script
|
||||
cd /opt/python
|
||||
source venv/bin/activate
|
||||
python3 config_exporter_legacy.py
|
||||
14
config-mover/run_get_ids_legacy.sh
Executable file
14
config-mover/run_get_ids_legacy.sh
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Example script to run the Zabbix host IDs retriever for older Zabbix versions
|
||||
# Replace the values below with your actual Zabbix configuration
|
||||
|
||||
# Set environment variables
|
||||
export ZABBIX_URL="https://your.zabbix/api_jsonrpc.php"
|
||||
export ZABBIX_USER="your_username"
|
||||
export ZABBIX_PASSWORD="your_password"
|
||||
|
||||
# Activate virtual environment and run the script
|
||||
cd /opt/python
|
||||
source venv/bin/activate
|
||||
python3 get_host_ids_legacy.py
|
||||
Reference in New Issue
Block a user