Home > Back-end >  Zabbix API with Powershell. How to get events with hostname
Zabbix API with Powershell. How to get events with hostname

Time:12-02

I'm trying to get event information using Zabbix API and PowerShell. Unfortunately, the result gives the host ID and not its name. The host name and its ID are obtained by another Zabbix API query.

How to get the answer in the event information to get the host name and not its ID. Script below:

#credentials
if(!$credential){
    $credential = Get-Credential
}


#zabbix adress
$baseurl = 'https://zabbix.sprawdzone.it'

#establish connection
$params = @{
    body =  @{
        "jsonrpc"= "2.0"
        "method"= "user.login"
        "params"= @{
            "user"= $credential.UserName
            "password"= $credential.GetNetworkCredential().Password
        }
        "id"= 2
        "auth"= $null
    } | ConvertTo-Json
    uri = "$baseurl/api_jsonrpc.php"
    headers = @{"Content-Type" = "application/json"}
    method = "Post"
}
$result = Invoke-WebRequest @params -UseBasicParsing

then I use the query for the hosts themselves:

#querying 4 host
$params.body = @{
    "jsonrpc" = "2.0"
    "method"= "host.get"
    "params"= @{
        output = @( "host", "hostid", "status" )
        selectInterfaces = @( "interfaceid", "ip", "dns", "useip" )
        groupids = @( "40")
    }
    auth = ($result.Content | ConvertFrom-Json).result
    id = 2
} | ConvertTo-Json

getting results:

#results
$result = Invoke-WebRequest @params -UseBasicParsing
$result.Content

Zabbix-api query for events:

#querying 4 events
$params.body = @{
    "jsonrpc" = "2.0"
    "method" = "event.get"
    "params" = @{
        "output" = "extend"
        "time_from" = "1638226800"
        "time_till" = "1638313200"
        "sortfield" = @("clock", "eventid")
        "sortorder" = "DESC"
    }
    auth = ($result.Content | ConvertFrom-Json).result
    id = 1
} | ConvertTo-Json

and the result is realized in the same way:

#results
$result = Invoke-WebRequest @params -UseBasicParsing
$result.Content

I guess I'm close to getting the hostname and event in one query - but I can't handle Zabbix-API. Or maybe there is an easier way?

And the result does not have to be JSON, it can be a table, string etc.

Thanks in advance for the hint.

CodePudding user response:

According to the API documentation, you can do a single event.get call with the selectHosts param:

Return a hosts property with hosts containing the object that created the event. Supported only for events generated by triggers, items or LLD rules.

Something like that:

$params.body = @{
    "jsonrpc" = "2.0"
    "method" = "event.get"
    "params" = @{
        "output" = "extend"
        "time_from" = "1638226800"
        "time_till" = "1638313200"
        "sortfield" = @("clock", "eventid")
        "sortorder" = "DESC"
        "selectHosts" = @("host", "hostid')
    }
    auth = ($result.Content | ConvertFrom-Json).result
    id = 1
} | ConvertTo-Json

CodePudding user response:

THX Simone Zabberoni! Works perfect!

  • Related