From 2c1f9fcdf8c670ae2491e6d6d81198fe8645e79a Mon Sep 17 00:00:00 2001 From: Andreas Lindemark <andreas.lindemark@liu.se> Date: Thu, 3 Apr 2025 18:32:44 +0200 Subject: [PATCH] Added support for custom config files. Added check to compare source and target files Added code to handle if any source files is missing (for example if the network is down) to start service if not running. --- .../ZabbixAgent/CheckAndStartZabbixAgent2.ps1 | 66 ++++++++++++++++--- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 index 55098bf..ce6e608 100644 --- a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 +++ b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 @@ -1,10 +1,15 @@ # Retrieve the domain name dynamically $domainName = (Get-CimInstance -ClassName Win32_ComputerSystem).Domain -# Path to the custom configuration file on SYSVOL using the dynamic domain name -$customConfigPath = "\\$domainName\SYSVOL\$domainName\Configs\zabbix_agent2.conf" +# Get computer name +$computerName = $env:COMPUTERNAME -# Path to the Zabbix Agent 2 configuration file +# Path to the configuration folder and files +$customConfigPath = "\\$domainName\SYSVOL\$domainName\Configs" +$customConfigFile = "$customConfigPath\zabbix_agent2_$computerName.conf" +$defaultConfigFile = "$customConfigPath\zabbix_agent2.conf" + +# Path to the local Zabbix Agent 2 configuration file $agentConfigPath = "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf" # Function to test if the Zabbix Agent 2 service is running @@ -21,13 +26,13 @@ function Test-ZabbixAgent2 { } } -# Function to copy the custom configuration file +# Function to copy the configuration file function Copy-ZabbixConfig { - Write-Output "Copying custom configuration file..." - Write-Output " - Source: $customConfigPath" + Write-Output "Copying configuration file..." + Write-Output " - Source: $customConfigFile" Write-Output " - Destination: $agentConfigPath" try { - Copy-Item -Path $customConfigPath -Destination $agentConfigPath -Force + Copy-Item -Path $customConfigFile -Destination $agentConfigPath -Force Write-Output "Configuration file copied successfully." } catch { Write-Output "Failed to copy configuration file: $_" @@ -45,12 +50,55 @@ function Start-ZabbixAgent2 { } } +# Function to stop the Zabbix Agent 2 service +function Stop-ZabbixAgent2 { + Write-Output "Stopping Zabbix Agent 2 service..." + try { + Stop-Service -Name "Zabbix Agent 2" -Force + Write-Output "Zabbix Agent 2 service stopped successfully." + } catch { + Write-Output "Failed to stop Zabbix Agent 2 service: $_" + } +} + # Main script logic +# Check if custom config file exists, otherwise use default +if (-Not (Test-Path $customConfigFile)) { + if (-Not (Test-Path $defaultConfigFile)) { + Write-Output "Neither custom nor default configuration file exists." + # Check if the service is running + $serviceRunning = Test-ZabbixAgent2 + Write-Output "Service running status: $serviceRunning" + if ($serviceRunning) { + Write-Output "Exiting script." + exit + } else { + Start-ZabbixAgent2 + Write-Output "Exiting script." + exit + } + } else { + $customConfigFile = $defaultConfigFile + } +} + +# Check if Zabbix Agent 2 service is running $serviceRunning = Test-ZabbixAgent2 Write-Output "Service running status: $serviceRunning" -if (-not $serviceRunning) { + +if (-Not $serviceRunning) { + # Service is not running, copy config file and start service Copy-ZabbixConfig Start-ZabbixAgent2 } else { - Write-Output "Zabbix Agent 2 is already running. No action needed." + # Service is running, compare config files + if ((Get-Content $agentConfigPath -Raw) -eq (Get-Content $customConfigFile -Raw)) { + Write-Output "Configuration files are identical. No action needed." + } else { + Write-Output "Configuration files do not match. Updating configuration..." + # Files are different, stop service, copy config file, and start service + Stop-ZabbixAgent2 + Copy-ZabbixConfig + Start-ZabbixAgent2 + } } \ No newline at end of file -- GitLab