update to the script to automaticly include/download/install pysabbix

This commit is contained in:
Kuro 2025-07-20 01:09:24 +02:00
parent 13e8011d6b
commit 04aef7d999
1 changed files with 19 additions and 15 deletions

View File

@ -2,11 +2,7 @@ import os
import sys import sys
import json import json
from pyzabbix import ZabbixAPI, ZabbixAPIException from pyzabbix import ZabbixAPI, ZabbixAPIException
import getpass # For secure password input
# --- Zabbix API Configuration (IMPORTANT: Update these!) ---
ZABBIX_SERVER = 'http://your_zabbix_server/api_jsonrpc.php'
ZABBIX_USER = 'Admin'
ZABBIX_PASSWORD = 'zabbix'
# --- Paths to the files created by the Nagios export script --- # --- Paths to the files created by the Nagios export script ---
ZABBIX_HOSTGROUPS_FILE = 'zabbix_hostgroups.txt' ZABBIX_HOSTGROUPS_FILE = 'zabbix_hostgroups.txt'
@ -18,14 +14,25 @@ ZABBIX_SERVICES_RAW_FILE = 'zabbix_services_raw.txt'
def main(): def main():
print("--- Zabbix Automatic Import Script ---") print("--- Zabbix Automatic Import Script ---")
# Ask user for Zabbix API Configuration
print("\nPlease provide your Zabbix API connection details:")
zabbix_server = input("Zabbix Server URL (e.g., http://your_zabbix_server/api_jsonrpc.php): ").strip()
zabbix_user = input("Zabbix API Login Name: ").strip()
zabbix_password = getpass.getpass("Zabbix API Password: ").strip()
# Validate inputs
if not zabbix_server or not zabbix_user or not zabbix_password:
print("Error: All Zabbix connection details must be provided.", file=sys.stderr)
sys.exit(1)
# Connect to Zabbix API # Connect to Zabbix API
try: try:
zapi = ZabbixAPI(ZABBIX_SERVER) zapi = ZabbixAPI(zabbix_server)
zapi.login(ZABBIX_USER, ZABBIX_PASSWORD) zapi.login(zabbix_user, zabbix_password)
print(f"Connected to Zabbix API at {ZABBIX_SERVER} as user {ZABBIX_USER}") print(f"Connected to Zabbix API at {zabbix_server} as user {zabbix_user}")
except ZabbixAPIException as e: except ZabbixAPIException as e:
print(f"Error connecting to Zabbix API: {e}", file=sys.stderr) print(f"Error connecting to Zabbix API: {e}", file=sys.stderr)
print("Please check ZABBIX_SERVER, ZABBIX_USER, and ZABBIX_PASSWORD.", file=sys.stderr) print("Please check the provided server URL, login name, and password.", file=sys.stderr)
sys.exit(1) sys.exit(1)
# 1. Import Hostgroups # 1. Import Hostgroups
@ -39,7 +46,6 @@ def main():
lines = [line for line in lines if not line.startswith("--- Hostgroups ---")] lines = [line for line in lines if not line.startswith("--- Hostgroups ---")]
for i in range(0, len(lines), 2): for i in range(0, len(lines), 2):
name_alias_line = lines[i].strip() name_alias_line = lines[i].strip()
# members_line = lines[i+1].strip() # Not directly used for Zabbix group creation
if not name_alias_line.startswith("Name:"): if not name_alias_line.startswith("Name:"):
print(f"Warning: Skipping malformed hostgroup line: {name_alias_line}", file=sys.stderr) print(f"Warning: Skipping malformed hostgroup line: {name_alias_line}", file=sys.stderr)
@ -79,7 +85,7 @@ def main():
host_data_buffer = [] host_data_buffer = []
for line in lines: for line in lines:
line = line.strip() line = line.strip()
if line.startswith("Host:"): if line.startswith(" Host:"):
if host_data_buffer: if host_data_buffer:
process_host_entry(zapi, host_data_buffer, created_hostgroups) process_host_entry(zapi, host_data_buffer, created_hostgroups)
host_data_buffer = [] host_data_buffer = []
@ -97,7 +103,6 @@ def main():
else: else:
try: try:
with open(ZABBIX_COMMANDS_FILE, 'r') as f: with open(ZABBIX_COMMANDS_FILE, 'r') as f:
# Skip header line before parsing JSON
commands_json_str = "" commands_json_str = ""
for line in f: for line in f:
if not line.startswith("--- Commands (JSON Data) ---"): if not line.startswith("--- Commands (JSON Data) ---"):
@ -159,7 +164,6 @@ def process_host_entry(zapi, host_lines, created_hostgroups):
address = "" address = ""
nagios_hostgroups = [] nagios_hostgroups = []
# Extract data from buffered lines
for line in host_lines: for line in host_lines:
if line.startswith(" Host:"): if line.startswith(" Host:"):
parts = line.split("|") parts = line.split("|")
@ -223,7 +227,7 @@ def process_host_entry(zapi, host_lines, created_hostgroups):
host_id = existing_hosts[0]['hostid'] host_id = existing_hosts[0]['hostid']
print(f" Host '{host_name}' already exists with ID: {host_id}. Skipping creation.") print(f" Host '{host_name}' already exists with ID: {host_id}. Skipping creation.")
try: try:
zapi.host.update(hostid=host_id, groups=zabbix_groups_payload, interfaces=zabbabbix_interfaces_payload) zapi.host.update(hostid=host_id, groups=zabbix_groups_payload, interfaces=zabbix_interfaces_payload)
print(f" Updated host '{host_name}'.") print(f" Updated host '{host_name}'.")
except ZabbixAPIException as e: except ZabbixAPIException as e:
print(f" Error updating host '{host_name}': {e}", file=sys.stderr) print(f" Error updating host '{host_name}': {e}", file=sys.stderr)