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

252 lines
8.5 KiB
PowerShell

#Requires -RunAsAdministrator
# =============================================================================
# Zabbix Agent 2 - Windows Installer / Updater
#
# Aufruf:
# PowerShell -ExecutionPolicy Bypass -File install-update-zabbix-agent.ps1
#
# Interaktiv: Proxy-Auswahl, Hostname-Eingabe, automatische Install/Update-Erkennung
# =============================================================================
# --------------------------------------------------------------------------
# Konfiguration (manuell anpassen)
# --------------------------------------------------------------------------
$ScriptVersion = "1.2" # Skript-Version
$ZabbixServer = "zabbix.server-nb.de" # Zabbix-Server-Adresse
$ZabbixVersion = "7.4.7" # Zabbix Agent-Version (bei neuen Releases anpassen)
# Proxy-Liste - hier manuell pflegen (Name und URL)
$ProxyList = @(
@{ Name = "Standort Buero A"; Url = "http://proxy01.example.com:3128" }
@{ Name = "Standort Buero B"; Url = "http://proxy02.example.com:3128" }
# weitere Proxys einfach hier eintragen:
# @{ Name = "Rechenzentrum"; Url = "http://proxy03.example.com:3128" }
)
# Installationspfade (normalerweise nicht aendern noetig)
$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 " Skript-Version: $ScriptVersion" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host ""
}
# --------------------------------------------------------------------------
# Installierte Version pruefen
# --------------------------------------------------------------------------
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
if ($null -ne $reg) { return $reg.DisplayVersion }
return $null
}
# --------------------------------------------------------------------------
# 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].Name) - $($ProxyList[$i].Url)"
}
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].Url
}
}
# --------------------------------------------------------------------------
# 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
# Verwendet ProcessStartInfo direkt - umgeht PS5.1 Quoting-Probleme
# bei Leerzeichen in Werten (z.B. Hostname "Tischlerei Terminal")
# --------------------------------------------------------------------------
function Install-ZabbixMsi {
param(
[string]$MsiPath,
[string]$Hostname,
[bool]$IsUpdate
)
$action = if ($IsUpdate) { "Update" } else { "Installation" }
Write-Info "Starte $action ..."
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
}
}
$logFile = "$TempDir\zabbix-install.log"
$argString = "/i `"$MsiPath`" /qn /l*v `"$logFile`" " +
"SERVER=`"$ZabbixServer`" " +
"SERVERACTIVE=`"$ZabbixServer`" " +
"HOSTNAME=`"$Hostname`" " +
"LISTENPORT=10050"
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "msiexec.exe"
$pinfo.Arguments = $argString
$pinfo.UseShellExecute = $false
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo = $pinfo
$proc.Start() | Out-Null
$proc.WaitForExit()
if ($proc.ExitCode -ne 0) {
Write-Err "MSI $action fehlgeschlagen (ExitCode: $($proc.ExitCode))"
Write-Err "Details: $logFile"
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
$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 ""
$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 ""
$selectedProxy = Select-Proxy
Write-Host ""
$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"
Get-ZabbixMsi -Url $msiUrl -Destination $msiDest -ProxyUrl $selectedProxy
Install-ZabbixMsi -MsiPath $msiDest -Hostname $agentHostname -IsUpdate $isUpdate
Remove-Item -Path $msiDest -Force -ErrorAction SilentlyContinue
Write-Info "Starte Zabbix-Dienst ..."
$status = Start-ZabbixService
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 ""