host.get

描述

integer/array host.get(object parameters)

该方法允许根据给定参数检索主机。

此方法可供任何类型的用户使用。调用该方法的权限可以在用户角色设置中撤销。更多信息请参阅User roles

参数

(object) 定义期望输出的参数。

该方法支持以下参数。

参数 数据类型 描述
groupids string/array 仅返回属于指定组的主机。
dserviceids string/array 仅返回与指定发现服务相关的主机。
graphids string/array 仅返回包含指定图形的主机。
hostids string/array 仅返回具有指定主机 ID的主机。
httptestids string/array 仅返回包含指定Web检查的主机。
interfaceids string/array 仅返回使用指定接口的主机。
itemids string/array 仅返回具有指定监控项的主机。
maintenanceids string/array 仅返回受指定维护影响的主机。
monitored_hosts flag 仅返回受监控的主机。
proxy_hosts flag 仅返回proxies。
proxyids string/array 仅返回由指定proxies监控的主机。
templated_hosts flag 同时返回主机和模板。
templateids string/array 仅返回链接到指定模板的主机。
triggerids string/array 仅返回具有指定触发器的主机。
with_items flag 仅返回具有监控项的主机。

覆盖with_monitored_itemswith_simple_graph_items参数。
with_item_prototypes flag 仅返回具有监控项原型的主机。

覆盖with_simple_graph_item_prototypes参数。
with_simple_graph_item_prototypes flag 仅返回具有监控项原型的主机,这些监控项原型已启用创建且具有数字类型信息。
with_graphs flag 仅返回具有图形的主机。
with_graph_prototypes flag 仅返回具有图形原型的主机。
with_httptests flag 仅返回具有Web检查的主机。

覆盖with_monitored_httptests参数。
with_monitored_httptests flag 仅返回具有启用Web检查的主机。
with_monitored_items flag 仅返回具有启用监控项的主机。

覆盖with_simple_graph_items参数。
with_monitored_triggers flag 仅返回具有启用触发器的主机。触发器使用的所有监控项也必须启用。
with_simple_graph_items flag 仅返回具有数字类型信息监控项的主机。
with_triggers flag 仅返回具有触发器的主机。

覆盖with_monitored_triggers参数。
withProblemsSuppressed boolean 返回具有被抑制问题的主机。

可能的值:
null - (默认)所有主机;
true - 仅具有被抑制问题的主机;
false - 仅具有未被抑制问题的主机。
evaltype integer 标签搜索规则。

可能的值:
0 - (默认)与/或;
2 - 或。
severities integer/array 返回仅具有指定严重性问题的主机。仅当问题object为触发器时适用。
tags object/array 仅返回具有指定标签的主机。根据操作符值,对标签进行精确匹配或对标签值进行大小写敏感/不敏感搜索。
格式:[{"tag": "<tag>", "value": "<value>", "operator": "<operator>"}, ...]
空array返回所有主机。

可能的操作符值:
0 - (默认)包含;
1 - 等于;
2 - 不包含;
3 - 不等于;
4 - 存在;
5 - 不存在。
inheritedTags boolean 返回主机,其所有链接模板也具有指定的tags。默认:

可能的值:
true - 链接模板也必须具有指定的tags
false - (默认)忽略链接模板标签。
selectDiscoveries query 返回一个discoveries属性,包含主机低级发现规则。

支持count
selectDiscoveryRule query 返回一个discoveryRule属性,包含创建主机的低级发现规则(来自VMware监控中的主机原型)。
selectGraphs query 返回一个graphs属性,包含主机图形。

支持count
selectGroups query 返回一个groups属性,包含主机所属的组数据。
selectHostDiscovery query 返回一个hostDiscovery属性,包含主机发现object数据。

主机发现object将发现的主机链接到主机原型或将主机原型链接到LLD规则,并具有以下属性:
host - (string) 主机原型的主机;
hostid - (string) 发现的主机或主机原型的ID;
parent_hostid - (string) 创建主机的主机原型的ID;
parent_itemid - (string) 创建发现的主机的LLD规则的ID;
lastcheck - (时间戳) 主机上次被发现的时间;
ts_delete - (时间戳) 不再被发现的主机将被删除的时间。
selectHttpTests query 返回一个httpTests属性,包含主机Web场景。

支持count
selectInterfaces query 返回一个interfaces属性,包含主机接口。

支持count
selectInventory query 返回一个主机资产清单属性,包含主机库存数据。
selectItems query 返回一个items属性,包含主机监控项。

支持count
selectMacros query 返回一个macros属性,包含主机宏。
selectParentTemplates query 返回一个parentTemplates属性,包含主机链接到的模板。

支持count
selectDashboards query 返回一个dashboards属性。

支持count
selectTags query 返回一个主机标签属性,包含主机标签。
selectInheritedTags query 返回一个主机标签属性,包含链接到主机的所有模板上的标签。
selectTriggers query 返回一个triggers属性,包含主机触发器。

支持count
selectValueMaps query 返回一个valuemaps属性,包含主机值映射。
filter object 仅返回与给定过滤器完全匹配的结果。

接受一个array,其中键是属性名称,值是要匹配的单个值或array。

允许按接口属性过滤。不适用于text字段。
limitSelects integer 限制子查询返回的记录数。

适用于以下子查询:
selectParentTemplates - 结果将按host排序;
selectInterfaces
selectItems - 按name排序;
selectDiscoveries - 按name排序;
selectTriggers - 按description排序;
selectGraphs - 按name排序;
selectDashboards - 按name排序。
search object 返回与给定模式匹配的结果(不区分大小写)。

接受一个array,其中键是属性名称,值是要搜索的字符串。如果未提供其他选项,将执行LIKE "%…%"搜索。

允许按接口属性搜索。仅适用于stringtext字段。
searchInventory object 仅返回库存数据与给定通配符搜索匹配的主机。

此参数受与search相同的附加参数影响。
sortfield string/array 按给定属性排序结果。

可能的值为:hostidhostnamestatus
countOutput boolean 这些参数在通用get方法参数中描述。
editable boolean
excludeSearch boolean
limit integer
output query
preservekeys boolean
searchByAny boolean
searchWildcardsEnabled boolean
sortorder string/array
startSearch boolean

返回值

(integer/array) 返回以下两种结果之一:

  • 一个包含objects的array;
  • 若使用了countOutput参数,则返回检索到的objects数量。

示例

按名称检索数据

获取两个名为"Zabbix server"和"Linux服务器"的主机的所有数据。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "filter": {
                   "host": [
                       "Zabbix server",
                       "Linux server"
                   ]
               }
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10160",
                   "proxy_hostid": "0",
                   "host": "Zabbix server",
                   "status": "0",
                   "lastaccess": "0",
                   "ipmi_authtype": "-1",
                   "ipmi_privilege": "2",
                   "ipmi_username": "",
                   "ipmi_password": "",
                   "maintenanceid": "0",
                   "maintenance_status": "0",
                   "maintenance_type": "0",
                   "maintenance_from": "0",
                   "name": "Zabbix server",
                   "flags": "0",
                   "description": "The Zabbix monitoring server.",
                   "tls_connect": "1",
                   "tls_accept": "1",
                   "tls_issuer": "",
                   "tls_subject": "",
                   "inventory_mode": "1"
               },
               {
                   "hostid": "10167",
                   "proxy_hostid": "0",
                   "host": "Linux server",
                   "status": "0",
                   "lastaccess": "0",
                   "ipmi_authtype": "-1",
                   "ipmi_privilege": "2",
                   "ipmi_username": "",
                   "ipmi_password": "",
                   "maintenanceid": "0",
                   "maintenance_status": "0",
                   "maintenance_type": "0",
                   "maintenance_from": "0",
                   "name": "Linux server",
                   "flags": "0",
                   "description": "",
                   "tls_connect": "1",
                   "tls_accept": "1",
                   "tls_issuer": "",
                   "tls_subject": "",
                   "inventory_mode": "1"
               }
           ],
           "id": 1
       }

检索主机组

获取主机 "Zabbix server"所属的组名称,但不包含主机本身的详细信息。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["hostid"],
               "selectGroups": "extend",
               "filter": {
                   "host": [
                       "Zabbix server"
                   ]
               }
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 2
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10085",
                   "groups": [
                       {
                           "groupid": "2",
                           "name": "Linux servers",
                           "internal": "0",
                           "flags": "0"
                       },
                       {
                           "groupid": "4",
                           "name": "Zabbix servers",
                           "internal": "0",
                           "flags": "0"
                       }
                   ]
               }
           ],
           "id": 2
       }

检索关联模板

获取链接到主机 "10084"的模板ID和名称。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["hostid"],
               "selectParentTemplates": [
                   "templateid",
                   "name"
               ],
               "hostids": "10084"
           },
           "id": 1,
           "auth": "70785d2b494a7302309b48afcdb3a401"
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10084",
                   "parentTemplates": [
                       {
                           "name": "Linux",
                           "templateid": "10001"
                       },
                       {
                           "name": "Zabbix Server",
                           "templateid": "10047"
                       }
                   ]
               }
           ],
           "id": 1
       }

通过模板检索主机

获取已关联"10001"(由Zabbix agent提供的Linux)模板的主机。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["hostid", "name"],
               "templateids": "10001"
           },
           "auth": "70785d2b494a7302309b48afcdb3a401",
           "id": 1
       }

响应:

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

按主机资产数据搜索

检索主机中包含主机清单"OS"字段为"Linux"的条目。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": [
                   "host"
               ],
               "selectInventory": [
                   "os"
               ],
               "searchInventory": {
                   "os": "Linux"
               }
           },
           "id": 2,
           "auth": "7f9e00124c75e8f25facd5c093f3e9a0"
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10084",
                   "host": "Zabbix server",
                   "inventory": {
                       "os": "Linux Ubuntu"
                   }
               },
               {
                   "hostid": "10107",
                   "host": "Linux server",
                   "inventory": {
                       "os": "Linux Mint"
                   }
               }
           ],
           "id": 1
       }

按主机标签搜索

获取标签"主机名称"等于"Linux服务器"的主机。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["hostid"],
               "selectTags": "extend",
               "evaltype": 0,
               "tags": [
                   {
                       "tag": "Host name",
                       "value": "Linux server",
                       "operator": 1
                   }
               ]
           },
           "auth": "7f9e00124c75e8f25facd5c093f3e9a0",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10085",
                   "tags": [
                       {
                           "tag": "Host name",
                           "value": "Linux server"
                       },
                       {
                           "tag": "OS",
                           "value": "RHEL 7"
                       }
                   ]
               }
           ],
           "id": 1
       }

获取不仅在主机级别具有这些标签,还在其关联的父模板中具有这些标签的主机。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["name"],
               "tags": [{"tag": "A", "value": "1", "operator": "0"}],
               "inheritedTags": true
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10623",
                   "name": "PC room 1"
               },
               {
                   "hostid": "10601",
                   "name": "Office"
               }
           ],
           "id": 1
       }

使用主机标签和模板标签搜索主机

获取带有标签的主机及其所有关联到父模板的标签

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["name"],
               "hostids": 10502,
               "selectTags": ["tag", "value"],
               "selectInheritedTags": ["tag", "value"]
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10502",
                   "name": "Desktop",
                   "tags": [
                       {
                           "tag": "A",
                           "value": "1"
                       }
                   ],
                   "inheritedTags": [
                       {
                           "tag": "B",
                           "value": "2"
                       }
                   ]
               }
           ],
           "id": 1
       }

按问题严重性搜索主机

检索存在"灾难"级别问题的主机。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["name"],
               "severities": 5
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "10160",
                   "name": "Zabbix server"
               }
           ],
           "id": 1
       }

检索存在"一般"和"严重"级别问题的主机。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["name"],
               "severities": [3, 4]
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "20170",
                   "name": "Database"
               },
               {
                   "hostid": "20183",
                   "name": "workstation"
               }
           ],
           "id": 1
       }

另请参阅

来源

CHost::get() 位于 ui/include/classes/api/services/CHost.php 文件中。