2022 Zabbix中国峰会
2022 Zabbix中国峰会

创建

描述

object discoveryrule.create(object/array lldRules)

此方法允许创建新的 LLD 规则。

此方法只有 Admin(管理员)Super admin(超级管理员) 用户可用。可以在用户角色设置中撤销调用该方法的权限。更多信息请查看用户角色

参数

(object/array) 要创建的LLD规则。

除了标准LLD规则属性,该方法还接受以下参数。

参数 类型 描述
filter object LLD规则过滤器
preprocessing object/array LLD规则预处理选项。
lld_macro_paths object/array LLD规则lld_macro_path选项。
overrides object/array LLD规则覆盖选项。

返回值

(object) 返回一个对象,其中包含在 itemids 属性下创建的 LLD 规则的 ID。返回 ID 的顺序与传递的 LLD 规则的顺序相匹配。

示例

创建一个 LLD 规则

创建 Zabbix agent LLD规则以发现挂载的文件系统。 发现的监控项将每 30 秒更新一次。

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "name": "Mounted filesystem discovery",
               "key_": "vfs.fs.discovery",
               "hostid": "10197",
               "type": 0,
               "interfaceid": "112",
               "delay": "30s"
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "27665"
               ]
           },
           "id": 1
       }

使用过滤器

使用一组条件创建 LLD 规则以过滤结果。 条件将使用逻辑“和”运算符组合在一起。

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "name": "Filtered LLD rule",
               "key_": "lld",
               "hostid": "10116",
               "type": 0,
               "interfaceid": "13",
               "delay": "30s",
               "filter": {
                   "evaltype": 1,
                   "conditions": [
                       {
                           "macro": "{#MACRO1}",
                           "value": "@regex1"
                       },
                       {
                           "macro": "{#MACRO2}",
                           "value": "@regex2",
                           "operator": "9"
                       },
                       {
                           "macro": "{#MACRO3}",
                           "value": "",
                           "operator": "12"
                       },
                       {
                           "macro": "{#MACRO4}",
                           "value": "",
                           "operator": "13"
                       }
                   ]
               }
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "27665"
               ]
           },
           "id": 1
       }

使用宏路径创建 LLD 规则

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "name": "LLD rule with LLD macro paths",
               "key_": "lld",
               "hostid": "10116",
               "type": 0,
               "interfaceid": "13",
               "delay": "30s",
               "lld_macro_paths": [
                   {
                       "lld_macro": "{#MACRO1}",
                       "path": "$.path.1"
                   },
                   {
                       "lld_macro": "{#MACRO2}",
                       "path": "$.path.2"
                   }
               ]
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "27665"
               ]
           },
           "id": 1
       }

使用自定义表达式过滤器

创建一个带有过滤器的LLD规则,该过滤器将使用自定义表达式来评估条件。LLD规则必须仅发现对象,其中“{#MACRO1}”宏的值同时匹配正则表达式“regex1”和“regex2”,并且“{#MACRO2}”的值匹配“regex3”或“regex4”。公式ID“A”,“B”,“C”和“D”是任意选择的。

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "name": "Filtered LLD rule",
               "key_": "lld",
               "hostid": "10116",
               "type": 0,
               "interfaceid": "13",
               "delay": "30s",
               "filter": {
                   "evaltype": 3,
                   "formula": "(A and B) and (C or D)",
                   "conditions": [
                       {
                           "macro": "{#MACRO1}",
                           "value": "@regex1",
                           "formulaid": "A"
                       },
                       {
                           "macro": "{#MACRO1}",
                           "value": "@regex2",
                           "formulaid": "B"
                       },
                       {
                           "macro": "{#MACRO2}",
                           "value": "@regex3",
                           "formulaid": "C"
                       },
                       {
                           "macro": "{#MACRO2}",
                           "value": "@regex4",
                           "formulaid": "D"
                       }
                   ]
               }
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "27665"
               ]
           },
           "id": 1
       }

使用自定义查询字段和标题

使用自定义查询字段和标题创建 LLD 规则。

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "hostid": "10257",
               "interfaceid": "5",
               "type": 19,
               "name": "API HTTP agent",
               "key_": "api_discovery_rule",
               "value_type": 3,
               "delay": "5s",
               "url": "http://127.0.0.1?discoverer.php",
               "query_fields": [
                   {
                       "name": "mode",
                       "value": "json"
                   },
                   {
                       "name": "elements",
                       "value": "2"
                   }
               ],
               "headers": [
                   {
                       "name": "X-Type",
                       "value": "api"
                   },
                   {
                       "name": "Authorization",
                       "value": "Bearer mF_A.B5f-2.1JcM"
                   }
               ],
               "allow_traps": 1,
               "trapper_hosts": "127.0.0.1"
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "28336"
               ]
           },
           "id": 1
       }

####创建一个带有预处理的LLD规则。

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "name": "Discovery rule with preprocessing",
               "key_": "lld.with.preprocessing",
               "hostid": "10001",
               "ruleid": "27665",
               "type": 0,
               "value_type": 3,
               "delay": "60s",
               "interfaceid": "1155",
               "preprocessing": [
                   {
                       "type": 20,
                       "params": "20",
                       "error_handler": 0,
                       "error_handler_params": ""
                   }
               ]
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "44211"
               ]
           },
           "id": 1
       }

创建一个带有覆盖设置的LLD规则。

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "name": "Discover database host",
               "key_": "lld.with.overrides",
               "hostid": "10001",
               "type": 0,
               "value_type": 3,
               "delay": "60s",
               "interfaceid": "1155",
               "overrides": [
                   {
                       "name": "Discover MySQL host",
                       "step": "1",
                       "stop": "1",
                       "filter": {
                           "evaltype": "2",
                           "conditions": [
                               {
                                   "macro": "{#UNIT.NAME}",
                                   "operator": "8",
                                   "value": "^mysqld\\.service$"
                               },
                               {
                                   "macro": "{#UNIT.NAME}",
                                   "operator": "8",
                                   "value": "^mariadb\\.service$"
                               }
                           ]
                       },
                       "operations": [
                           {
                               "operationobject": "3",
                               "operator": "2",
                               "value": "Database host",
                               "opstatus": {
                                   "status": "0"
                               },
                               "optemplate": [
                                   {
                                       "templateid": "10170"
                                   }
                               ],
                               "optag": [
                                   {
                                       "tag": "Database",
                                       "value": "MySQL"
                                   }
                               ]
                           }
                       ]
                   },
                   {
                       "name": "Discover PostgreSQL host",
                       "step": "2",
                       "stop": "1",
                       "filter": {
                           "evaltype": "0",
                           "conditions": [
                               {
                                   "macro": "{#UNIT.NAME}",
                                   "operator": "8",
                                   "value": "^postgresql\\.service$"
                               }
                           ]
                       },
                       "operations": [
                           {
                               "operationobject": "3",
                               "operator": "2",
                               "value": "Database host",
                               "opstatus": {
                                   "status": "0"
                               },
                               "optemplate": [
                                   {
                                       "templateid": "10263"
                                   }
                               ],
                               "optag": [
                                   {
                                       "tag": "Database",
                                       "value": "PostgreSQL"
                                   }
                               ]
                           }
                       ]
                   }
               ]
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "30980"
               ]
           },
           "id": 1
       }

创建脚本 LLD 规则

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "name": "Script example",
               "key_": "custom.script.lldrule",
               "hostid": "12345",
               "type": 21,
               "value_type": 4,
               "params": "var request = new HttpRequest();\nreturn request.post(\"https://postman-echo.com/post\", JSON.parse(value));",
               "parameters": [{
                   "name": "host",
                   "value": "{HOST.CONN}"
               }],
               "timeout": "6s",
               "delay": "30s"
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "23865"
               ]
           },
           "id": 1
       }

创建一个具有特定禁用时间段的LLD规则,且不删除规则。

创建一个自定义禁用时间周期的LLD规则,用于在实体不再被发现后禁用它,并设置为永不删除。

请求

{
           "jsonrpc": "2.0",
           "method": "discoveryrule.create",
           "params": {
               "name": "lld disable after 1h",
               "key_": "lld.disable",
               "hostid": "10001",
               "type": 2,
               "lifetime_type": 1,
               "enabled_lifetime_type": 0, 
               "enabled_lifetime": "1h"
           },
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "itemids": [
                   "46864"
               ]
           },
           "id": 1
       }

参见

来源

ui/include/classes/api/services/CDiscoveryRule.php 中的 CDiscoveryRule::create()。