[comment]: # translation:outdated

[comment]: # ({65bbffce-65bbffce})
# host.get

[comment]: # ({/65bbffce-65bbffce})

[comment]: # ({54619a1f-93035b19})
### 描述

`integer/array host.get(object parameters)`

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

::: noteclassic
此方法可供任何类型的用户使用. 调用该方法的权限可以在用户角色设置中撤销. 更多信息请参阅[User
roles](/manual/web_interface/frontend_sections/users/user_roles).

:::

[comment]: # ({/54619a1f-93035b19})

[comment]: # ({339c15a6-77a8fabb})
### 参数

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

该方法支持以下参数.

| 参数 | [数据类型](/manual/api/reference_commentary#数据类型) | 描述 |
|--|--|------|
| groupids | ID/array | 仅返回属于指定组的主机. |
| dserviceids | ID/array | 仅返回与指定发现服务相关的主机. |
| graphids | ID/array | 仅返回包含指定图形的主机. |
| hostids | ID/array | 仅返回具有指定主机 ID的主机. |
| httptestids | ID/array | 仅返回包含指定Web检查的主机. |
| interfaceids | ID/array | 仅返回使用指定接口的主机. |
| itemids | ID/array | 仅返回包含指定监控项的主机. |
| maintenanceids | ID/array | 仅返回受指定维护影响的主机. |
| monitored\_hosts | flag | 仅返回受监控的主机. |
| proxyids | ID/array | 仅返回由指定proxies监控的主机. |
| proxy_groupids | ID/array | 仅返回由指定proxy 组监控的主机. |
| templated\_hosts | flag | 同时返回主机和模板. |
| templateids | ID/array | 仅返回链接到指定模板的主机. |
| triggerids | ID/array | 仅返回包含指定触发器的主机. |
| with\_items | flag | 仅返回包含监控项的主机.<br><br>覆盖`with_monitored_items`和`with_simple_graph_items`参数. |
| with\_item\_prototypes | flag | 仅返回包含监控项原型的主机.<br><br>覆盖`with_simple_graph_item_prototypes`参数. |
| with\_simple\_graph\_item\_prototypes | flag | 仅返回包含已启用创建且具有数值类型信息的监控项原型的主机. |
| with\_graphs | flag | 仅返回包含图形的主机. |
| with\_graph\_prototypes | flag | 仅返回包含图形原型的主机. |
| with\_httptests | flag | 仅返回包含Web检查的主机.<br><br>覆盖`with_monitored_httptests`参数. |
| with\_monitored\_httptests | flag | 仅返回包含已启用Web检查的主机. |
| with\_monitored\_items | flag | 仅返回包含已启用监控项的主机.<br><br>覆盖`with_simple_graph_items`参数. |
| with\_monitored\_triggers | flag | 仅返回包含已启用触发器的主机. 触发器使用的所有监控项也必须启用. |
| with\_simple\_graph\_items | flag | 仅返回包含具有数值类型信息的监控项的主机. |
| with\_triggers | flag | 仅返回包含触发器的主机.<br><br>覆盖`with_monitored_triggers`参数. |
| withProblemsSuppressed | boolean | 返回具有被抑制问题的主机.<br><br>可能值:<br>`null` - *(默认)* 所有主机;<br>`true` - 仅包含被抑制问题的主机;<br>`false` - 仅包含未被抑制问题的主机. |
| evaltype | integer | 标签搜索规则.<br><br>可能值:<br>0 - *(默认)* 与/或;<br>2 - 或. |
| severities | integer/array | 返回仅包含指定严重性问题的主机. 仅当问题object为触发器时适用. |
| tags | object/array | 仅返回具有指定标签的主机. 根据运算符值对标签值进行精确匹配或大小写敏感/不敏感搜索.<br>格式: `[{"tag": "<tag>", "value": "<value>", "operator": "<operator>"}, ...]`.<br>空array返回所有主机.<br><br>可能运算符值:<br>0 - *(默认)* 包含;<br>1 - 等于;<br>2 - 不相似;<br>3 - 不等于;<br>4 - 存在;<br>5 - 不存在. |
| inheritedTags | boolean | 返回主机其所有链接模板也包含指定`tags`. 默认:<br><br>可能值:<br>`true` - 链接模板也必须包含指定`tags`;<br>`false` - *(默认)* 忽略链接模板标签. |
| selectDiscoveries | query | 返回包含主机低级发现规则的[`discoveries`](/manual/api/reference/discoveryrule/object)属性.<br><br>支持`count`. |
| selectDiscoveryData | query | 返回包含主机发现object数据的`discoveryData`属性. 主机发现object将发现的主机链接到从中发现的一个主机原型.<br><br>主机发现object将发现的主机链接到一个主机原型或将一个主机原型链接到LLD规则,并具有以下属性:<br>`parent_hostid` - *(string)* 创建主机的主机原型ID;<br>`parent_itemid` - *(string)* 创建发现主机的LLD规则ID;<br>`lastcheck` - *(时间戳)* 主机最后发现时间;<br>`status` - *(整数)* 主机发现状态:<br>0 - *(默认)* 主机已发现,<br>1 - 主机不再被发现;<br>`ts_delete` - *(时间戳)* 不再被发现的一个主机将被删除的时间;<br>`ts_disable` - *(时间戳)* 不再被发现的一个主机将被禁用的时间;<br>`disable_source` - *(整数)* 指示主机是由LLD规则还是手动禁用:<br>0 - *(默认)* 自动禁用,<br>1 - 由LLD规则禁用. |
| selectDiscoveryRule | query | 返回包含创建主机的低级发现规则的[`discoveryRule`](/manual/api/reference/discoveryrule/object)属性(来自VMware监控中的主机原型). |
| selectDiscoveryRulePrototype | query | 返回包含创建主机的父LLD规则原型的[`discoveryRulePrototype`](/manual/api/reference/discoveryruleprototype/object)属性(来自VMware监控中的主机原型). |
| selectGraphs | query | 返回包含主机图形的[`graphs`](/manual/api/reference/graph/object)属性.<br><br>支持`count`. |
| selectHostDiscovery | query | 返回包含主机发现object数据的`hostDiscovery`属性.<br><br>此query已**弃用**,请改用`selectDiscoveryData`. |
| selectHostGroups | query | 返回包含主机所属主机组数据的[`hostgroups`](/manual/api/reference/hostgroup/object)属性. |
| selectHttpTests | query | 返回包含主机Web场景的[`httpTests`](/manual/api/reference/httptest/object)属性.<br><br>支持`count`. |
| selectInterfaces | query | 返回包含主机接口的[`interfaces`](/manual/api/reference/hostinterface/object)属性.<br><br>支持`count`. |
| selectInventory | query | 返回包含主机清单数据的[`inventory`](/manual/api/reference/host/object#host_inventory)属性. |
| selectItems | query | 返回包含主机监控项的[`items`](/manual/api/reference/item/object)属性.<br><br>支持`count`. |
| selectMacros | query | 返回包含主机宏的[`macros`](/manual/api/reference/usermacro/object)属性. |
| selectParentTemplates | query | 返回包含[templates](/manual/api/reference/template/object)的`parentTemplates`属性,主机链接到这些模板.<br><br>除了模板object字段外,还包含`link_type` - `(integer)`模板链接到主机的方式.<br>可能值:<br>0 - *(默认)* 手动链接;<br>1 - 由LLD自动链接.<br><br>支持`count`. |
| selectDashboards | query | 返回[`dashboards`](/manual/api/reference/templatedashboard/object)属性.<br><br>支持`count`. |
| selectTags | query | 返回包含主机标签的[`tags`](/manual/api/reference/host/object#host_tag)属性. |
| selectInheritedTags | query | 返回包含链接到主机的所有模板上标签的[`inheritedTags`](/manual/api/reference/host/object#host_tag)属性. |
| selectTriggers | query | 返回包含主机触发器的[`triggers`](/manual/api/reference/trigger/object)属性.<br><br>支持`count`. |
| selectValueMaps | query | 返回包含主机值映射的[`valuemaps`](/manual/api/reference/valuemap/object)属性. |
| filter | object | 仅返回完全匹配给定筛选条件的结果.<br><br>接受一个object,其中键是属性名,值是要匹配的单个值或array值.<br><br>不支持`text`[数据类型](/manual/api/reference_commentary#数据类型)的属性.<br><br>支持附加属性: [主机-接口-object](/manual/api/reference/hostinterface/object#主机-接口-object)属性. |
| limitSelects | integer | 限制子查询返回的记录数.<br><br>适用于以下子查询:<br>`selectParentTemplates` - 结果将按`host`排序;<br>`selectInterfaces`;<br>`selectItems` - 按`name`排序;<br>`selectDiscoveries` - 按`name`排序;<br>`selectTriggers` - 按`description`排序;<br>`selectGraphs` - 按`name`排序;<br>`selectDashboards` - 按`name`排序. |
| search | object | 返回匹配给定模式的结果(不区分大小写).<br><br>接受一个object,其中键是属性名,值是要搜索的字符串. 如果未提供额外选项,将执行`LIKE "%…%"`搜索.<br><br>仅支持`string`和`text`[数据类型](/manual/api/reference_commentary#数据类型)的属性.<br><br>支持附加属性: [主机-接口-object](/manual/api/reference/hostinterface/object#主机-接口-object)属性. |
| searchInventory | object | 返回清单数据匹配给定模式的主机(不区分大小写).<br><br>接受一个object,其中键是属性名,值是要搜索的字符串. 如果未提供额外选项,将执行`LIKE "%…%"`搜索.<br><br>仅支持`string`和`text`[数据类型](/manual/api/reference_commentary#数据类型)的属性. |
| sortfield | string/array | 按给定属性排序结果.<br><br>可能值: `hostid`, `host`, `name`, `status`. |
| countOutput | boolean | 这些参数在[通用get方法参数](/manual/api/reference_commentary#通用get方法参数)中有描述. |
| editable | boolean | ^           |
| excludeSearch | boolean | ^           |
| limit | integer | ^           |
| output | query | ^           |
| preservekeys | boolean | ^           |
| searchByAny | boolean | ^           |
| searchWildcardsEnabled | boolean | ^           |
| sortorder | string/array | ^           |
| startSearch | boolean | ^           |

[comment]: # ({/339c15a6-77a8fabb})

[comment]: # ({7223bab1-7223bab1})
### 返回值

`(integer/array)` 返回以下两种情况之一:

- 一个包含array的objects数组
- 检索到的objects数量（当使用了`countOutput`参数时）

[comment]: # ({/7223bab1-7223bab1})

[comment]: # ({b41637d2-b41637d2})
### 示例

[comment]: # ({/b41637d2-b41637d2})

[comment]: # ({bdf49605-c6ea3cce})
#### 按名称检索数据

获取两个名为"Zabbix server"和"Linux server"的主机的所有数据.

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "filter": {
            "host": [
                "Zabbix server",
                "Linux server"
            ]
        }
    },
    "id": 1
}
```
响应:

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10160",
            "proxyid": "0",
            "host": "Zabbix server",
            "status": "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": "",
            "proxy_groupid": "0",
            "monitored_by": "0",
            "inventory_mode": "1",
            "active_available": "1",
            "assigned_proxyid": "0"
        },
        {
            "hostid": "10167",
            "proxyid": "0",
            "host": "Linux server",
            "status": "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": "",
            "proxy_groupid": "0",
            "monitored_by": "0",
            "inventory_mode": "1",
            "active_available": "1",
            "assigned_proxyid": "0"
        }
    ],
    "id": 1
}
```

[comment]: # ({/bdf49605-c6ea3cce})

[comment]: # ({8d87c206-9078aff7})
#### 检索主机组

获取主机所属的"Zabbix server"主机组

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid"],
        "selectHostGroups": "extend",
        "filter": {
            "host": [
                "Zabbix server"
            ]
        }
    },
    "id": 1
}
```
响应:

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10085",
            "hostgroups": [
                {
                    "groupid": "2",
                    "name": "Linux servers",
                    "flags": "0",
                    "uuid": "dc579cd7a1a34222933f24f52a68bcd8"
                },
                {
                    "groupid": "4",
                    "name": "Zabbix servers",
                    "flags": "0",
                    "uuid": "6f6799aa69e844b4b3918f779f2abf08"
                }
            ]
        }
    ],
    "id": 1
}
```

[comment]: # ({/8d87c206-9078aff7})

[comment]: # ({3f6d4bdf-59c8a46a})
#### 检索关联模板

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

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid"],
        "selectParentTemplates": [
            "templateid",
            "name"
        ],
        "hostids": "10084"
    },
    "id": 1
}
```
响应:

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

[comment]: # ({/3f6d4bdf-59c8a46a})

[comment]: # ({9a8b8f9f-942e31bf})
#### 通过模板检索主机

获取已关联"10001"（*由Zabbix agent提供的Linux*）模板的主机

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid", "name"],
        "templateids": "10001"
    },
    "id": 1
}
```
响应:

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

[comment]: # ({/9a8b8f9f-942e31bf})

[comment]: # ({63a1e5ba-8be1d99c})
#### 按主机资产数据搜索

检索在主机资产清单"OS"字段中包含"Linux"的主机

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "host"
        ],
        "selectInventory": [
            "os"
        ],
        "searchInventory": {
            "os": "Linux"
        }
    },
    "id": 1
}
```
响应:

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

[comment]: # ({/63a1e5ba-8be1d99c})

[comment]: # ({b6238857-b4e37e5e})
#### 按主机标签搜索

获取标签"主机-name"等于"linux-server"的主机

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid"],
        "selectTags": "extend",
        "evaltype": 0,
        "tags": [
            {
                "tag": "host-name",
                "value": "linux-server",
                "operator": 1
            }
        ]
    },
    "id": 1
}
```
响应:

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10085",
            "tags": [
                {
                    "tag": "host-name",
                    "value": "linux-server",
                    "automatic": "0"
                },
                {
                    "tag": "os",
                    "value": "rhel-7",
                    "automatic": "0"
                }
            ]
        }
    ],
    "id": 1
}
```
获取不仅在主机级别上具有这些标签，还在其链接的父模板中具有这些标签的主机

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["name"],
        "tags": [
            {
                "tag": "os",
                "value": "rhel-7",
                "operator": 1
            }
        ],
        "inheritedTags": true
    },
    "id": 1
}
```
响应:

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

[comment]: # ({/b6238857-b4e37e5e})

[comment]: # ({c6c128e0-9ebae84e})
#### 使用主机标签和模板标签搜索主机

获取带标签的一个主机以及链接到父模板的所有标签

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["name"],
        "hostids": 10502,
        "selectTags": ["tag", "value"],
        "selectInheritedTags": ["tag", "value"]
    },
    "id": 1
}
```
响应:

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10502",
            "name": "Desktop",
            "tags": [
                {
                    "tag": "host-name",
                    "value": "linux-server"
                },
                {
                    "tag": "os",
                    "value": "rhel-7"
                }
            ],
            "inheritedTags": [
                {
                    "tag": "class",
                    "value": "os"
                },
                {
                    "tag": "target",
                    "value": "linux"
                },
                {
                    "tag": "os",
                    "value": "rhel-7"
                }
            ]
        }
    ],
    "id": 1
}
```

[comment]: # ({/c6c128e0-9ebae84e})

[comment]: # ({ee33fbef-219d4ee1})
#### 按问题严重性搜索主机

获取存在"灾难"级别问题的主机

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["name"],
        "severities": 5
    },
    "id": 1
}
```
响应:

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10160",
            "name": "Zabbix server"
        }
    ],
    "id": 1
}
```
获取存在"一般"和"严重"级别问题的主机

[执行请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["name"],
        "severities": [3, 4]
    },
    "id": 1
}
```
响应:

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

[comment]: # ({/ee33fbef-219d4ee1})

[comment]: # ({new-56ff1999})
#### Retrieving host items

Retrieve items for the "Zabbix server" host. The request is limited to the item properties `itemid`, `name`, and `status`. Due to the large response size, only a subset of items is shown in the example.

[Request](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid"],
        "selectItems": ["itemid","name","status"],
        "filter": {
            "host": [
                "Zabbix server"
            ]
        }
    },
    "id": 1
}
```

Response:

```json
{
  "jsonrpc": "2.0",
  "result": [
    {
      "hostid": "10084",
      "items": [
        {
          "itemid": "42227",
          "name": "Host name of Zabbix agent running",
          "status": "0"
        },
        {
          "itemid": "42237",
          "name": "Zabbix agent ping",
          "status": "0"
        },
        {
          "itemid": "42250",
          "name": "Version of Zabbix agent running",
          "status": "0"
        },
        {
          "itemid": "42251",
          "name": "Maximum number of open file descriptors",
          "status": "0"
        },
        {
          "itemid": "42252",
          "name": "Maximum number of processes",
          "status": "0"
        },
        {
          "itemid": "69869",
          "name": "Interface enp0s3: Inbound packets discarded",
          "status": "0"
        },
        {
          "itemid": "69870",
          "name": "Interface enp0s3: Inbound packets with errors",
          "status": "0"
        },
        {
          "itemid": "69871",
          "name": "Interface enp0s3: Bits received",
          "status": "0"
        },
        {
          "itemid": "69872",
          "name": "Interface enp0s3: Outbound packets discarded",
          "status": "0"
        },
        {
          "itemid": "69873",
          "name": "Interface enp0s3: Outbound packets with errors",
          "status": "0"
        },
        {
          "itemid": "69874",
          "name": "Interface enp0s3: Bits sent",
          "status": "0"
        },
        {
          "itemid": "42253",
          "name": "Number of processes",
          "status": "0"
        },
        {
          "itemid": "42254",
          "name": "Number of running processes",
          "status": "0"
        },
        {
          "itemid": "42255",
          "name": "System boot time",
          "status": "0"
        }
      ]
    }
  ],
  "id": 1
}
```

[comment]: # ({/new-56ff1999})

[comment]: # ({1b616a06-06c7fe93})
### 另请参阅

-   [主机组](/manual/api/reference/hostgroup/object#主机组)
-   [模板](/manual/api/reference/template/object#模板)
-   [用户宏-object](/manual/api/reference/usermacro/object#用户宏-object)
-   [主机接口](/manual/api/reference/hostinterface/object#主机接口)
-   [proxy](/manual/api/reference/proxy/object#proxy)
-   [proxy-组](/manual/api/reference/proxygroup/object#proxy-组)

[comment]: # ({/1b616a06-06c7fe93})

[comment]: # ({55c08f7a-55c08f7a})
### 来源

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

[comment]: # ({/55c08f7a-55c08f7a})
