Ad Widget

Collapse

API: Get hosts linked to a template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuo
    Junior Member
    • Feb 2010
    • 18

    #1

    API: Get hosts linked to a template

    Hello community,

    I am currently stuck at a problem I can't seem to solve.

    I need to get all hosts which are linked to a template.
    I can query the template fine, I can query hosts based on other filters fine, but I can't get it to work with template or group ids.

    I am using Andrew Farleys PHP class (kudos to Mr. Farley!), the JSON Request it creates looks like this:

    PHP Code:
    Content-Typeapplication/json-rpc [1] => User-AgentZabbixAPI v1.0 http://andrewfarley.com/zabbix_php_api ) [10036] => POST [10015] => {"auth":"5eea9eee2c313eb7b20fceff058f12c0","method":"host.get","id":1,"params":{"extendoutput":1,"filter":{"templateids":["10053"]}},"jsonrpc":"2.0" 
    The template id is definitely correct. This is how I call it from PHP:

    PHP Code:
    $hosts ZabbixAPI::fetch_array('host','get',array('extendoutput'=>1'filter'=>array('templateids' => array($template[0]["templateid"])))); 
    It always returns all hosts, as if I have given an empty filter. What am I doing wrong?

    I also tried:

    PHP Code:
    $hosts ZabbixAPI::fetch_array('host','get',array('extendoutput'=>1'filter'=>array('templateids' => array("templateid" => $template[0]["templateid"])))); 
    without success.

    Any help is greatly appreciated!

    regards

    tuo
  • tuo
    Junior Member
    • Feb 2010
    • 18

    #2
    I hate to bump threads, but this problem currently sadly is a big show stopper for me, as I can't progress any further.

    Has anyone ever faced (and solved) a similar problem?

    best regards

    tuo

    Comment

    • zerstroer
      Junior Member
      • Jul 2009
      • 22

      #3
      Solution!

      You should use "selectHosts" parameter in the method "template.get" for get all hosts linked with your template.

      Comment

      • BigSmooth
        Member
        • Jun 2023
        • 46

        #4
        Hello,

        would you have an example of selectHosts usage?

        Comment

        • dimir
          Zabbix developer
          • Apr 2011
          • 1080

          #5
          Hosts of a single template

          Request:
          Code:
          {
              "jsonrpc": "2.0",
              "method": "template.get",
              "params": {
                  "output": ["templateid"],
                  "selectHosts": ["hostid", "name"],
                  "templateids": ["10047"]
              },
              "auth": "{{token}}",
              "id": 1
          }
          Response:
          Code:
          {
            "jsonrpc": "2.0",
            "result": [
              {
                "templateid": "10047",
                "hosts": [
                  {
                    "hostid": "10084",
                    "name": "Zabbix server"
                  },
                  [...]
                ]
              }
            ],
            "id": 1
          }
          You can specify more fields to return for each host:
          Code:
             "params": {
                "selectHosts": ["hostid", "host", "name", "description", "status"],
                [...]
             }
          or even tell the API to return all known fields of the host:
          Code:
             "params": {
                "selectHosts": "extend"
                [...]
          ​   }

          Hosts of all templates in the system​

          Request:
          Code:
          {
              "jsonrpc": "2.0",
              "method": "template.get",
              "params": {
                  "output": ["templateid"],
                  "selectHosts": ["hostid", "name"]
              },
              "auth": "{{token}}",
              "id": 1
          }
          Response:
          Code:
          {
            "jsonrpc": "2.0",
            "result": [
              {
                "templateid": "10001",
                "hosts": [
                  {
                    "hostid": "10084",
                    "name": "Zabbix server"
                  },
                  [...]
                ]
              },
              {
                "templateid": "10047",
                "hosts": [
                  {
                    "hostid": "10084",
                    "name": "Zabbix server"
                  },
                  [...]
                ]
              }
            ]
          }

          Comment

          • dimir
            Zabbix developer
            • Apr 2011
            • 1080

            #6
            The examples in documentation were updated. Please check if makes more sense now:


            Comment

            Working...