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
# DATE : 06.06.2018
# EDIT : 08.06.2018
# EDIT : 11.06.2018
# COMMENT :
#
#
# VERSION : 0.2
#
# CHANGELOG
# Version 0.3:
# Version 0.3: Added some improvements
<#
.NAME
Zabbix Agent Install.
......@@ -43,74 +43,38 @@ If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdent
}
#Importing module BitsTransfer for downloading zip archive with zabbix agent
Try
{
Import-Module BitsTransfer -ErrorAction stop
}
Catch
{
$ErrorMessage = $_.Exception.Message
write-host "Cannot import Bits module..."
write-host $ErrorMessage
Break
}
#Getting and defining some vars
#$urldistzabbix where your zabbix_agent archive
$urldistzabbix = "https://support.nugaev.net/files/distrib/zabbix_agent.zip"
#$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
###VARIABLES
# $UrlDistZabbix where your zabbix_agent archive
$UrlDistZabbix = "https://www.zabbix.com/downloads/3.4.6/zabbix_agents_3.4.6.win.zip"
#$UrlDistZabbix = "https://support.nugaev.net/files/distrib/zabbix_agent.zip"
# $MonitoringServer DNS Name of your Monitoring server
$MonitoringServer = "monitoring.nugaev.net"
# $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'
#$ZabbixConf zabbix agent config
# $ZabbixConf zabbix agent config
$ZabbixConf = $PathZabbix + '\conf\zabbix_agentd.win.conf'
#Your OS architechture
$OsArch = wmic os get OSArchitecture /value
#Getting monitoring server ip for Firewall Rule
$monserverip = (Test-Connection -ComputerName monitoring.nugaev.net -count 1).IPV4Address.ipaddressTOstring
#Downloading and unzip zabbix agent
Try
{
Start-BitsTransfer -Source $urldistzabbix -Destination $temparchive -ErrorAction stop
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
write-host "I cannot download the archive ((("
write-host $ErrorMessage
Break
}
#unzip it
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Unzip $temparchive $PathZabbix
Remove-Item -Force $temparchive
#Creating zabbix agent log folder
New-Item -ItemType directory -Path $PathZabbix\log
#$MonServerIp = [System.Net.Dns]::GetHostAddresses("$MonitoringServer").ipaddressTOstring
$MonServerIp = (Resolve-DNSName monitoring.nugaev.net).IP4Address
#Getting computer hostname for config file
$HostnameFull ="$env:computername.$env:userdnsdomain"
#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 = "
$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=monitoring.nugaev.net
ServerActive=monitoring.nugaev.net
Hostname=$HostnameFull
Server=$MonitoringServer
ServerActive=$MonitoringServer
Hostname=$CompHostName
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")
......@@ -122,56 +86,90 @@ else
$OsArch = 'win32'
}
#Downloading and unzip Zabbix Agent
Try
{
Start-BitsTransfer -Source $UrlDistZabbix -Destination $TempArchive -ErrorAction stop
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
write-host "Cannot download Zabbix Agent archive from link $UrlDistZabbix"
write-host $ErrorMessage
Break
}
#Unzip Zabbix Agent archive
Add-Type -AssemblyName System.IO.Compression.FileSystem
function UnZip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
UnZip $TempArchive $PathZabbix
Remove-Item -Force $TempArchive
#Creating Zabbix Agent log folder
New-Item -ItemType directory -Path $PathZabbix\log -ErrorAction Stop | out-null
#Setting up the content to the config file
Set-Content -Value "$ConfContent" -Path $ZabbixConf
#Defining zabbix install command. It depends on $OsArch variable
$ZabbixInstallCommand = "`"$PathZabbix\bin\$OsArch\zabbix_agentd.exe`" --config `"$ZabbixConf`""
#Installing Zabbix Service
Try
{
New-Service -Name "Zabbix Agent" -BinaryPathName $ZabbixInstallCommand `
-DisplayName "Zabbix Agent" -Description "Provides Monitoring" -StartupType "Automatic"
}
{
New-Service -Name "Zabbix Agent" -BinaryPathName $ZabbixInstallCommand `
-DisplayName "Zabbix Agent" -Description "Provides Monitoring" -StartupType "Automatic" | out-null
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
write-host "Cannot Install Zabbix Service ((("
write-host $ErrorMessage
Break
}
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
write-host "Cannot Install Zabbix Service!!!"
write-host $ErrorMessage
Break
}
#Starting zabbix agent
#Adding firewall rules for Zabbix Agent and Zabbix Server
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
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
write-host "Cannot Start Zabbix Service ((("
write-host "Cannot Add Firewall Rules..."
write-host $ErrorMessage
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
{
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
}
{
Start-Service -name "Zabbix Agent" -ErrorAction Stop
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
write-host "Cannot Add Firewall Rules..."
write-host $ErrorMessage
Break
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
write-host "Cannot Start Zabbix Service. See $PathZabbix\log\zabbix_agentd.log`
for details"
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..."
Write-host "Installation completed!"
\ No newline at end of file
Get-content $PathZabbix\log\zabbix_agentd.log
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