PI Services

Le blog des collaborateurs de PI Services

ZABBIX - Script - Liste des items d'un template (Native API)

Le script ci-dessous interroge l'api native zabbix pour récupérer le détail de la liste des items d'un nom de template donné en paramètre.

 

ZabbixGetItemsOfSpecTemplate(Native_API).ps1 (2,29 kb)

 

### Get Items of a specific template name (Use native zabbix json api)
Param(
[Parameter(Mandatory=$false)] $baseurl='https://MyZabbixSrv.My.domain/zabbix',
$credential = (Get-Credential -Credential "MyAccount"),
$TemplateName = "MY_TEMPLATE"
)

$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 TEMPLATE ID ##
Function GetTemplateId($TemplateName)
{
$JsonParams.body = @{
    "jsonrpc"= "2.0"
    "method"= "template.get"
    "params"= @{
       output = "extend"
        "filter" = @{"host"=$TemplateName}
        }
    auth = ($Connresult.Content | ConvertFrom-Json).result
    id = 2
} | ConvertTo-Json

$result = Invoke-WebRequest @JsonParams -UseBasicParsing
$result = $result.Content | ConvertFrom-Json

$TemplateId = $result.result | select templateid -ExpandProperty templateid
$TemplateId
}



## GET ITEMS FROM TEMPLATE ID ##
$JsonParams.body = @{
    "jsonrpc"= "2.0"
    "method"= "item.get"
    "params"= @{
        output = "extend"
        templateids = $(GetTemplateId -TemplateName $TemplateName)
        }
    auth = ($Connresult.Content | ConvertFrom-Json).result
    id = 2
} | ConvertTo-Json

$result = Invoke-WebRequest @JsonParams -UseBasicParsing
$result = $result.Content | ConvertFrom-Json


write-host "`n############################################################################"
write-host "## ZABBIX ITEMS IN $TemplateName ##:"
write-host "#############################################################################`n"


# Display Result tableau
$result.result | ft