updated zabbix import

This commit is contained in:
Kuro 2025-07-20 01:16:12 +02:00
parent 04aef7d999
commit 95503e8f36
1 changed files with 218 additions and 13 deletions

View File

@ -1,31 +1,229 @@
#!/bin/bash
# --- Paths to the files created by the Nagios export script ---
# These are passed as environment variables to the embedded Python script.
export ZABBIX_HOSTGROUPS_FILE="zabbix_hostgroups.txt"
export ZABBIX_HOSTS_FILE="zabbix_hosts.txt"
export ZABBIX_COMMANDS_FILE="zabbix_commands.txt"
export ZABBIX_SERVICES_RAW_FILE="zabbix_services_raw.txt"
echo "--- Zabbix Automatic Import Script (Bash wrapper for Python) ---"
echo " Attempting to ensure all Python dependencies are met."
echo ""
# --- OS Detection for package manager ---
DETECTED_OS=""
if [ -f /etc/os-release ]; then
. /etc/os-release
if [[ "$ID" == "debian" || "$ID_LIKE" == "debian" ]]; then
DETECTED_OS="debian"
elif [[ "$ID" == "ubuntu" ]]; then
DETECTED_OS="ubuntu"
elif [[ "$ID" == "centos" || "$ID" == "rhel" || "$ID_LIKE" == "fedora" || "$ID" == "fedora" ]]; then
DETECTED_OS="redhat"
fi
fi
install_package() {
local package_name="$1"
local install_cmd=""
case "$DETECTED_OS" in
"debian"|"ubuntu")
install_cmd="sudo apt update && sudo apt install -y $package_name"
;;
"redhat")
if command -v dnf &> /dev/null; then
install_cmd="sudo dnf install -y $package_name"
else
install_cmd="sudo yum install -y $package_name"
fi
;;
*)
echo "Error: Unsupported operating system for automatic package installation." >&2
echo "Please install '$package_name' manually." >&2
return 1
;;
esac
echo "Attempting to install '$package_name' using: $install_cmd"
if eval "$install_cmd"; then
echo "'$package_name' installed successfully."
return 0
else
echo "Error: Installation of '$package_name' failed. Please try installing it manually." >&2
return 1
fi
}
# --- Check and install Python3 if missing ---
echo "Checking for 'python3'..."
if ! command -v python3 &> /dev/null; then
echo "---------------------------------------------------------"
echo "WARNING: 'python3' is not found on your system."
echo "This script requires Python 3 to run."
echo ""
read -p "Do you want to attempt to install 'python3' now? (y/N): " -n 1 -r REPLY_PYTHON
echo
if [[ $REPLY_PYTHON =~ ^[Yy]$ ]]; then
if install_package "python3"; then
echo "'python3' installed. Verifying..."
if ! command -v python3 &> /dev/null; then
echo "Error: Python 3 not found even after installation. Please check your system PATH." >&2
exit 1
fi
else
echo "Python 3 installation failed. Exiting." >&2
exit 1
fi
else
echo "Python 3 installation skipped. Exiting." >&2
exit 1
fi
else
echo "'python3' found."
fi
# --- Determine the correct pip command (pip3 preferred) ---
PIP_CMD=""
if command -v pip3 &> /dev/null; then
PIP_CMD="pip3"
elif command -v pip &> /dev/null; then
PIP_CMD="pip"
fi
# --- Check and install pip (or python3-pip) if missing ---
echo "Checking for 'pip' (Python package installer)..."
if [[ -z "$PIP_CMD" ]]; then
echo "---------------------------------------------------------"
echo "WARNING: Neither 'pip3' nor 'pip' command found."
echo "Python package installer (pip) is required to install 'pyzabbix'."
echo ""
read -p "Do you want to attempt to install 'python3-pip' now? (y/N): " -n 1 -r REPLY_PIP
echo
if [[ $REPLY_PIP =~ ^[Yy]$ ]]; then
if install_package "python3-pip"; then
echo "'python3-pip' installed. Verifying..."
if command -v pip3 &> /dev/null; then
PIP_CMD="pip3"
elif command -v pip &> /dev/null; then
PIP_CMD="pip"
fi
if [[ -z "$PIP_CMD" ]]; then
echo "Error: pip not found even after installation. Please check your system PATH or pip setup." >&2
exit 1
fi
else
echo "Pip installation failed. Exiting." >&2
exit 1
fi
else
echo "Pip installation skipped. Exiting." >&2
exit 1
fi
else
echo "'$PIP_CMD' found."
fi
# --- Function to check if pyzabbix is installed ---
check_pyzabbix() {
python3 -c "import pyzabbix" &> /dev/null
}
# --- Check and potentially install pyzabbix ---
echo "Checking for 'pyzabbix' library..."
if ! check_pyzabbix; then
echo "---------------------------------------------------------"
echo "WARNING: The 'pyzabbix' Python library is not found."
echo "This script requires 'pyzabbix' to interact with the Zabbix API."
echo ""
read -p "Do you want to attempt to install 'pyzabbix' now using '$PIP_CMD'? (y/N): " -n 1 -r REPLY_PYZABBIX
echo
if [[ $REPLY_PYZABBIX =~ ^[Yy]$ ]]
then
echo "Attempting to install 'pyzabbix'..."
if "$PIP_CMD" install pyzabbix; then
echo "'pyzabbix' installed successfully."
else
echo "Installation failed. This often requires elevated privileges (sudo)."
read -p "Do you want to try installing 'pyzabbix' with 'sudo $PIP_CMD'? (y/N): " -n 1 -r REPLY_SUDO
echo
if [[ $REPLY_SUDO =~ ^[Yy]$ ]]
then
if sudo "$PIP_CMD" install pyzabbix; then
echo "'pyzabbix' installed successfully with sudo."
else
echo "Error: 'pyzabbix' installation failed even with sudo." >&2
echo "Please install it manually using: sudo $PIP_CMD install pyzabbix" >&2
exit 1
fi
else
echo "Automatic installation skipped. Exiting." >&2
exit 1
fi
fi
# Verify installation after attempt
if ! check_pyzabbix; then
echo "Error: 'pyzabbix' is still not found after installation attempt. Exiting." >&2
exit 1
fi
else
echo "Automatic installation skipped. Exiting." >&2
exit 1
fi
else
echo "'pyzabbix' library found. Proceeding..."
fi
echo ""
# --- Check if export files exist ---
REQUIRED_FILES=("$ZABBIX_HOSTGROUPS_FILE" "$ZABBIX_HOSTS_FILE" "$ZABBIX_COMMANDS_FILE" "$ZABBIX_SERVICES_RAW_FILE")
MISSING_FILES=()
for file in "${REQUIRED_FILES[@]}"; do
if [[ ! -f "$file" ]]; then
MISSING_FILES+=("$file")
fi
done
if [[ ${#MISSING_FILES[@]} -gt 0 ]]; then
echo "Error: The following required export files are missing:" >&2
for missing_file in "${MISSING_FILES[@]}"; do
echo " - $missing_file" >&2
done
echo "Please ensure you have run the Nagios export script first to create these files." >&2
exit 1
fi
echo "All core dependencies seem to be in order. Launching Zabbix import."
# Run the embedded Python script
python3 - <<EOF_PYTHON
import os
import sys
import json
from pyzabbix import ZabbixAPI, ZabbixAPIException
import getpass # For secure password input
import getpass
# --- Paths to the files created by the Nagios export script ---
ZABBIX_HOSTGROUPS_FILE = 'zabbix_hostgroups.txt'
ZABBIX_HOSTS_FILE = 'zabbix_hosts.txt'
ZABBIX_COMMANDS_FILE = 'zabbix_commands.txt' # Now contains JSON
ZABBIX_SERVICES_RAW_FILE = 'zabbix_services_raw.txt'
# Paths to the files created by the Nagios export script
# These are read from environment variables set by the bash script.
ZABBIX_HOSTGROUPS_FILE = os.environ.get('ZABBIX_HOSTGROUPS_FILE', 'zabbix_hostgroups.txt')
ZABBIX_HOSTS_FILE = os.environ.get('ZABBIX_HOSTS_FILE', 'zabbix_hosts.txt')
ZABBIX_COMMANDS_FILE = os.environ.get('ZABBIX_COMMANDS_FILE', 'zabbix_commands.txt')
ZABBIX_SERVICES_RAW_FILE = os.environ.get('ZABBIX_SERVICES_RAW_FILE', 'zabbix_services_raw.txt')
# --- Main script ---
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)
@ -37,7 +235,7 @@ def main():
# 1. Import Hostgroups
print("\n--- Importing Hostgroups ---")
created_hostgroups = {} # To store mapping of Nagios group name to Zabbix group ID
created_hostgroups = {}
if not os.path.exists(ZABBIX_HOSTGROUPS_FILE):
print(f"Warning: Hostgroups file not found: {ZABBIX_HOSTGROUPS_FILE}. Skipping hostgroup import.")
else:
@ -249,4 +447,11 @@ def process_host_entry(zapi, host_lines, created_hostgroups):
print(f" Error creating host '{host_name}': {e}", file=sys.stderr)
if __name__ == '__main__':
main()
main()
EOF_PYTHON
echo ""
echo "--- Zabbix Import Process Complete ---"
echo "Please review the output above for any errors or warnings."
echo "Remember to manually configure Zabbix Items, Triggers, and Templates"
echo "based on the exported Nagios Commands and Services."