[comment]: # translation:outdated

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

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

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

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

此方法允许根据指定的参数检索主机。

::: noteclassic
任何用户类型均可使用此方法。可以在用户角色设置中撤销调用方法的权限。详情参考[用户角色](/manual/web_interface/frontend_sections/users/user_roles)获取更多信息。
:::

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

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

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

该方法支持以下参数。

|参数|[类型](/manual/api/reference_commentary#数据类型)|描述|
|--|--|------|
|groupids|ID/array|仅返回指定主机组的主机。|
|dserviceids|ID/array|仅返回与指定发现的服务相关的主机。|
|graphids|ID/array|仅返回具有指定图表ID的主机。|
|hostids|ID/array|仅返回指定主机ID的主机。|
|httptestids|ID/array|仅返回具有指定web检查的主机。|
|interfaceids|ID/array|仅返回具有指定接口的主机。|
|itemids|ID/array|仅返回具有指定监控项的主机。|
|maintenanceids|ID/array|仅返回指定维护计划中的主机。|
|monitored\_hosts|flag|仅返回开启监控的主机。|
|proxyids|ID/array|仅返回被指定proxy监控的主机。|
|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 - *(默认)* And/Or;<br>2 - Or.|
|severities|integer/array|仅返回指定严重性问题的主机。仅当问题对象为触发器时适用。|
|tags|object/array|仅返回指定标签的主机。根据标签精确匹配，根据标签值进行大小写敏感或不区分大小写的搜索，具体取决于操作符值。<br>格式: `[{"tag": "<tag>", "value": "<value>", "operator": "<operator>"}, ...]`。<br>空数组将返回所有主机。<br><br>可能的操作符值:<br>0 - *(默认)* 包含;<br>1 - 等于;<br>2 - 不匹配;<br>3 - 不等于;<br>4 - 存在;<br>5 - 不存在。|
|inheritedTags|boolean|返回在其所有链接模板中也指定了 `标签` 的主机。默认:<br><br>可能的值:<br>`true` - 链接的模板中存在指定的 `标签` ;<br>`false` - *(默认)* 忽略链接模板中的标签|
|selectDiscoveries|query|返回包含主机低级别自动发现规则的[`发现规则`](/manual/api/reference/discoveryrule/object)的属性。<br><br>支持 `count `。|
|selectDiscoveryRule|query|返回创建主机（来自于VMware监控中的主机原型）的低级别自动发现规则的[`发现规则`](/manual/api/reference/discoveryrule/object)的属性。|
|selectGraphs|query|返回主机[`图表`](/manual/api/reference/graph/object)的属性。<br><br>支持 `count`。|
|selectHostDiscovery|query|返回带有主机发现对象数据的`主机发现规则` 属性。<br><br>主机发现对象将发现的主机链接到主机原型，或将主机原型链接到LLD规则，并具有以下属性:<br>`host` - *(string)* 主机的主机原型;<br>`hostid` - *(string)* 主机发现ID或者主机原型;<br>`parent_hostid` - *(string)* 创建主机的主机原型ID;<br>`parent_itemid` - *(string)* 创建发现主机的LLD规则ID;<br>`lastcheck` - *(timestamp)* 主机最后一次发现时间;<br>`status` - *(int)* 主机发现状态:<br>0 - *(默认)* 主机被发现,<br>1 - 主机没有被发现;<br>`ts_delete` - *(timestamp)* 不再被发现的主机将被删除的时间;<br>`ts_disable` - *(timestamp)* 不再被发现的主机将被禁用的时间;<br>`disable_source` - *(int)* 显示主机是由LLD规则禁用还是手动禁用的:<br>0 - *(默认)* 手动禁用,<br>1 - LLD规则禁用。|
|selectHostGroups|query|返回主机所属的[`主机组`](/manual/api/reference/hostgroup/object)的属性。|
|selectHttpTests|query|返回主机web场景[`httpTests`](/manual/api/reference/httptest/object)的属性。<br><br>支持 `count`。|
|selectInterfaces|query|返回主机[`接口`](/manual/api/reference/hostinterface/object)的属性。<br><br>支持 `count`。|
|selectInventory|query|返回主机[`资产清单`](/manual/api/reference/host/object#host_inventory)的属性。|
|selectItems|query|返回主机[`监控项`](/manual/api/reference/item/object)的属性。<br><br>支持 `count`。|
|selectMacros|query|返回主机[`宏`](/manual/api/reference/usermacro/object)的属性。|
|selectParentTemplates|query|返回主机链接[模板](/manual/api/reference/template/object)的 `父模板` 属性。<br><br>除了Template对象字段外，它还包含`link_type` - `(integer)` 模板链接到主机的方式。<br>可能的值:<br>0 - *(default)* 手动链接;<br>1 - LLD自动链接.<br><br>支持 `count`。|
|selectDashboards|query|返回[`仪表盘`](/manual/api/reference/templatedashboard/object)的属性。<br><br>支持 `count`。|
|selectTags|query|返回主机的[`标签`](/manual/api/reference/host/object#主机标签)的属性。|
|selectInheritedTags|query|返回通过模板链接[`继承标签`](/manual/api/reference/host/object#主机标签)的属性。|
|selectTriggers|query|返回主机的[`触发器`](/manual/api/reference/trigger/object)的属性。<br><br>支持 `count`。|
|selectValueMaps|query|返回主机[`值映射`](/manual/api/reference/valuemap/object)的属性。|
|filter|object|仅返回与指定过滤器精确匹配的结果。<br><br>接受一个数组，其中键是属性名，值是单个值或要匹配的值数组。<br><br>不支持`文本`[数据类型](/manual/api/reference_commentary#数据类型)的属性。<br><br>支持其它属性: [主机接口](/manual/api/reference/hostinterface/object#主机接口对象)的属性。|
|limitSelects|integer|限制子查询返回的记录数量。<br><br>适用于以下子查询:<br>`selectParentTemplates` - 结果将根据 `主机` 排序;<br>`selectInterfaces`;<br>`selectItems` - 根据`名称`排序;<br>`selectDiscoveries` - 根据`名称`排序<br>`selectTriggers` - 根据`描述`排序;<br>`selectGraphs` - 根据`名称`排序;<br>`selectDashboards` - 根据`名称`排序。|
|search|object|返回与指定模式匹配的结果 (不区分大小写)。<br><br>接受一个对象，其中键是属性名，值是要搜索的字符串。如果没有提供其他选项，这将执行`LIKE"%…%"`搜索。<br><br>仅支持`字符串` 和 `文本`[数据类型](/manual/api/reference_commentary#数据类型)的属性。<br><br>支持其它属性: [主机接口](/manual/api/reference/hostinterface/object#主机接口对象)的属性。|
|searchInventory|object|返回与指定资产清单数据匹配的主机 (不区分大小写).<br><br>接受一个对象，其中键是属性名，值是要搜索的字符串。如果没有提供其他选项，这将执行`LIKE"%…%"`搜索。<br><br>仅支持`字符串` 和 `文本`[数据类型](/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|^|
|selectGroups<br>(deprecated)|query|此参数已弃用，请改用 `selectHostGroups`。<br>返回主机组数据的[`主机组`](/manual/api/reference/hostgroup/object) 的属性。|

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

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

`(integer/array)` 返回其中之一:

-    一个对象的数组;
-    如果使用了 `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" (*Linux by Zabbix agent*) 的主机。

[请求](/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]: # ({abe274bf-b4e37e5e})
#### 通过主机标签搜索

获取主机标签 "Host 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"
                },
                {
                    "tag": "OS",
                    "value": "RHEL 7"
                }
            ]
        }
    ],
    "id": 1
}
```

检索在标签，且标签在主机级别以及链接的父模板中都存在。

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

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

响应:

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

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

[comment]: # ({14e4fd6f-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": "A",
                    "value": "1"
                }
            ],
            "inheritedTags": [
                {
                    "tag": "B",
                    "value": "2"
                }
            ]
        }
    ],
    "id": 1
}
```

[comment]: # ({/14e4fd6f-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]: # ({1b616a06-06c7fe93})
### 另见

-   [主机组](/manual/api/reference/hostgroup/object#主机组)
-   [模板](/manual/api/reference/template/object#模板)
-   [用户宏](/manual/api/reference/usermacro/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() in *ui/include/classes/api/services/CHost.php*.

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