创建

描述

object discoveryrule.create(object/array lldRules)

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

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

参数

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

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

参数类型描述
filterobjectLLD规则过滤器
preprocessingobject/arrayLLD规则预处理选项。
lld_macro_pathsobject/arrayLLD规则lld_macro_path选项。
overridesobject/arrayLLD规则覆盖选项。

返回值

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

示例

创建一个 LLD 规则

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

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "name": "Mounted filesystem discovery",
  6. "key_": "vfs.fs.discovery",
  7. "hostid": "10197",
  8. "type": 0,
  9. "interfaceid": "112",
  10. "delay": "30s"
  11. },
  12. "id": 1
  13. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "27665"
  6. ]
  7. },
  8. "id": 1
  9. }

使用过滤器

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

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "name": "Filtered LLD rule",
  6. "key_": "lld",
  7. "hostid": "10116",
  8. "type": 0,
  9. "interfaceid": "13",
  10. "delay": "30s",
  11. "filter": {
  12. "evaltype": 1,
  13. "conditions": [
  14. {
  15. "macro": "{#MACRO1}",
  16. "value": "@regex1"
  17. },
  18. {
  19. "macro": "{#MACRO2}",
  20. "value": "@regex2",
  21. "operator": "9"
  22. },
  23. {
  24. "macro": "{#MACRO3}",
  25. "value": "",
  26. "operator": "12"
  27. },
  28. {
  29. "macro": "{#MACRO4}",
  30. "value": "",
  31. "operator": "13"
  32. }
  33. ]
  34. }
  35. },
  36. "id": 1
  37. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "27665"
  6. ]
  7. },
  8. "id": 1
  9. }

使用宏路径创建 LLD 规则

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "name": "LLD rule with LLD macro paths",
  6. "key_": "lld",
  7. "hostid": "10116",
  8. "type": 0,
  9. "interfaceid": "13",
  10. "delay": "30s",
  11. "lld_macro_paths": [
  12. {
  13. "lld_macro": "{#MACRO1}",
  14. "path": "$.path.1"
  15. },
  16. {
  17. "lld_macro": "{#MACRO2}",
  18. "path": "$.path.2"
  19. }
  20. ]
  21. },
  22. "id": 1
  23. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "27665"
  6. ]
  7. },
  8. "id": 1
  9. }

使用自定义表达式过滤器

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

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "name": "Filtered LLD rule",
  6. "key_": "lld",
  7. "hostid": "10116",
  8. "type": 0,
  9. "interfaceid": "13",
  10. "delay": "30s",
  11. "filter": {
  12. "evaltype": 3,
  13. "formula": "(A and B) and (C or D)",
  14. "conditions": [
  15. {
  16. "macro": "{#MACRO1}",
  17. "value": "@regex1",
  18. "formulaid": "A"
  19. },
  20. {
  21. "macro": "{#MACRO1}",
  22. "value": "@regex2",
  23. "formulaid": "B"
  24. },
  25. {
  26. "macro": "{#MACRO2}",
  27. "value": "@regex3",
  28. "formulaid": "C"
  29. },
  30. {
  31. "macro": "{#MACRO2}",
  32. "value": "@regex4",
  33. "formulaid": "D"
  34. }
  35. ]
  36. }
  37. },
  38. "id": 1
  39. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "27665"
  6. ]
  7. },
  8. "id": 1
  9. }

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

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

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "hostid": "10257",
  6. "interfaceid": "5",
  7. "type": 19,
  8. "name": "API HTTP agent",
  9. "key_": "api_discovery_rule",
  10. "value_type": 3,
  11. "delay": "5s",
  12. "url": "http://127.0.0.1?discoverer.php",
  13. "query_fields": [
  14. {
  15. "name": "mode",
  16. "value": "json"
  17. },
  18. {
  19. "name": "elements",
  20. "value": "2"
  21. }
  22. ],
  23. "headers": [
  24. {
  25. "name": "X-Type",
  26. "value": "api"
  27. },
  28. {
  29. "name": "Authorization",
  30. "value": "Bearer mF_A.B5f-2.1JcM"
  31. }
  32. ],
  33. "allow_traps": 1,
  34. "trapper_hosts": "127.0.0.1"
  35. },
  36. "id": 1
  37. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "28336"
  6. ]
  7. },
  8. "id": 1
  9. }

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

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "name": "Discovery rule with preprocessing",
  6. "key_": "lld.with.preprocessing",
  7. "hostid": "10001",
  8. "ruleid": "27665",
  9. "type": 0,
  10. "value_type": 3,
  11. "delay": "60s",
  12. "interfaceid": "1155",
  13. "preprocessing": [
  14. {
  15. "type": 20,
  16. "params": "20",
  17. "error_handler": 0,
  18. "error_handler_params": ""
  19. }
  20. ]
  21. },
  22. "id": 1
  23. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "44211"
  6. ]
  7. },
  8. "id": 1
  9. }

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

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "name": "Discover database host",
  6. "key_": "lld.with.overrides",
  7. "hostid": "10001",
  8. "type": 0,
  9. "value_type": 3,
  10. "delay": "60s",
  11. "interfaceid": "1155",
  12. "overrides": [
  13. {
  14. "name": "Discover MySQL host",
  15. "step": "1",
  16. "stop": "1",
  17. "filter": {
  18. "evaltype": "2",
  19. "conditions": [
  20. {
  21. "macro": "{#UNIT.NAME}",
  22. "operator": "8",
  23. "value": "^mysqld\\.service$"
  24. },
  25. {
  26. "macro": "{#UNIT.NAME}",
  27. "operator": "8",
  28. "value": "^mariadb\\.service$"
  29. }
  30. ]
  31. },
  32. "operations": [
  33. {
  34. "operationobject": "3",
  35. "operator": "2",
  36. "value": "Database host",
  37. "opstatus": {
  38. "status": "0"
  39. },
  40. "optemplate": [
  41. {
  42. "templateid": "10170"
  43. }
  44. ],
  45. "optag": [
  46. {
  47. "tag": "Database",
  48. "value": "MySQL"
  49. }
  50. ]
  51. }
  52. ]
  53. },
  54. {
  55. "name": "Discover PostgreSQL host",
  56. "step": "2",
  57. "stop": "1",
  58. "filter": {
  59. "evaltype": "0",
  60. "conditions": [
  61. {
  62. "macro": "{#UNIT.NAME}",
  63. "operator": "8",
  64. "value": "^postgresql\\.service$"
  65. }
  66. ]
  67. },
  68. "operations": [
  69. {
  70. "operationobject": "3",
  71. "operator": "2",
  72. "value": "Database host",
  73. "opstatus": {
  74. "status": "0"
  75. },
  76. "optemplate": [
  77. {
  78. "templateid": "10263"
  79. }
  80. ],
  81. "optag": [
  82. {
  83. "tag": "Database",
  84. "value": "PostgreSQL"
  85. }
  86. ]
  87. }
  88. ]
  89. }
  90. ]
  91. },
  92. "id": 1
  93. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "30980"
  6. ]
  7. },
  8. "id": 1
  9. }

创建脚本 LLD 规则

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "name": "Script example",
  6. "key_": "custom.script.lldrule",
  7. "hostid": "12345",
  8. "type": 21,
  9. "value_type": 4,
  10. "params": "var request = new HttpRequest();\nreturn request.post(\"https://postman-echo.com/post\", JSON.parse(value));",
  11. "parameters": [{
  12. "name": "host",
  13. "value": "{HOST.CONN}"
  14. }],
  15. "timeout": "6s",
  16. "delay": "30s"
  17. },
  18. "id": 1
  19. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "23865"
  6. ]
  7. },
  8. "id": 1
  9. }

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

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

请求

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "discoveryrule.create",
  4. "params": {
  5. "name": "lld disable after 1h",
  6. "key_": "lld.disable",
  7. "hostid": "10001",
  8. "type": 2,
  9. "lifetime_type": 1,
  10. "enabled_lifetime_type": 0,
  11. "enabled_lifetime": "1h"
  12. },
  13. "id": 1
  14. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "itemids": [
  5. "46864"
  6. ]
  7. },
  8. "id": 1
  9. }

参见

来源

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