Search code examples
javaapizabbix

Rest Api Zabbix (method : item.get) how to get full names of the metrics


My method for obtaining id metrics from zabbix:

protected String getItemId(String host, String zabbixHostItemName) {
    JSONObject hostItemsFilter = new JSONObject();
    hostItemsFilter.put("name", new String[]{zabbixHostItemName});
    return connectZabbix.zabbixAPI.call(RequestBuilder.newBuilder()
            .method("item.get")
            .paramEntry("filter", hostItemsFilter)
            .paramEntry("host", host)
            .build()).getJSONArray("result").getJSONObject(0).getString("itemid");
}

What the following request body generates:

{
  "jsonrpc": "2.0",
  "method": "item.get",
  "params": {
    "filter": {
      "name": [
        "myItem"
      ]
    },
    "host": "myHost"
  }
}

It almost always works well.
The problem occurs when Zabbiks returns parameterized metric names. For example, if you request a metric:

Incomming network traffic on lan900

My method returns an error, because the data on the network interfaces parameterized.
If I request all the metrics on the host from the zabbix then for example the necessary "Incomming network traffic on" will match the name:

Incomming network traffic on $1

How to build a query that would find the itemid from the full name of the metric and host?


Solution

  • The current item API cannot expand macros automatically, it's a feature implementend for instance in the trigger API (expandComment, expandDescription, expandExpression).

    You can upvote this feature request.

    You can do a first query for "Incoming network traffic on $1", which will return an array of matching items, one for each network interface in your case.

    Then you can filter on the 'key_' field with the real interface name.

    A small python sample:

    f  = {  'name' : 'Incoming packet on $1'  }
    hostname = 'somehostname'
    
    itemObj = zapi.item.get(filter=f, host=hostname, output=['itemids', 'name', 'key_'] )
    
    for item in itemObj:
        if re.search('eth0', item['key_']):
            print item['itemid']