Update Deploy-ZabbixAgent2.ps1
This commit is contained in:
@@ -24,48 +24,43 @@ $localIPs = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress
|
||||
if ($localIPs -match '^192\.168\.(10[0-5])\.') {
|
||||
$Location = "Node1"
|
||||
$ZabbixServer = "192.168.101.30"
|
||||
$ShareSource = "\\EHI-DC4\Zabbix"
|
||||
$ShareSource = "\\EHI-DC4.efhosting.sbcit.com.au\Zabbix"
|
||||
}
|
||||
# Node 2: 192.168.130.0-135.255 AND 192.168.141.0-145.255
|
||||
elseif ($localIPs -match '^192\.168\.(13[0-5]|14[1-5])\.') {
|
||||
$Location = "Node2"
|
||||
$ZabbixServer = "192.168.130.80"
|
||||
$ShareSource = "\\E2HI-DC5\Zabbix"
|
||||
$ShareSource = "\\E2HI-DC5.efhosting.sbcit.com.au\Zabbix"
|
||||
}
|
||||
# Brisbane: 192.168.3.0/24 AND 192.168.200.0/24
|
||||
elseif ($localIPs -match '^192\.168\.(3|200)\.') {
|
||||
$Location = "Brisbane"
|
||||
$ZabbixServer = "192.168.3.43"
|
||||
$ShareSource = "\\BI-DC1\Zabbix"
|
||||
$ShareSource = "\\BI-DC1.efhosting.sbcit.com.au\Zabbix"
|
||||
}
|
||||
# Fallback Default to Node 1
|
||||
else {
|
||||
$Location = "Node1"
|
||||
$ZabbixServer = "192.168.101.30"
|
||||
$ShareSource = "\\EHI-DC4\Zabbix"
|
||||
$ShareSource = "\\EHI-DC4.efhosting.sbcit.com.au\Zabbix"
|
||||
}
|
||||
|
||||
# --- 3. Dynamic Hardware and Role Detection ---
|
||||
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
|
||||
$Hostname = $env:COMPUTERNAME
|
||||
|
||||
# Workstations
|
||||
if ($osInfo.ProductType -eq 1) {
|
||||
$Role = "Workstation"
|
||||
}
|
||||
# Primary: Hostname matches SQL naming convention (e.g., EHTS-SS01, BI-SS02)
|
||||
elseif ($Hostname -match '-ss\d*') {
|
||||
$Role = "SQL"
|
||||
}
|
||||
# Primary: Hostname matches Application naming convention (e.g., EHTS-APP38)
|
||||
elseif ($Hostname -match '-app\d*') {
|
||||
$Role = "Application"
|
||||
}
|
||||
# Secondary Fallback: Active MSSQLSERVER service on unstandardized server names
|
||||
elseif (Get-Service -Name "MSSQLSERVER" -ErrorAction SilentlyContinue | Where-Object { $_.Status -eq "Running" }) {
|
||||
$Role = "SQL"
|
||||
}
|
||||
# Default fallback for DCs, utilities, and general servers
|
||||
else {
|
||||
$Role = "Application"
|
||||
}
|
||||
@@ -81,17 +76,19 @@ $MetadataVal = "Windows:$($HardwareType):$($Location):$($Role)"
|
||||
# --- 4. File Acquisition with Multi-Tier Fallback (SMB -> Git -> CDN) ---
|
||||
$SourceZip = "$ShareSource\zabbix_agent2.zip"
|
||||
$GitZipUrl = "https://git.mkau.au/DistractADD/zabbix/raw/branch/main/zabbix_agent2.zip"
|
||||
$CdnZipUrl = "https://cdn.zabbix.com/zabbix/binaries/stable/7.4/7.4.11/zabbix_agent2-7.4.11-windows-amd64-openssl-static.zip"[cite: 1, 3, 4, 5]
|
||||
$InstallDir = "C:\Program Files\Zabbix Agent 2"[cite: 1, 3, 4, 5]
|
||||
$TempZip = "C:\Temp\zabbix_agent2.zip"[cite: 1, 3, 4, 5]
|
||||
$CdnZipUrl = "https://cdn.zabbix.com/zabbix/binaries/stable/7.4/7.4.11/zabbix_agent2-7.4.11-windows-amd64-openssl-static.zip"
|
||||
$InstallDir = "C:\Program Files\Zabbix Agent 2"
|
||||
$TempZip = "C:\Temp\zabbix_agent2.zip"
|
||||
|
||||
if (-not (Test-Path "C:\Temp")) { New-Item -ItemType Directory -Path "C:\Temp" -Force | Out-Null }[cite: 1, 3, 4, 5]
|
||||
if (-not (Test-Path "C:\Temp")) {
|
||||
New-Item -ItemType Directory -Path "C:\Temp" -Force | Out-Null
|
||||
}
|
||||
|
||||
# Stop running instances safely before replacing binaries
|
||||
Get-Service "Zabbix Agent*" -ErrorAction SilentlyContinue | Stop-Service -Force -ErrorAction SilentlyContinue[cite: 1, 3, 4, 5]
|
||||
Get-Process "zabbix_agent*" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue[cite: 1, 3, 4, 5]
|
||||
# Stop running instances safely before replacing files
|
||||
Get-Service "Zabbix Agent*" -ErrorAction SilentlyContinue | Stop-Service -Force -ErrorAction SilentlyContinue
|
||||
Get-Process "zabbix_agent*" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
|
||||
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12[cite: 1, 3, 4, 5]
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
$acquired = $false
|
||||
|
||||
# Attempt 1: Local DC SMB Share
|
||||
@@ -103,29 +100,23 @@ try {
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
# SMB failed or blocked
|
||||
}
|
||||
catch {}
|
||||
|
||||
# Attempt 2: Internal Git Server (Ideal for firewalled/isolated subnets)
|
||||
# Attempt 2: Internal Git Server
|
||||
if (-not $acquired) {
|
||||
try {
|
||||
Write-Host "SMB inaccessible. Downloading agent archive from internal Git..." -ForegroundColor Yellow
|
||||
Invoke-WebRequest -Uri $GitZipUrl -OutFile $TempZip -UseBasicParsing -TimeoutSec 30 -ErrorAction Stop
|
||||
if ((Test-Path $TempZip) -and ((Get-Item $TempZip).Length -gt 1000000)) {
|
||||
$acquired = $true
|
||||
}
|
||||
}
|
||||
catch {
|
||||
# Git raw zip not present or timed out
|
||||
}
|
||||
catch {}
|
||||
}
|
||||
|
||||
# Attempt 3: Official Zabbix CDN Fallback
|
||||
if (-not $acquired) {
|
||||
try {
|
||||
Write-Host "Git archive download failed. Falling back to official Zabbix CDN..." -ForegroundColor Yellow
|
||||
Invoke-WebRequest -Uri $CdnZipUrl -OutFile $TempZip -UseBasicParsing -TimeoutSec 60 -ErrorAction Stop[cite: 1, 3, 4, 5]
|
||||
Invoke-WebRequest -Uri $CdnZipUrl -OutFile $TempZip -UseBasicParsing -TimeoutSec 60 -ErrorAction Stop
|
||||
if ((Test-Path $TempZip) -and ((Get-Item $TempZip).Length -gt 1000000)) {
|
||||
$acquired = $true
|
||||
}
|
||||
@@ -136,15 +127,15 @@ if (-not $acquired) {
|
||||
}
|
||||
|
||||
if (Test-Path $InstallDir) {
|
||||
Remove-Item $InstallDir -Recurse -Force -ErrorAction SilentlyContinue[cite: 1, 3, 4, 5]
|
||||
Remove-Item $InstallDir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
New-Item -Path $InstallDir -ItemType Directory -Force | Out-Null[cite: 1, 3, 4, 5]
|
||||
New-Item -Path $InstallDir -ItemType Directory -Force | Out-Null
|
||||
|
||||
Expand-Archive -Path $TempZip -DestinationPath $InstallDir -Force[cite: 1, 3, 4, 5]
|
||||
Expand-Archive -Path $TempZip -DestinationPath $InstallDir -Force
|
||||
|
||||
$BinDir = Get-ChildItem -Path $InstallDir -Recurse | Where-Object { $_.Name -eq "zabbix_agent2.exe" } | Select-Object -ExpandProperty DirectoryName[cite: 1, 3, 4, 5]
|
||||
$BinDir = Get-ChildItem -Path $InstallDir -Recurse | Where-Object { $_.Name -eq "zabbix_agent2.exe" } | Select-Object -ExpandProperty DirectoryName
|
||||
if ($BinDir -ne $InstallDir) {
|
||||
Copy-Item -Path "$BinDir\*" -Destination $InstallDir -Recurse -Force[cite: 1, 3, 4, 5]
|
||||
Copy-Item -Path "$BinDir\*" -Destination $InstallDir -Recurse -Force
|
||||
}
|
||||
|
||||
# --- 5. Generate Standardized Configuration ---
|
||||
@@ -161,26 +152,22 @@ ControlSocket=\\.\pipe\agent.sock
|
||||
"@
|
||||
Set-Content -Path "$InstallDir\zabbix_agent2.conf" -Value $ConfigContent
|
||||
|
||||
# --- 6. Service Installation & Startup ---
|
||||
# --- 6. Service Registration & Startup ---
|
||||
$ExePath = "$InstallDir\zabbix_agent2.exe"
|
||||
$ConfPath = "$InstallDir\zabbix_agent2.conf"
|
||||
|
||||
$existingService = Get-Service -Name "Zabbix Agent 2" -ErrorAction SilentlyContinue
|
||||
|
||||
if (-not $existingService) {
|
||||
# Remove orphan Event Log key if left behind by prior uninstalls or sc delete
|
||||
Remove-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\Zabbix Agent 2" -Recurse -Force -ErrorAction SilentlyContinue
|
||||
|
||||
# Register the service for the first time
|
||||
& $ExePath --config "$ConfPath" --install
|
||||
Start-Sleep -Seconds 2
|
||||
}
|
||||
|
||||
# Ensure service is started and set to automatic
|
||||
Start-Service "Zabbix Agent 2" -ErrorAction SilentlyContinue
|
||||
Set-Service -Name "Zabbix Agent 2" -StartupType Automatic
|
||||
|
||||
# --- 7. Firewall Rules ---
|
||||
# --- 7. Firewall Configuration ---
|
||||
if (-not (Get-NetFirewallRule -DisplayName "Zabbix Agent Inbound" -ErrorAction SilentlyContinue)) {
|
||||
New-NetFirewallRule -DisplayName "Zabbix Agent Inbound" -Direction Inbound -LocalPort 10050 -Protocol TCP -Action Allow -Profile Any | Out-Null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user