规则

查询规则引擎的动作

GET /api/v4/rules/{rule_id}

获取某个规则的详情,包括规则的 SQL、Topics 列表、动作列表等。还会返回当前规则和动作的统计指标的值。

Path Parameters:

NameTypeRequiredDescription
rule_idStringFalse可选,Rule ID。如不指定 rule_id 则
以数组形式返回所有已创建的规则

Success Response Body (JSON):

NameTypeDescription
codeInteger0
dataObject规则对象
- data.idStringRule ID
- data.rawsqlStringSQL 语句,与请求中的 rawsql 一致
- data.forStringTopic 列表,表示哪些 topic 可以匹配到此规则
- data.metricsArray统计指标,具体可参看 Dashboard 上的 Rule Metrics
- data.descriptionString规则的描述信息,与请求中的 description 一致
- data.actionsArray动作列表
- data.actions[0].idStringAction ID
- data.actions[0].paramsObject动作参数,与请求中的 actions.params 一致
- data.actions[0].nameString动作名字,与请求中的 actions.name 一致
- data.actions[0].metricsArray统计指标,具体可参看 Dashboard 上的 Rule Metrics

POST /api/v4/rules

创建规则,返回规则 ID。

Parameters (json):

NameTypeRequiredDescription
rawsqlStringTrue规则的 SQL 语句
actionsArrayTrue动作列表
- actions[0].nameStringTrue动作名称
- actions[0].paramsObjectTrue动作参数。参数以 key-value 形式表示。
详情可参看添加规则的示例
descriptionStringFalse可选,规则描述

Success Response Body (JSON):

NameTypeDescription
codeInteger0
dataObject创建成功的规则对象,包含 Rule ID
- data.idStringRule ID
- data.rawsqlStringSQL 语句,与请求中的 rawsql 一致
- data.forStringTopic 列表,表示哪些 topic 可以匹配到此规则
- data.metricsArray统计指标,具体可参看 Dashboard 上的 Rule Metrics
- data.descriptionString规则的描述信息,与请求中的 description 一致
- data.actionsArray动作列表,每个动作是一个 Object
- data.actions[0].idStringAction ID
- data.actions[0].paramsObject动作参数,与请求中的 actions.params 一致
- data.actions[0].nameString动作名字,与请求中的 actions.name 一致
- data.actions[0].metricsArray统计指标,具体可参看 Dashboard 上的 Rule Metrics

DELETE /api/v4/rules/{rule_id}

删除规则。

Parameters:

Success Response Body (JSON):

NameTypeDescription
codeInteger0

Examples:

添加一个规则,对于所有匹配到主题 “t/a” 的消息,打印其规则运行参数。

  1. $ curl -XPOST -d '{
  2. "rawsql": "select * from \"t/a\"",
  3. "actions": [{
  4. "name": "inspect",
  5. "params": {
  6. "a": 1
  7. }
  8. }],
  9. "description": "test-rule"
  10. }' --basic -u admin:public 'http://localhost:8081/api/v4/rules'
  11. {"data":{"rawsql":"select * from \"t/a\"","metrics":[{"speed_max":0,"speed_last5m":0.0,"speed":0.0,"node":"emqx@127.0.0.1","matched":0}],"id":"rule:7fdb2c9e","for":["t/a"],"enabled":true,"description":"test-rule","actions":[{"params":{"a":1},"name":"inspect","metrics":[{"success":0,"node":"emqx@127.0.0.1","failed":0}],"id":"inspect_1582434715354188116"}]},"code":0}

使用规则 ID 获取刚才创建的规则详情:

  1. $ curl --basic -u admin:public 'http://localhost:8081/api/v4/rules/rule:7fdb2c9e'
  2. {"data":{"rawsql":"select * from \"t/a\"","metrics":[{"speed_max":0,"speed_last5m":0.0,"speed":0.0,"node":"emqx@127.0.0.1","matched":0}],"id":"rule:7fdb2c9e","for":["t/a"],"enabled":true,"description":"test-rule","actions":[{"params":{"a":1},"name":"inspect","metrics":[{"success":0,"node":"emqx@127.0.0.1","failed":0}],"id":"inspect_1582434715354188116"}]},"code":0}

获取所有的规则,注意返回值里的 data 是个规则对象的数组:

  1. $ curl --basic -u admin:public 'http://localhost:8081/api/v4/rules'
  2. {"data":[{"rawsql":"select * from \"t/a\"","metrics":[{"speed_max":0,"speed_last5m":0.0,"speed":0.0,"node":"emqx@127.0.0.1","matched":0}],"id":"rule:7fdb2c9e","for":["t/a"],"enabled":true,"description":"test-rule","actions":[{"params":{"a":1},"name":"inspect","metrics":[{"success":0,"node":"emqx@127.0.0.1","failed":0}],"id":"inspect_1582434715354188116"}]}],"code":0}

删除规则:

  1. $ curl -XDELETE --basic -u admin:public 'http://localhost:8081/api/v4/rules/rule:7fdb2c9e'
  2. {"code":0}