Le script ci-dessous affiche tout les hosts avec leur status et les templates associés
GetZabbixAlIHostsAndTemplates(Native_API).ps1 (2,46 kb)
### GET ALL ZABBIX HOSTS WITH THEIR STATUS AND TEMPLATES LINKED
Param(
[Parameter(Mandatory=$false)] $baseurl='https://MyZabbixSrv.My.domain/zabbix',
$credential = (Get-Credential -Credential "MyAccount"),
)
$global:JsonParams = @{}
#
Function ConnectZabbix($cred,$baseurl)
{
$JsonParams.body = @{
"jsonrpc"= "2.0"
"method"= "user.login"
"params"= @{
"user"= $cred.UserName
"password"= $cred.GetNetworkCredential().Password
}
"id"= 1
"auth"= $null
} | ConvertTo-Json
$JsonParams.uri = "$baseurl/api_jsonrpc.php"
$JsonParams.headers = @{"Content-Type" = "application/json"}
$JsonParams.method = "Post"
[System.Net.ServicePointManager]::SecurityProtocol = 'tls12'
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$global:Connresult = Invoke-WebRequest @JsonParams -UseBasicParsing
}
# Invoke ConnectZabbix
ConnectZabbix -cred $credential -baseurl $baseurl
## GET HOSTS ##
$JsonParams.body = @{
"jsonrpc"= "2.0"
"method"= "host.get"
"params"= @{
output = "hostid","name","available","status"
selectParentTemplates = "templateid","name"
}
auth = ($Connresult.Content | ConvertFrom-Json).result
id = 2
} | ConvertTo-Json
$ZabbixHosts = Invoke-WebRequest @JsonParams -UseBasicParsing
$ZabbixHosts = $ZabbixHosts.Content | ConvertFrom-Json
## CREATE EMPTY TABLEAU THAT WILL STORE HOSTS AND TEMPLATES
$HostsAndTemp = @()
foreach ($Zabbhost in $ZabbixHosts.result)
{
$obj = New-Object psobject
$obj | Add-Member -Name "HOSTNAME" -membertype Noteproperty -Value $Zabbhost.name
$obj | Add-Member -Name "ZBX AGENT STATUS" -membertype Noteproperty -Value `
$(
switch ($Zabbhost.status)
{
0 {"ENABLED"}
1 {"DISABLED"}
}
)
$obj | Add-Member -Name "ZBX AGENT AVAILABLE" -membertype Noteproperty -Value `
$(
switch ($Zabbhost.available)
{
1 {"AVAILABLE"}
2 {"UNAVAILABLE"}
0 {"UNKNOWN"}
}
)
$obj | Add-Member -Name "ZABBIX TEMPLATES LINKED" -membertype Noteproperty -Value $Zabbhost.parentTemplates.name
$HostsAndTemp += $obj
}
# TABLEAU FINAL
write-host "`n---- $($ZabbixHosts.result.Count) HOSTS ---`n"
write-host "HOSTNAME - ZBX AGENT STATUS - ZBX AGENT AVAILABLE - ZABBIX TEMPLATES LINKED`n"
$HostsAndTemp | sort hostname | ft -AutoSize