Files
zabbix-agent-deployment/deploy-update-zabbix-agent.sh

58 lines
2.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# =============================================================================
# Zabbix Agent 2 Remote-Update via SSH
#
# Aufruf:
# bash deploy-update-zabbix-agent.sh <ssh-config-name>
#
# Beispiel:
# bash deploy-update-zabbix-agent.sh webserver
#
# Der <ssh-config-name> entspricht dem Host-Eintrag in ~/.ssh/config.
# =============================================================================
set -euo pipefail
# Farben
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }
# ── Parameter ─────────────────────────────────────────────────────────────────
[[ $# -lt 1 ]] && error "Aufruf: $0 <ssh-config-name>\nBeispiel: $0 webserver"
SSH_HOST="$1"
REMOTE_TMP="/tmp/update-zabbix-agent.sh"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UPDATE_SCRIPT="${SCRIPT_DIR}/update-zabbix-agent.sh"
# ── Prüfungen ─────────────────────────────────────────────────────────────────
[[ ! -f "$UPDATE_SCRIPT" ]] && error "Update-Skript nicht gefunden: ${UPDATE_SCRIPT}"
# SSH-Verbindung testen
info "Teste SSH-Verbindung zu '${SSH_HOST}' (via ~/.ssh/config) ..."
ssh -o ConnectTimeout=10 \
-o BatchMode=yes \
-o StrictHostKeyChecking=accept-new \
"${SSH_HOST}" "echo ok" > /dev/null \
|| error "SSH-Verbindung fehlgeschlagen. Ist '${SSH_HOST}' in ~/.ssh/config definiert?"
# ── Skript übertragen ─────────────────────────────────────────────────────────
info "Übertrage Update-Skript auf '${SSH_HOST}' ..."
scp -q \
-o StrictHostKeyChecking=accept-new \
"$UPDATE_SCRIPT" \
"${SSH_HOST}:${REMOTE_TMP}"
# ── Remote-Update ─────────────────────────────────────────────────────────────
info "Starte Update auf '${SSH_HOST}' ..."
echo "──────────────────────────────────────────────────"
ssh -o StrictHostKeyChecking=accept-new \
"${SSH_HOST}" \
"bash ${REMOTE_TMP}; rm -f ${REMOTE_TMP}"
echo "──────────────────────────────────────────────────"
info "Update auf '${SSH_HOST}' abgeschlossen."