Commit c81b68e9 by Rinat K. Nugaev

Final version of zabbix_install script

parent 2ef8b007
# AUTHOR : Rinat K. Nugaev - http://www.nugaev.net - code@nugaev.net # AUTHOR : Rinat K. Nugaev - http://www.nugaev.net - code@nugaev.net
# DATE : 06.06.2018 # DATE : 06.06.2018
# EDIT : 08.06.2018 # EDIT : 11.06.2018
# COMMENT : # COMMENT :
# #
# #
# VERSION : 0.2 # VERSION : 0.2
# #
# CHANGELOG # CHANGELOG
# Version 0.3: # Version 0.3: Added some improvements
<# <#
.NAME .NAME
Zabbix Agent Install. Zabbix Agent Install.
...@@ -43,135 +43,133 @@ If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdent ...@@ -43,135 +43,133 @@ If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdent
} }
#Importing module BitsTransfer for downloading zip archive with zabbix agent #Importing module BitsTransfer for downloading zip archive with zabbix agent
Try
{
Import-Module BitsTransfer -ErrorAction stop Import-Module BitsTransfer -ErrorAction stop
} ###VARIABLES
Catch # $UrlDistZabbix where your zabbix_agent archive
{ $UrlDistZabbix = "https://www.zabbix.com/downloads/3.4.6/zabbix_agents_3.4.6.win.zip"
$ErrorMessage = $_.Exception.Message #$UrlDistZabbix = "https://support.nugaev.net/files/distrib/zabbix_agent.zip"
write-host "Cannot import Bits module..."
write-host $ErrorMessage # $MonitoringServer DNS Name of your Monitoring server
Break $MonitoringServer = "monitoring.nugaev.net"
}
#Getting and defining some vars # $temparchivedir where you'll download zabbix_agent archive
#$urldistzabbix where your zabbix_agent archive $TempArchive = "$env:TEMP\zabbix_agent.zip"
$urldistzabbix = "https://support.nugaev.net/files/distrib/zabbix_agent.zip" # $PathZabbix folder where you'll install zabbix agent
#$urldistzabbix = "https://www.zabbix.com/downloads/3.4.6/zabbix_agents_3.4.6.win.zip"
#$temparchivedir where you'll download zabbix_agent archive
$temparchive = "$env:TEMP\zabbix_agent.zip"
#$PathZabbix folder where you'll install zabbix agent
$PathZabbix = ${env:ProgramFiles} + '\Zabbix' $PathZabbix = ${env:ProgramFiles} + '\Zabbix'
#$ZabbixConf zabbix agent config # $ZabbixConf zabbix agent config
$ZabbixConf = $PathZabbix + '\conf\zabbix_agentd.win.conf' $ZabbixConf = $PathZabbix + '\conf\zabbix_agentd.win.conf'
#Your OS architechture
$OsArch = wmic os get OSArchitecture /value
#Getting monitoring server ip for Firewall Rule #Getting monitoring server ip for Firewall Rule
$monserverip = (Test-Connection -ComputerName monitoring.nugaev.net -count 1).IPV4Address.ipaddressTOstring
#Downloading and unzip zabbix agent #$MonServerIp = [System.Net.Dns]::GetHostAddresses("$MonitoringServer").ipaddressTOstring
$MonServerIp = (Resolve-DNSName monitoring.nugaev.net).IP4Address
#Getting computer hostname for config file
$CompHostName ="$env:computername.$env:userdnsdomain"
#Removing dot from end of the hostname (dot appears when computer is not a domain member)
$CompHostName = $CompHostName -replace "\.(?<=\z)", ""
###Creating config file
#Preparing content for config file
$ConfContent = "
LogFile=$PathZabbix\log\zabbix_agentd.log
Server=$MonitoringServer
ServerActive=$MonitoringServer
Hostname=$CompHostName
HostMetadataItem=system.uname
"
#Defining $OsArch variable. It depend on system platform, 64 or 32 bits
$OsArch = wmic os get OSArchitecture /value
if ($OsArch -eq "OSArchitecture=64-bit")
{
$OsArch = 'win64'
}
else
{
$OsArch = 'win32'
}
#Downloading and unzip Zabbix Agent
Try Try
{ {
Start-BitsTransfer -Source $urldistzabbix -Destination $temparchive -ErrorAction stop Start-BitsTransfer -Source $UrlDistZabbix -Destination $TempArchive -ErrorAction stop
} }
Catch Catch
{ {
$ErrorMessage = $_.Exception.Message $ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName $FailedItem = $_.Exception.ItemName
write-host "I cannot download the archive (((" write-host "Cannot download Zabbix Agent archive from link $UrlDistZabbix"
write-host $ErrorMessage write-host $ErrorMessage
Break Break
} }
#unzip it #Unzip Zabbix Agent archive
Add-Type -AssemblyName System.IO.Compression.FileSystem Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip function UnZip
{ {
param([string]$zipfile, [string]$outpath) param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath) [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
} }
Unzip $temparchive $PathZabbix UnZip $TempArchive $PathZabbix
Remove-Item -Force $temparchive Remove-Item -Force $TempArchive
#Creating zabbix agent log folder #Creating Zabbix Agent log folder
New-Item -ItemType directory -Path $PathZabbix\log New-Item -ItemType directory -Path $PathZabbix\log -ErrorAction Stop | out-null
#Getting computer hostname for config file #Setting up the content to the config file
$HostnameFull ="$env:computername.$env:userdnsdomain" Set-Content -Value "$ConfContent" -Path $ZabbixConf
#Removing dot from end of the hostname (this behavior possible if computer is not domain joined)
$HostnameFull = $HostnameFull -replace "\.(?<=\z)", ""
#Creating config
#Preparing content for config gile
$confcont = "
LogFile=$PathZabbix\log\zabbix_agentd.log
Server=monitoring.nugaev.net
ServerActive=monitoring.nugaev.net
Hostname=$HostnameFull
HostMetadataItem=system.uname
"
#Adding content to the config file
Set-Content -Value "$confcont" -Path $ZabbixConf
#Defining $OsArch variable. It depend on system platform, 64 or 32 bits
$OsArch = wmic os get OSArchitecture /value
if ($OsArch -eq "OSArchitecture=64-bit")
{
$OsArch = 'win64'
}
else
{
$OsArch = 'win32'
}
#Defining zabbix install command. It depends on $OsArch variable #Defining zabbix install command. It depends on $OsArch variable
$ZabbixInstallCommand = "`"$PathZabbix\bin\$OsArch\zabbix_agentd.exe`" --config `"$ZabbixConf`"" $ZabbixInstallCommand = "`"$PathZabbix\bin\$OsArch\zabbix_agentd.exe`" --config `"$ZabbixConf`""
#Installing Zabbix Service #Installing Zabbix Service
Try Try
{ {
New-Service -Name "Zabbix Agent" -BinaryPathName $ZabbixInstallCommand ` New-Service -Name "Zabbix Agent" -BinaryPathName $ZabbixInstallCommand `
-DisplayName "Zabbix Agent" -Description "Provides Monitoring" -StartupType "Automatic" -DisplayName "Zabbix Agent" -Description "Provides Monitoring" -StartupType "Automatic" | out-null
} }
Catch Catch
{ {
$ErrorMessage = $_.Exception.Message $ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName $FailedItem = $_.Exception.ItemName
write-host "Cannot Install Zabbix Service (((" write-host "Cannot Install Zabbix Service!!!"
write-host $ErrorMessage write-host $ErrorMessage
Break Break
} }
#Starting zabbix agent #Adding firewall rules for Zabbix Agent and Zabbix Server
Try Try
{ {
start-service -name "Zabbix Agent" -ErrorAction Stop New-NetFirewallRule -DisplayName "Zabbix Agent IN" -RemoteAddress $MonServerIp `
-Profile Any -Action Allow -Direction Inbound -Protocol TCP -LocalPort 10050 | out-null
New-NetFirewallRule -DisplayName "Zabbix Agent OUT" -RemoteAddress $MonServerIp `
-Profile Any -Action Allow -Direction OutBound -Protocol TCP -LocalPort 10051 | out-null
} }
Catch Catch
{ {
$ErrorMessage = $_.Exception.Message $ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName $FailedItem = $_.Exception.ItemName
write-host "Cannot Start Zabbix Service (((" write-host "Cannot Add Firewall Rules..."
write-host $ErrorMessage write-host $ErrorMessage
Break Break
} }
#Dont remember why but entering to the temporary directory
cd ${ENV:temp}
#Adding firewall rules for zabbix agent inbound for zabbix server and outbound for agent #Starting Zabbix Agent
Try Try
{ {
New-NetFirewallRule -DisplayName "Zabbix Agent IN" -RemoteAddress $monserverip ` Start-Service -name "Zabbix Agent" -ErrorAction Stop
-Profile Any -Action Allow -Direction Inbound -Protocol TCP -LocalPort 10050 | out-null
New-NetFirewallRule -DisplayName "Zabbix Agent OUT" -RemoteAddress $monserverip ` }
-Profile Any -Action Allow -Direction OutBound -Protocol TCP -LocalPort 10051 | out-null
}
Catch Catch
{ {
$ErrorMessage = $_.Exception.Message $ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName $FailedItem = $_.Exception.ItemName
write-host "Cannot Add Firewall Rules..." write-host "Cannot Start Zabbix Service. See $PathZabbix\log\zabbix_agentd.log`
write-host $ErrorMessage for details"
Break write-host $ErrorMessage
Break
}
For ($i=5; $i -gt 1; $i-) {
Write-Progress -Activity "`nInstallation completed! Waiting Zabbix Agent starts..." `
-SecondsRemaining $i
Start-Sleep 1
} }
Write-host "Firewall rules for Zabbix Agent added..." Get-content $PathZabbix\log\zabbix_agentd.log
Write-host "Installation completed!"
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment