获取

描述

integer/array host.get(object parameters)

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

此方法对任何类型的用户都可用。调用该方法的权限可以在用户角色设置中撤销。有关更多信息,请参见 User roles。

参数

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

该方法支持以下参数。

参数 类型 描述
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 仅返回由指定 proxy 监控的主机。
proxy_groupids ID/array 仅返回由指定 proxy 组监控的主机。
templated_hosts flag 同时返回主机和模板。
templateids ID/array 仅返回链接到指定模板的主机。
triggerids ID/array 仅返回具有指定触发器的主机。
with_items flag 仅返回具有监控项的主机。

覆盖 with_monitored_items 和 with_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 如果设置为 true,则仅返回具有已抑制问题的主机。
evaltype integer 标签评估方法。

可能的值:
0 - (默认) And/Or;
2 - Or。
severities integer/array 仅返回具有指定严重性问题的主机。仅当问题对象为触发器时适用。
tags object/array 仅返回具有指定标签的主机。
格式:[{"tag": "<tag>", "value": "<value>", "operator": "<operator>"}, ...]。
空数组返回所有主机。

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

可能的值:
true - 链接模板也必须具有指定的 tags;
false - (默认) 忽略链接模板标签。
selectDiscoveryData query 返回包含主机发现对象数据的 discoveryData 属性。主机发现对象将已发现的主机链接到发现该主机的主机原型。

它具有以下属性:
host - (string) 主机 ID;
parent_hostid - (string) 创建该主机所依据的主机原型 ID;
status - (int) 主机发现状态:
0 - (默认) 已发现主机,
1 - 不再发现主机;
ts_delete - (timestamp) 删除不再发现的主机的时间;
ts_disable - (timestamp) 禁用不再发现的主机的时间;
disable_source - (int) 指示主机是由 LLD 规则还是手动禁用:
0 - (默认) 自动禁用,
1 - 由 LLD 规则禁用。
selectDiscoveryRule query 返回一个 discoveryRule 属性,其中包含创建该主机的低级别发现规则(在 VMware 监控中,该规则来自主机原型)。
selectDiscoveryRules query 返回一个 discoveryRules 属性,其中包含主机 LLD 规则。

支持 count。
selectGraphs query 返回一个 graphs 属性,其中包含主机图形。

支持 count。
selectHostGroups query 返回一个 hostgroups 属性,其中包含主机所属的主机组数据。
selectHttpTests query 返回一个 httpTests 属性,其中包含主机 Web 场景。

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

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

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

除模板对象字段外,还包含 link_type - (integer) 模板链接到主机的方式。
可能的值:
0 - (默认) 手动链接;
1 - 由 LLD 自动链接。

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

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

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

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

不支持 text 数据类型的属性。

支持其他属性:主机接口属性。
limitSelects integer 限制子查询返回的记录数。

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

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

仅支持 string 和 text 数据类型的属性。

支持其他属性:主机接口属性。
searchInventory object 返回资产清单数据与指定模式匹配的主机(不区分大小写)。

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

仅支持 string 和 text 数据类型的属性。
sortfield string/array 按指定属性对结果排序。

可能的值:hostid、host、name、status。
countOutput boolean 这些参数在参考说明中进行了描述。
editable boolean
excludeSearch boolean
limit integer
output query
preservekeys boolean
searchByAny boolean
searchWildcardsEnabled boolean
sortorder string/array
startSearch boolean
selectDiscoveries query 返回一个 discoveries 属性,其中包含主机低级别发现规则。

支持 count。

此查询已弃用,请改用 selectDiscoveryRules。
selectHostDiscovery query 返回一个 hostDiscovery 属性,其中包含主机发现对象数据。

此查询已弃用,请改用 selectDiscoveryData。

返回值

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

  • 一个对象的数组;
  • 如果使用了 countOutput 参数, 则返回检索到的对象数量。

示例

按名称检索数据

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

请求:

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

响应:

{
    "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
}

获取主机组

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

请求:

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

响应:

{
    "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
}

获取链接的模板

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

请求:

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

响应:

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

按模板获取主机

获取链接模板"10001" (Linux by Zabbix agent) 的主机。

请求:

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

响应:

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

按主机标签搜索

检索标签 "host-name" 等于 "linux-server" 的主机。

请求:

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

响应:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10085",
            "tags": [
                {
                    "tag": "host-name",
                    "value": "linux-server",
                    "automatic": "0"
                },
                {
                    "tag": "os",
                    "value": "rhel-7",
                    "automatic": "0"
                }
            ]
        }
    ],
    "id": 1
}

检索不仅在主机级别,而且在其已链接父模板中也具有这些标签的主机。

请求:

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

响应:

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

检索排除带有标签 "component" 的主机。

请求:

{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid"],
        "selectTags": "extend",
        "evaltype": 0,
        "tags": [
            {
                "tag": "component",
                "value": "",
                "operator": 5
            }
        ]
    },
    "id": 1
}

响应:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10084",
            "tags": [
                {
                    "tag": "class",
                    "value": "os",
                    "automatic": "0"
                },
                {
                    "tag": "target",
                    "value": "linux",
                    "automatic": "0"
                }
            ]
        },
        {
            "hostid": "10629",
            "tags": [
                {
                    "tag": "class",
                    "value": "application",
                    "automatic": "0"
                },
                {
                    "tag": "target",
                    "value": "browser",
                    "automatic": "0"
                }
            ]
        }
    ],
    "id": 1
}

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

检索带有标签以及所有链接到父模板的标签的主机。

请求:

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

响应:

{
    "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
}

根据问题严重程度检索主机

检索所有 "灾难" 问题的主机。

请求:

{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["name"],
        "severities": 5
    },
    "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]
    },
    "id": 1
}

响应:

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

获取主机监控项

获取“Zabbix server”主机的监控项。该请求仅限返回监控项属性 itemid、name 和 status。由于响应内容较大,示例中仅显示了部分监控项。

请求:

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

响应:

{
  "jsonrpc": "2.0",
  "result": [
    {
      "hostid": "10084",
      "items": [
        {
          "itemid": "42227",
          "name": "正在运行的 Zabbix agent 的主机名",
          "status": "0"
        },
        {
          "itemid": "42237",
          "name": "Zabbix agent ping",
          "status": "0"
        },
        {
          "itemid": "42250",
          "name": "正在运行的 Zabbix agent 的版本",
          "status": "0"
        },
        {
          "itemid": "42251",
          "name": "打开文件描述符的最大数量",
          "status": "0"
        },
        {
          "itemid": "42252",
          "name": "进程的最大数量",
          "status": "0"
        },
        {
          "itemid": "69869",
          "name": "接口 enp0s3:丢弃的入站数据包数",
          "status": "0"
        },
        {
          "itemid": "69870",
          "name": "接口 enp0s3:有错误的入站数据包数",
          "status": "0"
        },
        {
          "itemid": "69871",
          "name": "接口 enp0s3:接收的比特数",
          "status": "0"
        },
        {
          "itemid": "69872",
          "name": "接口 enp0s3:丢弃的出站数据包数",
          "status": "0"
        },
        {
          "itemid": "69873",
          "name": "接口 enp0s3:有错误的出站数据包数",
          "status": "0"
        },
        {
          "itemid": "69874",
          "name": "接口 enp0s3:发送的比特数",
          "status": "0"
        },
        {
          "itemid": "42253",
          "name": "进程数量",
          "status": "0"
        },
        {
          "itemid": "42254",
          "name": "正在运行的进程数量",
          "status": "0"
        },
        {
          "itemid": "42255",
          "name": "系统启动时间",
          "status": "0"
        }
      ]
    }
  ],
  "id": 1
}

另请参见

源码

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