update to the script to automaticly include/download/install pysabbix
This commit is contained in:
parent
13e8011d6b
commit
04aef7d999
|
@ -2,11 +2,7 @@ import os
|
|||
import sys
|
||||
import json
|
||||
from pyzabbix import ZabbixAPI, ZabbixAPIException
|
||||
|
||||
# --- Zabbix API Configuration (IMPORTANT: Update these!) ---
|
||||
ZABBIX_SERVER = 'http://your_zabbix_server/api_jsonrpc.php'
|
||||
ZABBIX_USER = 'Admin'
|
||||
ZABBIX_PASSWORD = 'zabbix'
|
||||
import getpass # For secure password input
|
||||
|
||||
# --- Paths to the files created by the Nagios export script ---
|
||||
ZABBIX_HOSTGROUPS_FILE = 'zabbix_hostgroups.txt'
|
||||
|
@ -18,14 +14,25 @@ ZABBIX_SERVICES_RAW_FILE = 'zabbix_services_raw.txt'
|
|||
def main():
|
||||
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
|
||||
try:
|
||||
zapi = ZabbixAPI(ZABBIX_SERVER)
|
||||
zapi.login(ZABBIX_USER, ZABBIX_PASSWORD)
|
||||
print(f"Connected to Zabbix API at {ZABBIX_SERVER} as user {ZABBIX_USER}")
|
||||
zapi = ZabbixAPI(zabbix_server)
|
||||
zapi.login(zabbix_user, zabbix_password)
|
||||
print(f"Connected to Zabbix API at {zabbix_server} as user {zabbix_user}")
|
||||
except ZabbixAPIException as e:
|
||||
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)
|
||||
|
||||
# 1. Import Hostgroups
|
||||
|
@ -39,8 +46,7 @@ def main():
|
|||
lines = [line for line in lines if not line.startswith("--- Hostgroups ---")]
|
||||
for i in range(0, len(lines), 2):
|
||||
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:"):
|
||||
print(f"Warning: Skipping malformed hostgroup line: {name_alias_line}", file=sys.stderr)
|
||||
continue
|
||||
|
@ -79,7 +85,7 @@ def main():
|
|||
host_data_buffer = []
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line.startswith("Host:"):
|
||||
if line.startswith(" Host:"):
|
||||
if host_data_buffer:
|
||||
process_host_entry(zapi, host_data_buffer, created_hostgroups)
|
||||
host_data_buffer = []
|
||||
|
@ -97,7 +103,6 @@ def main():
|
|||
else:
|
||||
try:
|
||||
with open(ZABBIX_COMMANDS_FILE, 'r') as f:
|
||||
# Skip header line before parsing JSON
|
||||
commands_json_str = ""
|
||||
for line in f:
|
||||
if not line.startswith("--- Commands (JSON Data) ---"):
|
||||
|
@ -159,7 +164,6 @@ def process_host_entry(zapi, host_lines, created_hostgroups):
|
|||
address = ""
|
||||
nagios_hostgroups = []
|
||||
|
||||
# Extract data from buffered lines
|
||||
for line in host_lines:
|
||||
if line.startswith(" Host:"):
|
||||
parts = line.split("|")
|
||||
|
@ -223,7 +227,7 @@ def process_host_entry(zapi, host_lines, created_hostgroups):
|
|||
host_id = existing_hosts[0]['hostid']
|
||||
print(f" Host '{host_name}' already exists with ID: {host_id}. Skipping creation.")
|
||||
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}'.")
|
||||
except ZabbixAPIException as e:
|
||||
print(f" Error updating host '{host_name}': {e}", file=sys.stderr)
|
||||
|
|
Loading…
Reference in New Issue