这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.
2022 Zabbix中国峰会
2022 Zabbix中国峰会

检索模板

描述

integer/array template.get(object parameters)

该方法允许根据给定的参数检索模板。

任何类型的用户都可以使用此方法。可以在用户角色设置中撤销调用该方法的权限。详情请阅 User roles

参数

(object) 定义所需输出的参数。

该方法支持以下参数。

参数 类型 描述
templateids string/array 仅返回具有给定模板ID的模板。
groupids string/array 仅返回属于给定主机群组的模板。
parentTemplateids string/array 仅返回给定模板的父模板。
hostids string/array 仅返回链接到给定主机/模板的模板。
graphids string/array 仅返回包含给定图形的模板。
itemids string/array 仅返回包含给定监控项的模板。
triggerids string/array 仅返回包含给定触发器的模板。
with_items flag 仅返回包含监控项的模板。
with_triggers flag 仅返回具有触发器的模板。
with_graphs flag 仅返回包含图形的模板。
with_httptests flag 仅返回有web场景的模板。
evaltype integer 标签搜索规则。

可能的值:
0 - (默认值) And/Or;
2 - Or.
tags array/object 仅返回带有给定标签的模板。根据标签进行精确匹配,并根据运算符值按标签值进行区分大小写或不区分大小写的搜索。
Format: [{"tag": "<tag>", "value": "<value>", "operator": "<operator>"}, ...].
一个空数组返回所有模板。

可能的运算符值
0 - (默认值) Contains;
1 - Equals;
2 - Not like;
3 - Not equal
4 - Exists;
5 - Not exists.
selectGroups query 群组属性中返回模板所属的主机组。
selectTags query 标签属性中返回模板标签。
selectHosts query 主机 中返回链接到模板的主机。

支持 count.
selectTemplates query 返回 模版 中的子模板。

支持 count.
selectParentTemplates query 返回parentTemplates中的父模板

支持 count.
selectHttpTests query httpTests 属性中的模板返回web场景。

支持 count.
selectItems query 监控项属性中的模板返回监控项。

支持 count.
selectDiscoveries query discoveries属性中的模板返回底层自动发现。

支持 count.
selectTriggers query 触发器属性中的模板返回触发器。

支持 count.
selectGraphs query 图形属性中的模板返回图形。

支持 count.
selectMacros query macros属性中的模板返回宏..
selectDashboards query 仪表盘属性中的模板返回仪表板。

支持 count.
selectValueMaps query 返回一个带有模板值映射的值映射属性。
limitSelects integer 限制子选择返回的记录数。

适用于以下子选项:
selectTemplates - 结果将按name排序
selectHosts - 按host排序;
selectParentTemplates - 按host排序;
selectItems - 按name排序;
selectDiscoveries - 按name排序;
selectTriggers - 按description排序;
selectGraphs - 按name排序;
selectDashboards - 按name排序.
sortfield string/array 按给定属性对结果进行排序。

可能的值: hostid, host, name, status.
countOutput boolean 这些参数对于所有get方法都是通用的,在[参考注释]/manual/api/reference_commentary#common_get_method_parameters)中有详细描述。
editable boolean
excludeSearch boolean
filter object
limit integer
output query
preservekeys boolean
search object
searchByAny boolean
searchWildcardsEnabled boolean
sortorder string/array
startSearch boolean

返回值

(integer/array) 返回两者其中任一:

  • 一组对象数组;
  • 如果已经使用了countOutput参数,则检索对象的计数。

示例

按名称检索模版

检索有关名为“Linux”和“Windows”的两个模板的所有数据。

请求:

{
           "jsonrpc": "2.0",
           "method": "template.get",
           "params": {
               "output": "extend",
               "filter": {
                   "host": [
                       "Linux",
                       "Windows"
                   ]
               }
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "proxy_hostid": "0",
                   "host": "Linux",
                   "status": "3",
                   "disable_until": "0",
                   "error": "",
                   "available": "0",
                   "errors_from": "0",
                   "lastaccess": "0",
                   "ipmi_authtype": "0",
                   "ipmi_privilege": "2",
                   "ipmi_username": "",
                   "ipmi_password": "",
                   "ipmi_disable_until": "0",
                   "ipmi_available": "0",
                   "snmp_disable_until": "0",
                   "snmp_available": "0",
                   "maintenanceid": "0",
                   "maintenance_status": "0",
                   "maintenance_type": "0",
                   "maintenance_from": "0",
                   "ipmi_errors_from": "0",
                   "snmp_errors_from": "0",
                   "ipmi_error": "",
                   "snmp_error": "",
                   "jmx_disable_until": "0",
                   "jmx_available": "0",
                   "jmx_errors_from": "0",
                   "jmx_error": "",
                   "name": "Linux",
                   "flags": "0",
                   "templateid": "10001",
                   "description": "",
                   "tls_connect": "1",
                   "tls_accept": "1",
                   "tls_issuer": "",
                   "tls_subject": "",
                   "tls_psk_identity": "",
                   "tls_psk": "",
                   "uuid": "282ffe33afc74cccaf1524d9aa9dc502"
               },
               {
                   "proxy_hostid": "0",
                   "host": "Windows",
                   "status": "3",
                   "disable_until": "0",
                   "error": "",
                   "available": "0",
                   "errors_from": "0",
                   "lastaccess": "0",
                   "ipmi_authtype": "0",
                   "ipmi_privilege": "2",
                   "ipmi_username": "",
                   "ipmi_password": "",
                   "ipmi_disable_until": "0",
                   "ipmi_available": "0",
                   "snmp_disable_until": "0",
                   "snmp_available": "0",
                   "maintenanceid": "0",
                   "maintenance_status": "0",
                   "maintenance_type": "0",
                   "maintenance_from": "0",
                   "ipmi_errors_from": "0",
                   "snmp_errors_from": "0",
                   "ipmi_error": "",
                   "snmp_error": "",
                   "jmx_disable_until": "0",
                   "jmx_available": "0",
                   "jmx_errors_from": "0",
                   "jmx_error": "",
                   "name": "Windows",
                   "flags": "0",
                   "templateid": "10081",
                   "description": "",
                   "tls_connect": "1",
                   "tls_accept": "1",
                   "tls_issuer": "",
                   "tls_subject": "",
                   "tls_psk_identity": "",
                   "tls_psk": "",
                   "uuid": "522d17e1834049be879287b7c0518e5d"
               }
           ],
           "id": 1
       }

Retrieving template groups

Retrieve template groups that the template "Linux by Zabbix agent" is a member of.

Request:

{
           "jsonrpc": "2.0",
           "method": "template.get",
           "params": {
               "output": ["hostid"],
               "selectTemplateGroups": "extend",
               "filter": {
                   "host": [
                       "Linux by Zabbix agent"
                   ]
               }
           },
           "id": 1
       }

Response:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "templateid": "10001",
                   "templategroups": [
                       {
                           "groupid": "10",
                           "name": "Templates/Operating systems",
                           "uuid": "846977d1dfed4968bc5f8bdb363285bc"
                       }
                   ]
               }
           ],
           "id": 1
       }

Retrieving hosts by template

Retrieve hosts that have the "10001" (Linux by Zabbix agent) template linked to them.

Request:

{
           "jsonrpc": "2.0",
           "method": "template.get",
           "params": {
               "output": "templateid",
               "templateids": "10001",
               "selectHosts": ["hostid", "name"]
           },
           "id": 1
       }

Response:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "templateid": "10001",
                   "hosts": [
                       {
                           "hostid": "10084",
                           "name": "Zabbix server"
                       },
                       {
                           "hostid": "10603",
                           "name": "Host 1"
                       },
                       {
                           "hostid": "10604",
                           "name": "Host 2"
                       }
                   ]
               }
           ],
           "id": 1
       }

按模板标签搜索

检索标签“主机名称”等于“{HOST.NAME}”的模板。

请求:

{
           "jsonrpc": "2.0",
           "method": "template.get",
           "params": {
               "output": ["hostid"],
               "selectTags": "extend",
               "evaltype": 0,
               "tags": [
                   {
                       "tag": "Host name",
                       "value": "{HOST.NAME}",
                       "operator": 1
                   }
               ]
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10402",
                   "tags": [
                       {
                           "tag": "Host name",
                           "value": "{HOST.NAME}"
                       }
                   ]
               }
           ],
           "id": 1
       }

参阅

源码

CTemplate::get() in ui/include/classes/api/services/CTemplate.php.