From 3d6b3626037ec314f74dec8adc44551f3f7af77b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BONNET?= Date: Wed, 28 Oct 2020 13:12:35 +0100 Subject: [PATCH 1/4] Add setup and change veeam discovery informations. --- Setup.ps1 | 76 +++++++++++++++++++ Template EN/TemplateVEEAM-BACKUP-eng.xml | 2 +- ...bbix-V4.X-only-TemplateVEEAM-BACKUP-FR.xml | 2 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 Setup.ps1 diff --git a/Setup.ps1 b/Setup.ps1 new file mode 100644 index 0000000..5aab1bf --- /dev/null +++ b/Setup.ps1 @@ -0,0 +1,76 @@ +function Restart-Zabbix { + $Zabbix_Service = Get-Service -Name 'Zabbix Agent' -ErrorAction SilentlyContinue + if ($Zabbix_Service -ne $null -and $Zabbix_Service.Status -eq 'Running') { + $Zabbix_Service.Stop() + $Zabbix_Service.WaitForStatus('Stopped') + $Zabbix_Service.Start() + } +} + +function Get-ZabbixAgent { + $service_config = Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Services\Zabbix Agent' -ErrorAction SilentlyContinue + if ($service_config -ne $null) { + $image_path = $service_config.GetValue('ImagePath') + if ($image_path -match "^`"([^`"]*)`".*$") { + return $Matches.1 + } + return $image_path -replace "--config .*", "" + } + + throw 'Zabbix not installed' +} + +function Get-ZabbixPath { + $install_x64 = Get-Item 'HKLM:\SOFTWARE\Zabbix SIA\Zabbix Agent (64-bit)' -ErrorAction SilentlyContinue + if ($install_x64 -ne $null) { + return $install_x64.GetValue('InstallFolder') + } + + $install_x86 = Get-Item 'HKLM:\SOFTWARE\Zabbix SIA\Zabbix Agent' -ErrorAction SilentlyContinue + if ($install_x86 -ne $null) { + return $install_x86.GetValue('InstallFolder') + } + + return (Get-ChildItem -Path (Get-ZabbixAgent)).DirectoryName +} + +function Setup-VeeamAgent { + param( + [string]$zabbix_path = (Get-ZabbixPath) + ) + + $wmi_product = Get-WmiObject -Class Win32_Product -Filter 'Name = "Veeam Backup & Replication Server"' + if ($wmi_product -eq $null) { + Write-Debug 'Veeam server not installed. Skip.' + return + } + + $zabbix_config = " +EnableRemoteCommands=1 +UnsafeUserParameters=1 +Alias=service.discovery.veeam:service.discovery +Timeout=30 +UserParameter=vbr[*],powershell -NoProfile -ExecutionPolicy Bypass -File ""${zabbix_path}scripts\zabbix_vbr_job.ps1"" ""`$1"" ""`$2"" ""`$3"" + " -replace "`r`n", "`n" + + #$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False + #[System.IO.File]::WriteAllLines($zabbix_path + 'zabbix_agentd.conf.d\veeam_agent.conf', $zabbix_config, $Utf8NoBomEncoding) + Set-Content -Path ($zabbix_path + 'zabbix_agentd.conf.d\veeam_agent.conf') -Value $zabbix_config -Encoding Ascii + + $script_directory = $zabbix_path + 'scripts' + if (-not (Test-Path -Path $script_directory)) { + New-Item -ItemType Directory -Path $script_directory + } + + $client = new-object System.Net.WebClient + $download_path = [System.IO.Path]::GetTempFileName() + $client.DownloadFile('https://raw.githubusercontent.com/bontiv/zabbix-VEEAM_B-R/master/zabbix_vbr_job.ps1', $download_path) + + $script_content = Get-Content -Path $download_path + Set-Content -Path ($zabbix_path + 'scripts\zabbix_vbr_job.ps1') -Value ($script_content -replace "pathxml = 'C:\\Program Files\\Zabbix Agent\\scripts\\TempXmlVeeam'", "pathxml = '${zabbix_path}scripts\TempXmlVeeam'") + Remove-Item -Path $download_path + + Restart-Zabbix +} + +Setup-VeeamAgent \ No newline at end of file diff --git a/Template EN/TemplateVEEAM-BACKUP-eng.xml b/Template EN/TemplateVEEAM-BACKUP-eng.xml index 49986b2..c2cf21d 100644 --- a/Template EN/TemplateVEEAM-BACKUP-eng.xml +++ b/Template EN/TemplateVEEAM-BACKUP-eng.xml @@ -191,7 +191,7 @@ 0 - service.discovery + service.discovery.veeam 3600 0 diff --git a/Template FR/Zabbix-V4.X-only-TemplateVEEAM-BACKUP-FR.xml b/Template FR/Zabbix-V4.X-only-TemplateVEEAM-BACKUP-FR.xml index f7ba84d..9545516 100644 --- a/Template FR/Zabbix-V4.X-only-TemplateVEEAM-BACKUP-FR.xml +++ b/Template FR/Zabbix-V4.X-only-TemplateVEEAM-BACKUP-FR.xml @@ -106,7 +106,7 @@ VEEAM Services - service.discovery + service.discovery.veeam 1h From 1174ad8af817a4e1fd9567ffe961631752190a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BONNET?= Date: Wed, 28 Oct 2020 13:28:30 +0100 Subject: [PATCH 2/4] Add administrator elevation in setup script. --- Setup.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Setup.ps1 b/Setup.ps1 index 5aab1bf..302f3fa 100644 --- a/Setup.ps1 +++ b/Setup.ps1 @@ -73,4 +73,10 @@ UserParameter=vbr[*],powershell -NoProfile -ExecutionPolicy Bypass -File ""${zab Restart-Zabbix } -Setup-VeeamAgent \ No newline at end of file +# Run as administrator +$loop = [string]$args[0] +if ([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) { + Setup-VeeamAgent +} elseif ($loop -eq "") { + Start-Process Powershell -Verb runAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" NoLoop" +} \ No newline at end of file From e4f22e01000ccf8c8937b701af02a1377f7acf60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BONNET?= Date: Wed, 28 Oct 2020 15:44:00 +0100 Subject: [PATCH 3/4] Update, permit include in another script and correct the trailing slash from manual installation path discovery. --- Setup.ps1 | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Setup.ps1 b/Setup.ps1 index 302f3fa..42f0908 100644 --- a/Setup.ps1 +++ b/Setup.ps1 @@ -1,9 +1,11 @@ +param( + [switch]$elevate = $true +) + function Restart-Zabbix { $Zabbix_Service = Get-Service -Name 'Zabbix Agent' -ErrorAction SilentlyContinue if ($Zabbix_Service -ne $null -and $Zabbix_Service.Status -eq 'Running') { - $Zabbix_Service.Stop() - $Zabbix_Service.WaitForStatus('Stopped') - $Zabbix_Service.Start() + $Zabbix_Service | Restart-Service } } @@ -12,7 +14,7 @@ function Get-ZabbixAgent { if ($service_config -ne $null) { $image_path = $service_config.GetValue('ImagePath') if ($image_path -match "^`"([^`"]*)`".*$") { - return $Matches.1 + return $Matches[1] } return $image_path -replace "--config .*", "" } @@ -31,7 +33,7 @@ function Get-ZabbixPath { return $install_x86.GetValue('InstallFolder') } - return (Get-ChildItem -Path (Get-ZabbixAgent)).DirectoryName + return ((Get-ChildItem -Path (Get-ZabbixAgent)).DirectoryName + '/') } function Setup-VeeamAgent { @@ -73,10 +75,13 @@ UserParameter=vbr[*],powershell -NoProfile -ExecutionPolicy Bypass -File ""${zab Restart-Zabbix } -# Run as administrator -$loop = [string]$args[0] -if ([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) { - Setup-VeeamAgent -} elseif ($loop -eq "") { - Start-Process Powershell -Verb runAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" NoLoop" +# If main script, launch setup +if ($MyInvocation.PSCommandPath -eq $null) { + if ([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) { + Setup-VeeamAgent + } elseif ($elevate.IsPresent) { + Start-Process Powershell -Verb runAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -elevate:$false" + } else { + throw "Cannot elevate setup." + } } \ No newline at end of file From 93ee792fbbe5169814ad04adb3e870046a469b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BONNET?= Date: Wed, 28 Oct 2020 18:25:27 +0100 Subject: [PATCH 4/4] Add setup of PowerShell 3.0 (required) --- README.md | 10 +++++++++- Setup.ps1 | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 116bfee..08b49ea 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Why? Because the execution of this command can take between 30 seconds and more - Result of each jobs - Execution status for each jobs - Next run time of each jobs - + ### 5. Veeam Jobs Replication Backup: - Result of each jobs - Execution status for each jobs @@ -112,6 +112,14 @@ Why? Because the execution of this command can take between 30 seconds and more ## Setup +### Automatic scripted installation +Run Setup.ps1 for installing the monitoring script on hosts. + +### Manual installation + +Your host must have PowerShell 3.0 or above. Install upgrade from [Microsoft Download](https://www.microsoft.com/en-us/download/details.aspx?id=34595) for +windows 7 and Windows 2008. + 1. Install the Zabbix agent on your host 2. Copy `zabbix_vbr_job.ps1` in the directory : `C:\Program Files\Zabbix Agent\scripts\` (create folder if not exist) 3. Add the following line to your Zabbix agent configuration file: diff --git a/Setup.ps1 b/Setup.ps1 index 42f0908..916d162 100644 --- a/Setup.ps1 +++ b/Setup.ps1 @@ -1,5 +1,5 @@ param( - [switch]$elevate = $true + [switch]$no_elevate ) function Restart-Zabbix { @@ -75,12 +75,43 @@ UserParameter=vbr[*],powershell -NoProfile -ExecutionPolicy Bypass -File ""${zab Restart-Zabbix } +function Setup-PowerShell3 { + if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64") { + $arch = "x64" + } else { + $arch = "x86" + } + + $win_version = (Get-WmiObject Win32_OperatingSystem).Version -split '\.' + $base_version = $win_version[0] + '.' + $win_version[1] + + if ($base_version -eq '6.0') { + $package = 'Windows6.0-KB2506146' + } elseif ($base_version -eq '6.1') { + $package = 'Windows6.1-KB2506143' + } + $url = "https://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/${package}-${arch}.msu" + $msu_file = $msi_file = [System.IO.Path]::GetTempPath() + "${package}-${arch}.msu" + + $client = new-object System.Net.WebClient + $client.DownloadFile($url, $msu_file) + + Start-Process WUSA -ArgumentList ($msu_file, '/quiet', '/norestart') -Wait +} + # If main script, launch setup if ($MyInvocation.PSCommandPath -eq $null) { if ([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) { + if ($PSVersionTable.PSCompatibleVersions -notcontains "3.0") { + Setup-PowerShell3 + } Setup-VeeamAgent - } elseif ($elevate.IsPresent) { - Start-Process Powershell -Verb runAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -elevate:$false" + } elseif (-not $no_elevate.IsPresent) { + # PowerShell 2.0 do not set $PSCommandPath. So use current file invocation. + if ($PSCommandPath -eq $null) { + $PSCommandPath = $MyInvocation.InvocationName + } + Start-Process Powershell -Verb runAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -no_elevate" } else { throw "Cannot elevate setup." }