Ad Widget

Collapse

Querying Zabbix API (4.4 instances) for hosts by tag name and tag value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kbcmdba
    Junior Member
    • Oct 2021
    • 2

    #1

    Querying Zabbix API (4.4 instances) for hosts by tag name and tag value

    I've been attempting to read through the documentation to figure out how I can use the API to poll a list of servers that fall into a particular class. In this case, we use the tag name ServerClass and tag value of MySQL. Can someone please help me out with an example and put it into the docs? I hope this solution will help others. :-)

    Code:
    $ cat test9.sh
    #!/bin/bash
    
    source auth.sh
    
    read -r -d '' DATA <<EOF
    {
      "auth": $AUTH,
      "jsonrpc": "2.0",
      "method": "host.get",
      "params": {
        "output":["hostid","host"],
        "selectTags": ["tag", "value"],
        "selectInheritedTags": ["tag", "value"],
        "tags": [{"tag": "ServerClass", "value": "MySQL"}]
      },
      "id": 1
    }
    EOF
    echo "DATA=$DATA"
    echo -e '------------------------------------------------------------------------'
    curl -Lk -s -X POST -H 'Content-Type:application/json' -d "$DATA" $API_URL | jq '.result'
    $ ./test9.sh
    DATA={
      "auth": "21...e5",
      "jsonrpc": "2.0",
      "method": "host.get",
      "params": {
        "output":["hostid","host"],
        "selectTags": ["tag", "value"],
        "selectInheritedTags": ["tag", "value"],
        "tags": [{"tag": "ServerClass", "value": "MySQL"}]
      },
      "id": 1
    }
    ------------------------------------------------------------------------
    ... returns *way* too many hosts. I want to limit it to just servers with the tag or inherited tag.
    Last edited by kbcmdba; 28-10-2021, 18:35. Reason: Extra , after "tags" value interpreted as a syntax error but that's debatable.
  • kbcmdba
    Junior Member
    • Oct 2021
    • 2

    #2
    Following up - the tags I'm looking for are implemented both at the Host and the Template level. I'm having no difficulty with host-level tags, but getting parent tags to automatically include matching hosts is a problem.

    Comment

    Working...