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

获取

描述

host.get(对象参数)整数/数组

此方法允许根据指定的参数获取主机。

任何用户类型均可使用此方法。可以在用户角色中撤销调用方法的权限设置。详情参考 用户角色

参数

(对象) 参数定义了所需的输出。

该方法支持以下参数。

参数 描述
groupids 字符串/数组 只返回属于给定组的主机。
dserviceids 字符串/数组 只返回与所发现的给定服务相关的主机。
graphids 字符串/数组 只返回具有给定图形的主机。
hostids 字符串/数组 只返回具有给定主机id的主机。
httptestids 字符串/数组 只返回具有给定web检查的主机。
interfaceids 字符串/数组 只返回使用给定接口的主机。
itemids 字符串/数组 只返回具有给定监控项的主机。
maintenanceids 字符串/数组 只返回受给定维护影响的主机。
monitored_hosts 标记 只返回开启监控的主机。
proxy_hosts 标记 只返回代理的主机。
proxyids 字符串/数组 只返回被给定代理监视的主机。
templated_hosts 标记 返回主机和模板。
templateids 字符串/数组 只返回链接到给定模板的主机。
triggerids 字符串/数组 只返回具有给定触发器的主机。
with_items 标记 只返回有监控项的主机。

覆盖 with_monitored_itemswith_simple_graph_items 参数。
with_item_prototypes 标记 只返回具有项目原型的主机。

覆盖 with_simple_graph_item_prototypes 参数。
with_simple_graph_item_prototypes 标记 只返回具有项目原型的主机,该原型支持创建并具有数字类型的信息。
with_graphs 标记 只返回有图形的主机。
with_graph_prototypes 标记 只返回具有图形原型的主机。
with_httptests 标记 只返回有网络检查的主机。

覆盖 with_monitored_httptests 参数.
with_monitored_httptests 标记 只返回启用web检查的主机。
with_monitored_items 标记 只返回启用监控项的主机。

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

覆盖 with_monitored_triggers 参数。
withProblemsSuppressed 布尔值 返回已抑制问题的主机。

可能的值:
null - (默认) 所有主机;
true - 仅包含被抑制问题的主机;
false - 只允许存在未抑制问题的主机。
evaltype 整型 标记搜索规则。

可能的值:
0 - (默认) 和/或;
2 - 或.
severities 整型/属组 返回只存在给定严重性问题的主机。仅当问题对象为触发器时适用。
tags 数组/对象 只返回带有给定标签的主机。根据标记精确匹配,根据标记值进行大小写敏感或不区分大小写的搜索,具体取决于操作符值。
格式: [{"tag": "<tag>", "value": "<value>", "operator": "<operator>"}, ...].
空数组返回所有主机。

可能的操作符值:
0 - (默认) 包含;
1 - 登录;
2 - 不类似;
3 - 不等于
4 - 存在;
5 - 不存在。
inheritedTags 布尔值 返回那些在所有链接模板中也给出了 tags 的主机。 默认:

可能的值:
true - 链接模板也必须有tags;
false - (默认) 链接的模板标签将被忽略。
selectDiscoveries 查询 返回 发现 属性和主机低级发现规则。

支持 count.
selectDiscoveryRule 查询 返回 发现规则 属性和创建主机的底层发现规则(来自VMware监控中的主机原型)。
selectGraphs 查询 返回 图形 属性和主机图。

支持 count.
selectGroups 查询 返回 主机组 属性和主机所属的主机组数据。
selectHostDiscovery 查询 返回 hostDiscovery 属性和主机发现对象数据。

主机发现对象将发现的主机链接到主机原型,或将主机原型链接到LLD规则,并具有以下属性:
host - (string) 主机原型的主机;
hostid - (string) 发现的主机或主机原型ID;
parent_hostid - (string) 创建主机的主机原型ID;
parent_itemid - (string) 发现主机的LLD规则ID;
lastcheck - (timestamp) 最后发现主机的时间;
ts_delete - (timestamp) 删除已发现主机的时间。
selectHttpTests 查询 返回 httpTests 属性与主机web场景.

支持 count.
selectInterfaces 查询 返回 interfaces 属性和主机接口

Supports count
selectInventory 查询 返回 inventory 属性和主机库存数据。
selectItems query 返回 items 属性和主机项。

支持 count.
selectMacros 查询 返回 macros 属性和主机宏。
selectParentTemplates 查询 返回 parentTemplates 属性和主机链接到的模板。

支持 count.
selectDashboards 查询 返回 dashboards 属性.

支持 count.
selectTags 查询 返回 tags 属性和主机标记。
selectInheritedTags 查询 返回 inheritedTags 属性的标记,这些标记位于链接到主机的所有模板上。
selectTriggers 查询 返回 triggers 属性和主机触发器。

支持 count.
selectValueMaps 查询 返回 valuemaps 属性和主机映射值。
filter 对象 只返回与给定过滤器精确匹配的结果。

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

允许通过接口属性进行过滤。
limitSelects 整型 限制子选择返回的记录数量。

适用于以下子选择:
selectParentTemplates - 结果将按 host 排序;
selectInterfaces;
selectItems - 结果将按 name 排序;
selectDiscoveries - 结果将按 name 排序;
selectTriggers - 结果将按 description 排序;
selectGraphs - 结果将按 name 排序;
selectDashboards - 结果将按 name 排序。
search 对象 返回匹配给定通配符搜索的结果。

接受一个数组,其中键是属性名,值是要搜索的字符串。如果没有给出额外的选项,这将执行 LIKE "%…%" 搜索。

允许根据接口属性进行搜索。仅适用于文本字段。
searchInventory 对象 只返回库存数据与给定通配符搜索匹配的主机。

该参数受与 search 相同的附加参数的影响。
sortfield 字符串/数组 根据给定的属性对结果进行排序。

可能的值: hostid, host, name, status.
countOutput 布尔值 这些对所有 get 方法通用的参数在 参考注释 中有详细描述。
editable boolean
excludeSearch boolean
limit integer
output query
preservekeys boolean
searchByAny boolean
searchWildcardsEnabled boolean
sortorder string/array
startSearch boolean

返回值

(整数/数组) 返回其中之一:

  • 一组对象;
  • 如果使用了 countOutput 参数,则返回获取的对象数量。

示例

按名称检索数据

获取所有关于"Zabbix server"和"Linux server"两个主机的数据。

请求:

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

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "maintenances": [],
                   "hostid": "10160",
                   "proxy_hostid": "0",
                   "host": "Zabbix server",
                   "status": "0",
                   "lastaccess": "0",
                   "ipmi_authtype": "-1",
                   "ipmi_privilege": "2",
                   "ipmi_username": "",
                   "ipmi_password": "",
                   "ipmi_disable_until": "0",
                   "snmp_disable_until": "0",
                   "maintenanceid": "0",
                   "maintenance_status": "0",
                   "maintenance_type": "0",
                   "maintenance_from": "0",
                   "name": "Zabbix server",
                   "description": "The Zabbix monitoring server.",
                   "tls_connect": "1",
                   "tls_accept": "1",
                   "tls_issuer": "",
                   "tls_subject": ""
               },
               {
                   "maintenances": [],
                   "hostid": "10167",
                   "proxy_hostid": "0",
                   "host": "Linux server",
                   "status": "0",
                   "lastaccess": "0",
                   "ipmi_authtype": "-1",
                   "ipmi_privilege": "2",
                   "ipmi_username": "",
                   "ipmi_password": "",
                   "ipmi_disable_until": "0",
                   "snmp_disable_until": "0",
                   "maintenanceid": "0",
                   "maintenance_status": "0",
                   "maintenance_type": "0",
                   "maintenance_from": "0",
                   "name": "Linux server",
                   "description": "",
                   "tls_connect": "1",
                   "tls_accept": "1",
                   "tls_issuer": "",
                   "tls_subject": ""
               }
           ],
           "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
       }

Retrieving hosts by template

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

Request:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["hostid", "name"],
               "templateids": "10001"
           },
           "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
       }

按主机资产数据进行搜索

检索主机资产“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() in ui/include/classes/api/services/CHost.php.