Ad Widget

Collapse

Zabbix Agent deployment with PowerShell / MSI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dotup.de
    Junior Member
    • Aug 2020
    • 3

    #1

    Zabbix Agent deployment with PowerShell / MSI

    i had to install the agent on many computers for a customer. I decided to use a powershell script. Simple filter over e.g. OUs possible

    There is still a lot to do, but distributing it already works. It's using the MSI package 5.0.2 for x64

    Here it is:

    Some powershell scripts for zabbix. Contribute to dotupNET/DotupZabbixEssentials development by creating an account on GitHub.


    Maybe someone would like to contribute...

    Code:
    # Get all AD servers
    $servers = Get-ADComputer -filter * -Properties operatingsystem | Where-Object { $_.operatingsystem -like "*server*" }
    
    # Get the admin user for all servers
    $credential = Get-Credential
    
    # Install zabbix on each server
    $servers.forEach{
      Write-Host "Installing Zabbix on $($_.Name)"
      Install-ZabbixAgent -ComputerName $_.Name -Credential $credential -Server 192.168.1.32 -ServerActive 192.168.1.32
    }
    
    Write-Host "Multi installation completed"
    Last edited by dotup.de; 05-08-2020, 16:20.
  • srb
    Junior Member
    • Sep 2020
    • 10

    #2
    Hi,

    Slightly different scenario to you, I use an AD GPO Startup script to execute the below PS script. It's not great, but will give you a place to starts, I haven't managed to find a way to include "HOSTMETADATA" in the install process so have had to script it:

    Code:
    $CheckInstalled = $null
    $InstallLocation = "<UNC PATH TO MSI>"
    $MSIFile = "zabbix_agent-5.0.3-windows-amd64-openssl.msi"
    $exe = "msiexec.exe"
    $ZabbixServer = "<ZABBIX SERVER FQDN>"
    $hostFQDN = ([System.Net.Dns]::GetHostByName(($env:computerName))).HostName
    $PSKIDENTITY="<PSK IDENTIFY>"
    $PSKKEY="<PSK KEY>"
    $Arguments = "/i $InstallLocation\$MSIFile HOSTNAME=$hostFQDN SERVER=$ZabbixServer SERVERACTIVE=$ZabbixServer ENABLEPATH=TRUE LOGTYPE=system TLSCONNECT=psk TLSACCEPT=psk TLSPSKIDENTITY=$PSKIDENTITY TLSPSKVALUE=$PSKKEY /qn"
    $AgentVersion = "5.0.3"
    
    $InstallDestination = ($env:ProgramFiles)+"\Zabbix Agent"
    $ZabbixConfig = "zabbix_agentd.conf"
    
    $CheckInstalled = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -like "zabbix agent*"}
    
    #First Install
    If ($null -eq $CheckInstalled){
    Start-Process -FilePath $exe -ArgumentList $Arguments -Wait
    Start-Sleep -s 3
    # Update zabbix_agentd.conf for HOSTMETADATA
    $GetFile = Get-Content -Path "$InstallDestination\$ZabbixConfig"
    $GetFile -replace '# HostMetadata=', 'HostMetadata=Windows' | Set-Content -Path "$InstallDestination\$ZabbixConfig"
    Restart-Service -Name 'Zabbix Agent'
    sc.exe failure 'zabbix agent' reset= 86400 actions= restart/120000
    sc.exe config 'zabbix agent' start= delayed-auto
    }
    
    #Upgrade
    if ($null -ne $CheckInstalled -and $CheckInstalled.Version -lt $AgentVersion){
    Start-Process -FilePath $exe -ArgumentList $Arguments -Wait
    Restart-Service -Name 'Zabbix Agent'
    }

    Comment

    • jcalo
      Junior Member
      • Dec 2020
      • 1

      #3
      Hi,

      so quick question in regards to your deployment method. For PSKIDENTITY does this value have to be a unique identifier IE two servers cannot be the same or does it have to be unique to the zabbix server IE PSKIDENTITY="CORPNAME" for a multitude of servers.

      Comment

      • rthonpm
        Member
        • Jan 2016
        • 41

        #4
        The pre-shared keys can be unique or identical. As long as the server and Zabbix agree on the value to use it doesn't really matter.

        Sent from my BBE100-5 using Tapatalk

        Comment

        • Zablab
          Junior Member
          • Jan 2022
          • 28

          #5
          How are you folks handling upgrades? We've got a few different Server "types" (SQL, IIS, File Etc.) so the same PowerShell scripts don't reside on the same servers. The Configuration file also have unique variables. When it comes time to upgrade the agent I haven't decided what the best method is yet. I'm working on a script to do the upgrade and it's probably full of overkill\nonsensical steps, but here's what that looks like currently. It seems to work locally but I ran into issues executing remotely & haven't had time to mess with it.

          Code:
          <#
          
          Zabbix Agent Upgrade
          
          Primary Steps in upgrade:
          1. Backup Zabbix Agent
          2. Remove Zabbix Agent
          3. Install new Agent
          4. Restore Scripts, Config file
          5. Start Zabbix service
          
          Additional notes: Don't forget to set path.
          
          #>
          
          #################################################
          ###################Begin Variables##################
          #################################################
          
          ########Pick an install location########
          
          $InstallLocation = "\\super\secret\zabbix\location"
          
          #####Additional Variables#####
          
          $MSIFile = "z5.4.msi"
          $EXE = "msiexec.exe"
          $ZabbixServer = "zabbix-server.stuff.local,192.168.x.x"
          $hostFQDN = ([System.Net.Dns]::GetHostByName(($env:computerName))).HostName
          $Arguments = "/i $InstallLocation\$MSIFile SERVER=$ZabbixServer SERVERACTIVE=$ZabbixServer ENABLEPATH=TRUE LOGTYPE=file /qn"
          $AgentVersion = "5.4"
          $InstallDestination = ($env:ProgramFiles)+"\Zabbix Agent"
          $ZabbixBackup = "C:\ZabbixBackup\$((Get-Date).ToString('yyyy-MM-dd_hh.mm.ss'))"
          $Zabbixtemp = "C:\ZabbixTemp"
          $ZabbixScripts = "$Zabbixtemp\scripts"
          $ZabbixConfig = "$Zabbixtemp\zabbix_agentd.conf"
          $PSScripts ="$Zabbixtemp\*.ps1"
          $ErrorActionPreference = 'Stop'
          $Servicename = "Zabbix Agent"
          $ServiceStatus = (Get-Service -Name $ServiceName).status
          
          ###################End Variables###################
          
          #Verify Zabbix Config file exists, essentially verifying installation.
          Write-Host "Verifying Zabbix Config file." -ForegroundColor Green
          if(![System.IO.File]::Exists("$InstallDestination\zabbix_agentd.conf") ) {
          # throwing an exception will abort the job
          throw (New-Object System.IO.FileNotFoundException("Config not found. Terminating Upgrade."))
          } else {
          Write-Host 'Config found. Continuing Upgrade.' -ForegroundColor Green
          }
          
          #Create Zabbix Scripts Folder if it doesn't exist.
          #This won't overwrite or delete anything just creates the folder since there's a check later.
          New-Item -ItemType Directory -Force -Path $InstallDestination\Scripts
          
          #Create Zabbix Backup
          Write-Host "Backing up Zabbix Folder." -ForegroundColor Green
          New-Item -ItemType Directory -Force -Path $ZabbixBackup
          ICACLS $ZabbixBackup /grant Everyone:F
          ROBOCOPY /MIR $InstallDestination $ZabbixBackup /NFL /NDL /NJH /NJS /nc /ns /np
          
          #Create Zabbix TempDir
          New-Item -ItemType Directory -Force -Path $Zabbixtemp
          ICACLS $Zabbixtemp /grant Everyone:F
          ROBOCOPY /MIR $InstallDestination $Zabbixtemp /NFL /NDL /NJH /NJS /nc /ns /np
          
          #Verify Backup & Terminate upon failure
          if(![System.IO.File]::Exists($ZabbixConfig)) {
          # throwing an exception will abort the job
          throw (New-Object System.IO.FileNotFoundException("File not found: $ZabbixConfig"))
          } else {
          Write-Host 'Backup of Config Successful' -ForegroundColor Green
          }
          if(![System.IO.Directory]::Exists($ZabbixScripts)) {
          # throwing an exception will abort the job
          throw (New-Object System.IO.DirectoryNotFoundException("Directory not found: $ZabbixScripts"))
          } else {
          Write-Host 'Backup of Scripts Successful' -ForegroundColor Green
          }
          
          #Stop Zabbix Service
          Stop-Service -Name 'Zabbix Agent' -ErrorAction SilentlyContinue
          
          #Uninstall Zabbix Agent
          $product_Management_Agent = Get-WmiObject win32_product | where{$_.name -eq "Zabbix Agent (64-bit)"}
          $product_Management_Agent.IdentifyingNumber
          Start-Process "C:\Windows\System32\msiexec.exe" `
          -ArgumentList "/x $($product_Management_Agent.IdentifyingNumber) /quiet"
          
          #Remove Zabbix Folder
          Remove-item $InstallDestination -Recurse -force
          
          #Remove Zabbix Service. Sometimes it gets abandoned which will fail the installation
          sc.exe delete "zabbix agent" -ErrorAction SilentlyContinue
          
          #Upgrade Agent
          Write-Host "Starting Upgrade." -ForegroundColor Green
          Start-Process -FilePath $EXE -ArgumentList $Arguments -Wait
          
          #Verify Upgrade Installation Successful
          if(![System.IO.File]::Exists("$InstallDestination\zabbix_agentd.conf") ) {
          # throwing an exception will abort the job
          throw (New-Object System.IO.FileNotFoundException("Config not found. Upgrade Failed."))
          } else {
          Write-Host 'Config found. Continuing Upgrade.' -ForegroundColor Green
          }
          
          #Copy Scripts & Config back to Zabbix folder
          ROBOCOPY /MIR $ZabbixScripts "$InstallDestination\scripts" /NFL /NDL /NJH /NJS /nc /ns /np
          COPY $ZabbixConfig $InstallDestination
          COPY $PSScripts $InstallDestination
          
          #Delete Temp folder
          Remove-Item $Zabbixtemp -Recurse
          
          #Start Zabbix Service
          Start-Service -Name 'Zabbix Agent'
          
          #Verify Installation Successful
          if($ServiceStatus -eq "Running")
          {
          Write-Host "Upgrade successful." -ForegroundColor Green
          }
          else {
          Write-Host "Upgrade failed." -ForegroundColor Red
          }
          Here's an agent removal script:

          Code:
          <#
          
          Zabbix Agent Removal.
          
          Primary Steps in upgrade:
          1. Uninstall Zabbix Agent
          2. Remove service & Delete installation folder
          
          Additional notes:
          
          #>
          
          ###Variables###
          
          $InstallDestination = ($env:ProgramFiles)+"\Zabbix Agent"
          
          ###############
          
          #Stop Zabbix Service
          Stop-Service -Name 'Zabbix Agent' -ErrorAction SilentlyContinue
          
          #Uninstall Zabbix Agent
          $product_Management_Agent = Get-WmiObject win32_product | where{$_.name -eq "Zabbix Agent (64-bit)"}
          $product_Management_Agent.IdentifyingNumber
          Start-Process "C:\Windows\System32\msiexec.exe" `
          -ArgumentList "/x $($product_Management_Agent.IdentifyingNumber) /quiet"
          
          #Remove Zabbix Folder
          Remove-item $InstallDestination -Recurse -force
          
          #Remove Zabbix Service. Sometimes it gets abandoned which will fail the installation
          sc.exe delete "zabbix agent" -ErrorAction SilentlyContinue
          Last edited by Zablab; 10-03-2022, 23:22.

          Comment


          • Zablab
            Zablab commented
            Editing a comment
            Update: Sharing our full installation script for Zabbix wouldn't be helpful in this forum due to customization, but a snippet that might help in your own script include text replacement in config file:
            This is useful because after silent agent installation this updates the config file.

            If you utilize the Metadata line you can create some autoregistration actions and auto-assign groups, templates, etc. when the agent gets auto-created. There are other ways to do this, but finding this to be a decent solution for the time being.

            Code:
                (Get-Content "C:\Program Files\Zabbix Agent\zabbix_agentd.conf") | 
                    ForEach-Object { $_ -replace "###\s+Option: Include", "###  Option: Include`nInclude=C:\Program Files\Zabbix Agent\zabbix_agentd.d\*.conf" -replace "#\s+1\s-\sAllowKey", "AllowKey" -replace "#\s+LogRemoteCommands=0", "LogRemoteCommands=1" -replace "#\s+Timeout=30", "Timeout=30" -replace "#\s+UnsafeUserParameters=0", "UnsafeUserParameters=1" -replace "#\s+HostMetadata=", "# HostMetadata=`nHostMetadata=:Host=$($VMHost):ServerType=$($ZabbixServerType):"} | 
                    Set-Content "C:\Program Files\Zabbix Agent\zabbix_agentd.conf"
            Here's the overall installation cmd if you're having issues silent installing, replace variables with your desired variables:

            Code:
                Start-Process `
                    -FilePath msiexec.exe `
                    -ArgumentList "/i `"$localInstallerPath`" SERVER=$ZabbixServer SERVERACTIVE=$ZabbixServer ENABLEPATH=TRUE LOGTYPE=file /qn /quiet /passive" `
                    -Wait
        • xeroplasmic
          Junior Member
          • Jun 2022
          • 5

          #6
          Thanks for the Scripts, I'm about to try an deploy here too and This will help a lot =)

          Comment

          Working...