Files
zabbix-agent-deployment/install-update-zabbix-agent.ps1

231 lines
8.3 KiB
PowerShell
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.

#Requires -RunAsAdministrator
# =============================================================================
# Zabbix Agent 2 Windows Installer / Updater (Version 7.4)
#
# Aufruf:
# PowerShell -ExecutionPolicy Bypass -File install-update-zabbix-agent.ps1
#
# Interaktiv: Proxy-Auswahl, Hostname-Eingabe, automatische Install/Update-Erkennung
# =============================================================================
# ── Konfiguration (manuell anpassen) ─────────────────────────────────────────
$ZabbixServer = "zabbix.example.com" # Zabbix-Server-Adresse
$ZabbixVersion = "7.4.2" # Zabbix-Version (bei neuen Releases anpassen)
# Proxy-Liste hier manuell pflegen
$ProxyList = @(
"http://proxy01.example.com:3128"
"http://proxy02.example.com:3128"
# weitere Proxys einfach hier eintragen
)
# Installationspfade (normalerweise nicht ändern nötig)
$InstallDir = "C:\Program Files\Zabbix Agent 2"
$ConfigFile = "$InstallDir\zabbix_agent2.conf"
$ServiceName = "Zabbix Agent 2"
$TempDir = $env:TEMP
# ── Hilfsfunktionen ───────────────────────────────────────────────────────────
function Write-Info { param($msg) Write-Host "[INFO] $msg" -ForegroundColor Green }
function Write-Warn { param($msg) Write-Host "[WARN] $msg" -ForegroundColor Yellow }
function Write-Err { param($msg) Write-Host "[ERROR] $msg" -ForegroundColor Red }
function Show-Header {
Write-Host ""
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host " Zabbix Agent 2 $ZabbixVersion Windows Setup" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host ""
}
# ── Installierte Version prüfen ───────────────────────────────────────────────
function Get-InstalledVersion {
$reg = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" `
-ErrorAction SilentlyContinue |
Where-Object { $_.DisplayName -like "Zabbix Agent 2*" } |
Select-Object -First 1
return $reg?.DisplayVersion
}
# ── Proxy-Auswahl ─────────────────────────────────────────────────────────────
function Select-Proxy {
Write-Host "Proxy-Einstellungen:" -ForegroundColor Cyan
Write-Host " [0] Kein Proxy (direkte Verbindung)"
for ($i = 0; $i -lt $ProxyList.Count; $i++) {
Write-Host " [$($i+1)] $($ProxyList[$i])"
}
Write-Host ""
do {
$choice = Read-Host "Auswahl [0-$($ProxyList.Count)]"
} while ($choice -notmatch "^\d+$" -or [int]$choice -gt $ProxyList.Count)
if ($choice -eq "0") {
return $null
} else {
return $ProxyList[[int]$choice - 1]
}
}
# ── Download ──────────────────────────────────────────────────────────────────
function Get-ZabbixMsi {
param(
[string]$Url,
[string]$Destination,
[string]$ProxyUrl
)
Write-Info "Lade MSI herunter ..."
Write-Info "URL: $Url"
try {
$webClient = New-Object System.Net.WebClient
if ($ProxyUrl) {
Write-Info "Verwende Proxy: $ProxyUrl"
$proxy = New-Object System.Net.WebProxy($ProxyUrl, $true)
$proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$webClient.Proxy = $proxy
} else {
$webClient.Proxy = $null
Write-Info "Direktverbindung (kein Proxy)"
}
$webClient.DownloadFile($Url, $Destination)
Write-Info "Download abgeschlossen: $Destination"
} catch {
Write-Err "Download fehlgeschlagen: $_"
exit 1
} finally {
$webClient.Dispose()
}
}
# ── MSI Install / Update ──────────────────────────────────────────────────────
function Install-ZabbixMsi {
param(
[string]$MsiPath,
[string]$Hostname,
[bool]$IsUpdate
)
$action = if ($IsUpdate) { "Update" } else { "Installation" }
Write-Info "Starte $action ..."
# Bei Update: Dienst stoppen
if ($IsUpdate) {
$svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($svc -and $svc.Status -eq "Running") {
Write-Info "Stoppe Dienst '$ServiceName' ..."
Stop-Service -Name $ServiceName -Force
Start-Sleep -Seconds 2
}
}
$msiArgs = @(
"/i", $MsiPath,
"/qn", # Silent
"/l*v", "$TempDir\zabbix-install.log",
"SERVER=$ZabbixServer",
"SERVERACTIVE=$ZabbixServer",
"HOSTNAME=$Hostname",
"LISTENPORT=10050"
)
$proc = Start-Process -FilePath "msiexec.exe" -ArgumentList $msiArgs -Wait -PassThru
if ($proc.ExitCode -ne 0) {
Write-Err "MSI $action fehlgeschlagen (ExitCode: $($proc.ExitCode))"
Write-Err "Details: $TempDir\zabbix-install.log"
exit 1
}
Write-Info "MSI $action erfolgreich."
}
# ── Dienst starten ────────────────────────────────────────────────────────────
function Start-ZabbixService {
$svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if (-not $svc) {
Write-Err "Dienst '$ServiceName' nicht gefunden."
exit 1
}
Set-Service -Name $ServiceName -StartupType Automatic
Start-Service -Name $ServiceName
Start-Sleep -Seconds 2
$svc.Refresh()
return $svc.Status
}
# ── Hauptprogramm ─────────────────────────────────────────────────────────────
Show-Header
# Installierte Version ermitteln
$installedVersion = Get-InstalledVersion
$isUpdate = $null -ne $installedVersion
if ($isUpdate) {
Write-Info "Installierte Version gefunden: $installedVersion"
Write-Info "Modus: UPDATE auf $ZabbixVersion"
} else {
Write-Info "Kein Zabbix Agent gefunden."
Write-Info "Modus: NEUINSTALLATION von $ZabbixVersion"
}
Write-Host ""
# Hostname abfragen
$defaultHostname = $env:COMPUTERNAME
$inputHostname = Read-Host "Zabbix-Hostname eingeben [Standard: $defaultHostname]"
$agentHostname = if ($inputHostname.Trim() -eq "") { $defaultHostname } else { $inputHostname.Trim() }
Write-Info "Verwende Hostname: $agentHostname"
Write-Host ""
# Proxy-Auswahl
$selectedProxy = Select-Proxy
Write-Host ""
# MSI-Download-URL zusammenbauen
$arch = "amd64"
$msiFile = "zabbix_agent2-$ZabbixVersion-windows-$arch-openssl.msi"
$msiUrl = "https://cdn.zabbix.com/zabbix/binaries/stable/" +
"$($ZabbixVersion.Split('.')[0..1] -join '.')/$ZabbixVersion/$msiFile"
$msiDest = "$TempDir\$msiFile"
# Download
Get-ZabbixMsi -Url $msiUrl -Destination $msiDest -ProxyUrl $selectedProxy
# Installation / Update
Install-ZabbixMsi -MsiPath $msiDest -Hostname $agentHostname -IsUpdate $isUpdate
# Temporäre Datei aufräumen
Remove-Item -Path $msiDest -Force -ErrorAction SilentlyContinue
# Dienst starten
Write-Info "Starte Zabbix-Dienst ..."
$status = Start-ZabbixService
# Ergebnis
Write-Host ""
Write-Host "=============================================" -ForegroundColor Cyan
Write-Info " Zabbix Agent 2 erfolgreich eingerichtet!"
Write-Info " Hostname : $agentHostname"
Write-Info " Server : $ZabbixServer"
Write-Info " Version : $ZabbixVersion"
Write-Info " Modus : Aktiv"
Write-Info " Dienst : $status"
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host ""
Write-Warn "Denk daran: Den Host '$agentHostname' im Zabbix-Frontend anlegen,"
Write-Warn "falls er noch nicht existiert (Configuration -> Hosts -> Create host)."
Write-Host ""