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

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

[comment]: # ({e9f79f9b-b87cd1ee})
### 描述

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

该方法允许根据给定参数检索 LLD 规则。

::: noteclassic
此方法对任何类型的用户都可用。调用该方法的权限可以在用户角色设置中撤销。有关更多信息，请参见[用户角色](/manual/web_interface/frontend_sections/users/user_roles)。
:::

[comment]: # ({/e9f79f9b-b87cd1ee})

[comment]: # ({ae0c9661-57007d3d})
### 参数

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

该方法支持以下参数。

|参数|[类型](/manual/api/reference_commentary#data-types)|说明|
|--|--|------|
|itemids|ID/array|仅返回具有给定 ID 的 LLD 规则。|
|groupids|ID/array|仅返回属于给定组中主机的 LLD 规则。|
|hostids|ID/array|仅返回属于给定主机的 LLD 规则。|
|inherited|boolean|如果设置为 `true`，则仅返回从模板继承的 LLD 规则。|
|interfaceids|ID/array|仅返回使用给定主机接口的 LLD 规则。|
|monitored|boolean|如果设置为 `true`，则仅返回属于受监控主机的已启用 LLD 规则。|
|templated|boolean|如果设置为 `true`，则仅返回属于模板的 LLD 规则。|
|templateids|ID/array|仅返回属于给定模板的 LLD 规则。|
|selectDiscoveryData|query|返回 `discoveryData` 属性，其中包含 LLD 规则发现对象数据。LLD 规则发现对象将已发现的 LLD 规则关联到发现该规则的 LLD 规则原型。|
|selectDiscoveryRulePrototypes|query|返回 [`discoveryRulePrototypes`](/manual/api/reference/discoveryruleprototype/object) 属性，其中包含属于该 LLD 规则的 LLD 规则原型。<br><br>支持 `count`。|
|selectFilter|query|返回 [`filter`](/manual/api/reference/discoveryrule/object#lld-rule-filter) 属性，其中包含 LLD 规则所使用过滤器的数据。|
|selectGraphs|query|返回 [`graphs`](/manual/api/reference/graph/object) 属性，其中包含属于该 LLD 规则的图形原型。<br><br>支持 `count`。|
|selectHostPrototypes|query|返回 [`hostPrototypes`](/manual/api/reference/hostprototype/object) 属性，其中包含属于该 LLD 规则的主机原型。<br><br>支持 `count`。|
|selectHosts|query|返回 [`hosts`](/manual/api/reference/host/object) 属性，其中包含该 LLD 规则所属主机的数组。|
|selectItems|query|返回 [`items`](/manual/api/reference/item/object) 属性，其中包含属于该 LLD 规则的监控项原型。<br><br>支持 `count`。|
|selectTriggers|query|返回 [`triggers`](/manual/api/reference/trigger/object) 属性，其中包含属于该 LLD 规则的触发器原型。<br><br>支持 `count`。|
|selectLLDMacroPaths|query|返回 [`lld_macro_paths`](/manual/api/reference/discoveryrule/object#lld-macro-path) 属性，其中包含 LLD 宏列表以及分配给每个对应宏的值路径。|
|selectPreprocessing|query|返回 [`preprocessing`](/manual/api/reference/discoveryrule/object#lld-rule-preprocessing) 属性，其中包含 LLD 规则预处理选项。|
|selectOverrides|query|返回 [`lld_rule_overrides`](/manual/api/reference/discoveryrule/object#lld-rule-overrides) 属性，其中包含对原型对象执行的覆盖过滤器、条件和操作列表。|
|filter|object|仅返回与给定过滤器完全匹配的结果。<br><br>接受一个对象，其中键为属性名称，值可以是单个值或用于匹配的值数组。<br><br>不支持 `text` [data type](/manual/api/reference_commentary#data-types) 的属性。<br><br>支持附加属性：<br>`host` - LLD 规则所属主机的技术名称。|
|limitSelects|integer|限制子选择返回的记录数。<br><br>适用于以下子选择：`selectItems`、`selectGraphs`、`selectTriggers`。|
|sortfield|string/array|按给定属性对结果进行排序。<br><br>可能的值：`itemid`、`name`、`key_`、`delay`、`type`、`status`。|
|countOutput|boolean|这些参数在[参考说明](/manual/api/reference_commentary#common-get-method-parameters)中进行了描述。|
|editable|boolean|^|
|excludeSearch|boolean|^|
|limit|integer|^|
|output|query|^|
|preservekeys|boolean|^|
|search|object|^|
|searchByAny|boolean|^|
|searchWildcardsEnabled|boolean|^|
|sortorder|string/array|^|
|startSearch|boolean|^|

[comment]: # ({/ae0c9661-57007d3d})

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

`(integer/array)` 返回以下之一：

-   对象数组；
-   如果使用了 `countOutput` 参数，则返回检索到的对象数量。

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

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

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

[comment]: # ({ab9d9991-5f8b5429})
#### 从主机检索发现规则

检索指定主机 ID 的所有发现规则。

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

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.get",
    "params": {
        "output": "extend",
        "hostids": "10202"
    },
    "id": 1
}
```

响应：

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "itemid": "27425",
            "type": "0",
            "snmp_oid": "",
            "hostid": "10202",
            "name": "Network interface discovery",
            "key_": "net.if.discovery",
            "delay": "1h",
            "status": "0",
            "trapper_hosts": "",
            "templateid": "22444",
            "valuemapid": "0",
            "params": "",
            "ipmi_sensor": "",
            "authtype": "0",
            "username": "",
            "password": "",
            "publickey": "",
            "privatekey": "",
            "flags": "1",
            "interfaceid": "119",
            "description": "根据全局正则表达式 \"Network interfaces for discovery\" 定义的网络接口发现。",
            "lifetime": "30d",
            "jmx_endpoint": "",
            "master_itemid": "0",
            "timeout": "",
            "url": "",
            "query_fields": [],
            "posts": "",
            "status_codes": "200",
            "follow_redirects": "1",
            "post_type": "0",
            "http_proxy": "",
            "headers": [],
            "retrieve_mode": "0",
            "request_method": "0",
            "ssl_cert_file": "",
            "ssl_key_file": "",
            "ssl_key_password": "",
            "verify_peer": "0",
            "verify_host": "0",
            "allow_traps": "0",
            "uuid": "",
            "lifetime_type": "0",
            "enabled_lifetime_type": "2",
            "enabled_lifetime": "0",
            "state": "0",
            "error": "",
            "parameters": []
        },
        {
            "itemid": "27426",
            "type": "0",
            "snmp_oid": "",
            "hostid": "10202",
            "name": "Mounted filesystem discovery",
            "key_": "vfs.fs.discovery",
            "delay": "1h",
            "status": "0",
            "trapper_hosts": "",
            "templateid": "22450",
            "valuemapid": "0",
            "params": "",
            "ipmi_sensor": "",
            "authtype": "0",
            "username": "",
            "password": "",
            "publickey": "",
            "privatekey": "",
            "flags": "1",
            "interfaceid": "119",
            "description": "根据全局正则表达式 \"File systems for discovery\" 定义的不同类型文件系统发现。",
            "lifetime": "30d",
            "jmx_endpoint": "",
            "master_itemid": "0",
            "timeout": "",
            "url": "",
            "query_fields": [],
            "posts": "",
            "status_codes": "200",
            "follow_redirects": "1",
            "post_type": "0",
            "http_proxy": "",
            "headers": [],
            "retrieve_mode": "0",
            "request_method": "0",
            "ssl_cert_file": "",
            "ssl_key_file": "",
            "ssl_key_password": "",
            "verify_peer": "0",
            "verify_host": "0",
            "allow_traps": "0",
            "uuid": "",
            "lifetime_type": "0",
            "enabled_lifetime_type": "2",
            "enabled_lifetime": "0",
            "state": "0",
            "error": "",
            "parameters": []
        }
    ],
    "id": 1
}
```

[comment]: # ({/ab9d9991-5f8b5429})

[comment]: # ({22ef0184-4d0af030})
#### 获取过滤条件

获取 LLD 规则“24681”的名称及其过滤条件。该过滤器使用“and”求值类型，因此 `formula` 属性为空，并且 `eval_formula` 会自动生成。

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

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.get",
    "params": {
        "output": ["name"],
        "selectFilter": "extend",
        "itemids": ["24681"]
    },
    "id": 1
}
```

响应：

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "itemid": "24681",
            "name": "Filtered LLD rule",
            "filter": {
                "evaltype": "1",
                "formula": "",
                "conditions": [
                    {
                        "macro": "{#MACRO1}",
                        "value": "@regex1",
                        "operator": "8",
                        "formulaid": "A"
                    },
                    {
                        "macro": "{#MACRO2}",
                        "value": "@regex2",
                        "operator": "9",
                        "formulaid": "B"
                    },
                    {
                        "macro": "{#MACRO3}",
                        "value": "",
                        "operator": "12",
                        "formulaid": "C"
                    },
                    {
                        "macro": "{#MACRO4}",
                        "value": "",
                        "operator": "13",
                        "formulaid": "D"
                    }
                ],
                "eval_formula": "A and B and C and D"
            }
        }
    ],
    "id": 1
}
```

[comment]: # ({/22ef0184-4d0af030})

[comment]: # ({6830cd5d-e3de6fe8})
#### 通过 URL 获取 LLD 规则

根据规则 URL 字段值获取主机的 LLD 规则。仅支持与 LLD 规则中定义的 URL 字符串完全匹配。

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

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.get",
    "params": {
        "hostids": "10257",
        "filter": {
            "type": 19,
            "url": "http://127.0.0.1/discoverer.php"
        }
    },
    "id": 1
}
```

响应：

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "itemid": "28336",
            "type": "19",
            "snmp_oid": "",
            "hostid": "10257",
            "name": "API HTTP agent",
            "key_": "api_discovery_rule",
            "delay": "5s",
            "status": "0",
            "trapper_hosts": "",
            "templateid": "0",
            "valuemapid": "0",
            "params": "",
            "ipmi_sensor": "",
            "authtype": "0",
            "username": "",
            "password": "",
            "publickey": "",
            "privatekey": "",
            "flags": "1",
            "interfaceid": "5",
            "description": "",
            "lifetime": "30d",
            "jmx_endpoint": "",
            "master_itemid": "0",
            "timeout": "",
            "url": "http://127.0.0.1/discoverer.php",
            "query_fields": [
                {
                    "name": "mode",
                    "value": "json"
                },
                {
                    "name": "elements",
                    "value": "2"
                }
            ],
            "posts": "",
            "status_codes": "200",
            "follow_redirects": "1",
            "post_type": "0",
            "http_proxy": "",
            "headers": [
                {
                    "name" : "X-Type",
                    "value": "api"
                },
                {
                    "name": "Authorization",
                    "value": "Bearer mF_A.B5f-2.1JcM"
                }
            ],
            "retrieve_mode": "0",
            "request_method": "1",
            "ssl_cert_file": "",
            "ssl_key_file": "",
            "ssl_key_password": "",
            "verify_peer": "0",
            "verify_host": "0",
            "allow_traps": "0",
            "uuid": "",
            "lifetime_type": "0",
            "enabled_lifetime_type": "2",
            "enabled_lifetime": "0",
            "state": "0",
            "error": "",
            "parameters": []
        }
    ],
    "id": 1
}
```

[comment]: # ({/6830cd5d-e3de6fe8})

[comment]: # ({cbbe028e-c0ae2d21})
#### 获取带有覆盖项的 LLD 规则

获取一个具有多种覆盖项设置的 LLD 规则。

[请求](/manual/api#performing-requests)：

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.get",
    "params": {
        "output": ["name"],
        "itemids": "30980",
        "selectOverrides": ["name", "step", "stop", "filter", "operations"]
    },
    "id": 1
}
```

响应：

```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "name": "Discover database host",
            "overrides": [
                {
                    "name": "Discover MySQL host",
                    "step": "1",
                    "stop": "1",
                    "filter": {
                        "evaltype": "2",
                        "formula": "",
                        "conditions": [
                            {
                                "macro": "{#UNIT.NAME}",
                                "operator": "8",
                                "value": "^mysqld\\.service$",
                                "formulaid": "A"
                            },
                            {
                                "macro": "{#UNIT.NAME}",
                                "operator": "8",
                                "value": "^mariadb\\.service$",
                                "formulaid": "B"
                            }
                        ],
                        "eval_formula": "A or B"
                    },
                    "operations": [
                        {
                            "operationobject": "3",
                            "operator": "2",
                            "value": "Database host",
                            "opstatus": {
                                "status": "0"
                            },
                            "optag": [
                                {
                                    "tag": "database",
                                    "value": "mysql"
                                }
                            ],
                            "optemplate": [
                                {
                                    "templateid": "10170"
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": "Discover PostgreSQL host",
                    "step": "2",
                    "stop": "1",
                    "filter": {
                        "evaltype": "0",
                        "formula": "",
                        "conditions": [
                            {
                                "macro": "{#UNIT.NAME}",
                                "operator": "8",
                                "value": "^postgresql\\.service$",
                                "formulaid": "A"
                            }
                        ],
                        "eval_formula": "A"
                    },
                    "operations": [
                        {
                            "operationobject": "3",
                            "operator": "2",
                            "value": "Database host",
                            "opstatus": {
                                "status": "0"
                            },
                            "optag": [
                                {
                                    "tag": "database",
                                    "value": "postgresql"
                                }
                            ],
                            "optemplate": [
                                {
                                    "templateid": "10263"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ],
    "id": 1
}
```

[comment]: # ({/cbbe028e-c0ae2d21})

[comment]: # ({590f198c-f25d02bb})
### 另请参阅

-   [图形原型](/manual/api/reference/graphprototype/object#graph-prototype)
-   [主机](/manual/api/reference/host/object#host)
-   [监控项原型](/manual/api/reference/itemprototype/object#item-prototype)
-   [LLD 规则过滤器](object#lld-rule-filter)
-   [触发器原型](/manual/api/reference/triggerprototype/object#trigger-prototype)

[comment]: # ({/590f198c-f25d02bb})

[comment]: # ({e14efed8-e14efed8})
### 源

CDiscoveryRule::get() 位于
*ui/include/classes/api/services/CDiscoveryRule.php*。

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