Last active
September 11, 2020 08:55
-
-
Save Satak/eeaa68c87057c2508a3e04fe817220ab to your computer and use it in GitHub Desktop.
Install and register Zabbix agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
$username = 'Admin', | |
$password = 'zabbix', | |
$zabbixHost = '10.0.0.2' | |
) | |
Import-Module .\PS-Zabbix-Host.psm1 -force | |
$credentials = New-Object System.Management.Automation.PSCredential ($userName, (ConvertTo-SecureString $password -AsPlainText -Force)) | |
Install-ZabbixAgent | |
$ip = Get-LocalIPAddress | |
$token = New-ZabbixToken -ZabbixHost $zabbixHost -Credentials $credentials | |
New-ZabbixHost -ZabbixHost $zabbixHost -Token $token -AgentIPAddress $ip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Install-ZabbixAgent { | |
<# | |
.SYNOPSIS | |
Install Zabbix agent for Windows | |
.DESCRIPTION | |
Downloads Zabbix agent and installs it for Windows x64 OS | |
.EXAMPLE | |
Install-ZabbixAgent | |
.PARAMETER ZabbixServerIPAddress | |
Your Zabbix Server IP Address, defaults to allow all connections | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[string]$ZabbixServerIPAddress = '0.0.0.0/0' | |
) | |
# download zabbix agent here C:\Users\<user>\AppData\Local\Temp\2\ZabbixAgent.msi | |
$outfile = Join-Path -Path $env:TEMP -ChildPath "ZabbixAgent.msi" | |
$logFile = Join-Path -Path $env:TEMP -ChildPath "ZabbixAgentInstallLog.txt" | |
$agentFileExist = Test-Path $outfile | |
$zabbixAgentInstalled = Get-Service 'Zabbix Agent' -ErrorAction SilentlyContinue | |
if (!$agentFileExist) { | |
Write-Verbose 'Downloading Zabbix agent 5.0.3 from www.zabbix.com' | |
Invoke-WebRequest -Uri "https://www.zabbix.com/downloads/5.0.3/zabbix_agent-5.0.3-windows-amd64-openssl.msi" -OutFile $outfile | |
} | |
if (!$zabbixAgentInstalled) { | |
Write-Verbose 'Installing Zabbix Agent' | |
Start-Process -FilePath $outfile -ArgumentList "/l*v $logFile SERVER=$ZabbixServerIPAddress /qb!" -PassThru -Wait | |
} | |
$agentIsRunning = Test-NetConnection localhost -Port 10050 -InformationLevel Quiet | |
Write-Verbose "Zabbix agent is downloaded and installed to $($env:computername). Agent is running on port 10050: $agentIsRunning" | |
} | |
function Get-LocalIPAddress { | |
( | |
Get-NetIPConfiguration | | |
Where-Object { | |
$null -ne $_.IPv4DefaultGateway -and | |
$_.NetAdapter.Status -ne "Disconnected" | |
} | |
).IPv4Address.IPAddress | |
} | |
function New-ZabbixToken { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string]$ZabbixHost, | |
[Parameter(Mandatory)] | |
[PSCredential]$Credentials | |
) | |
$ctype = "application/json" | |
$url = "http://$($ZabbixHost)/api_jsonrpc.php" | |
$loginBody = @{ | |
jsonrpc = "2.0" | |
method = "user.login" | |
id = 1 | |
auth = $null | |
params = @{ | |
user = $Credentials.UserName | |
password = $Credentials.GetNetworkCredential().Password | |
} | |
} | ConvertTo-Json | |
$login = Invoke-RestMethod -Uri $url -Body $loginBody -Method Post -ContentType $ctype | |
$token = $login.result | |
$token | |
} | |
function New-ZabbixHost { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string]$ZabbixHost, | |
[Parameter(Mandatory)] | |
[string]$Token, | |
[Parameter(Mandatory)] | |
[string]$AgentIPAddress | |
) | |
$ctype = "application/json" | |
$url = "http://$($ZabbixHost)/api_jsonrpc.php" | |
# 10081 = Template OS Windows by Zabbix agent | |
$templateId = 10081 | |
# 10 = Templates/Operating systems, 6 = Virtual machines | |
$groupId = 10 | |
# 10050 = default | |
$zabbixPort = "10050" | |
$createHostBody = @{ | |
jsonrpc = "2.0" | |
method = "host.create" | |
id = 1 | |
auth = $Token | |
params = @{ | |
host = $env:computername | |
interfaces = @( | |
@{type = 1 | |
main = 1 | |
useip = 1 | |
ip = $AgentIPAddress | |
dns = "" | |
port = $zabbixPort | |
} | |
) | |
groups = @( | |
@{groupid = $groupId } | |
) | |
templates = @( | |
@{templateid = $templateId } | |
) | |
} | |
} | ConvertTo-Json -Depth 3 | |
$res = Invoke-RestMethod -Uri $url -Body $createHostBody -Method Post -ContentType $ctype | |
$res.result.hostids | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment