【问题标题】:Zabbix API with Powershell. How to get events with hostname带有 Powershell 的 Zabbix API。如何使用主机名获取事件
【发布时间】:2021-11-30 22:59:46
【问题描述】:

我正在尝试使用 Zabbix API 和 PowerShell 获取事件信息。不幸的是,结果给出了主机 ID 而不是它的名称。主机名及其 ID 由另一个 Zabbix API 查询获得。

如何在事件信息中获取答案以获取主机名而不是其 ID。脚本如下:

#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

然后我使用主机本身的查询:

#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

得到结果:

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

Zabbix-api 事件查询:

#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

结果也是这样实现的:

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

我想我已经接近在一次查询中获取主机名和事件 - 但我无法处理 Zabbix-API。或者也许有更简单的方法?

而且结果不一定是JSON,可以是表格、字符串等

提前感谢您的提示。

【问题讨论】:

    标签: powershell zabbix zabbix-api


    【解决方案1】:

    根据API documentation,您可以使用selectHosts 参数进行单个event.get 调用:

    返回 hosts 属性,其中 hosts 包含创建的对象 事件。仅支持由触发器、项目或 LLD 规则。

    类似的东西:

    $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
    

    【讨论】:

      【解决方案2】:

      THX Simone Zabberoni!完美运行!

      【讨论】:

        猜你喜欢
        • 2015-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多