discoveryrule.create

Description 说明

object discoveryrule.create(object/array lldRules)

This method allows to create new LLD rules.此方法允许创建新的LLD规则。

Parameters 参数

(object/array) LLD rules to create.

Additionally to the standard LLD rule properties, the method accepts the following parameters.

ParameterTypeDescription
filterobjectLLD rule filter object for the LLD rule.

Return values

(object) Returns an object containing the IDs of the created LLD rules under the itemids property. The order of the returned IDs matches the order of the passed LLD rules. (object)itemids属性下返回一个包含IDs的被创建的LLD规则。返回的IDs的顺序与传递的LLD规则顺序相匹配。

Examples 示例

Creating an LLD rule

Create a Zabbix agent LLD rule to discover mounted file systems. Discovered items will be updated every 30 seconds. 创建Zabbix agent LLD规则去发现以已装入的文件系统。发现items将被每30s被更新一次。

Request:

  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. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  13. "id": 1
  14. }

Response:

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

Using a filter

Create an LLD rule with a set of conditions to filter the results by. The conditions will be grouped together using the logical “and” operator. 创建有由一套删选条件的得到的LLD规则。这些条件将使用逻辑“和”运算符将条件组合在一起。

Request:

  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. },
  22. {
  23. "macro": "{#MACRO3}",
  24. "value": "@regex3"
  25. }
  26. ]
  27. }
  28. },
  29. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  30. "id": 1
  31. }

Response:

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

Using a custom expression filter 使用自定义表达式的筛选器

Create an LLD rule with a filter that will use a custom expression to evaluate the conditions. The LLD rule must only discover objects the “{#MACRO1}” macro value of which matches both regular expression “regex1” and “regex2”, and the value of “{#MACRO2}” matches either “regex3” or “regex4”. The formula IDs “A”, “B”, “C” and “D” have been chosen arbitrarily.

Request:

  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. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  39. "id": 1
  40. }

Response:

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

Using custom query fields and headers 使用自定义查询字段和报头

Create LLD rule with custom query fields and headers.

Request:

  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. "mode": "json"
  16. },
  17. {
  18. "elements":"2"
  19. }
  20. ],
  21. "headers": {
  22. "X-Type": "api",
  23. "Authorization": "Bearer mF_A.B5f-2.1JcM"
  24. },
  25. "allow_traps": "1",
  26. "trapper_hosts": "127.0.0.1",
  27. "id": 35,
  28. "auth": "d678e0b85688ce578ff061bd29a20d3b",
  29. }
  30. }

Response:

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

See also

Source

CDiscoveryRule::create() in frontends/php/include/classes/api/services/CDiscoveryRule.php.

Creating a LLD rule with overrides

Request:

  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. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  93. "id": 1
  94. }

Response:

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

Create script LLD rule

Create a simple data collection using a script LLD rule.

Request:

  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 CurlHttpRequest();\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. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  19. "id": 2
  20. }

Response:

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

See also

Source

CDiscoveryRule::create() in ui/include/classes/api/services/CDiscoveryRule.php.