Save data to OpenTSDB

Setup a OpenTSDB database, taking Mac OSX for instance:

  1. $ docker pull petergrace/opentsdb-docker
  2. $ docker run -d --name opentsdb -p 4242:4242 petergrace/opentsdb-docker

Create a rule:

Go to EMQX DashboardSave data to OpenTSDB - 图1 (opens new window), select the “rule” tab on the menu to the left.

Select “message.publish”, then type in the following SQL:

  1. SELECT
  2. payload.metric as metric, payload.tags as tags, payload.value as value
  3. FROM
  4. "message.publish"

image

Bind an action:

Click on the “+ Add” button under “Action Handler”, and then select “Data to OpenTSDB” in the pop-up dialog window.

image

Fill in the parameters required by the action:

Six parameters is required by action “Data to OpenTSDB”:

1). Details. Whether let OpenTSDB Server return the failed data point and their error reason, defaults to false.

2). Summary. Whether let OpenTSDB Server return data point success/failure count, defaults to true.

3). Max Batch Size. In case of heavy traffic, how many data points are allowed to be included in a single request. Default to 20.

4). Sync Call. Defaults to false.

5). Sync Timeout. Defaults to 0.

image

6). Bind a resource to the action. Since the dropdown list “Resource” is empty for now, we create a new resource by clicking on the “New Resource” to the top right, and then select “OpenTSDB”:

image

Configure the resource:

Keep all the default configs as default, and click on the “Testing Connection” button to make sure the connection can be created successfully, and then click on the “Create” button.

image

Back to the “Actions” dialog, and then click on the “Confirm” button.

image

Back to the creating rule page, then click on “Create” button. The rule we created will be show in the rule list:

image

We have finished, testing the rule by sending an MQTT message to emqx:

  1. > Topic: "t/1"
  2. >
  3. > QoS: 0
  4. >
  5. > Retained: false
  6. >
  7. > Payload: {"metric":"cpu","tags":{"host":"serverA"},"value":12}

Then inspect the OpenTSDB table, verify a new record has been inserted:

  1. ## Use postman to send an HTTP request to the opentsdb server:
  2. POST /api/query HTTP/1.1
  3. Host: 127.0.0.1:4242
  4. Content-Type: application/json
  5. cache-control: no-cache
  6. Postman-Token: 69af0565-27f8-41e5-b0cd-d7c7f5b7a037
  7. {
  8. "start": 1560409825000,
  9. "queries": [
  10. {
  11. "aggregator": "last",
  12. "metric": "cpu",
  13. "tags": {
  14. "host": "*"
  15. }
  16. }
  17. ],
  18. "showTSUIDs": "true",
  19. "showQuery": "true",
  20. "delete": "false"
  21. }
  22. ------WebKitFormBoundary7MA4YWxkTrZu0gW--

The response should look like following:

  1. [
  2. {
  3. "metric": "cpu",
  4. "tags": {
  5. "host": "serverA"
  6. },
  7. "aggregateTags": [],
  8. "query": {
  9. "aggregator": "last",
  10. "metric": "cpu",
  11. "tsuids": null,
  12. "downsample": null,
  13. "rate": false,
  14. "filters": [
  15. {
  16. "tagk": "host",
  17. "filter": "*",
  18. "group_by": true,
  19. "type": "wildcard"
  20. }
  21. ],
  22. "index": 0,
  23. "tags": {
  24. "host": "wildcard(*)"
  25. },
  26. "rateOptions": null,
  27. "filterTagKs": [
  28. "AAAC"
  29. ],
  30. "explicitTags": false
  31. },
  32. "tsuids": [
  33. "000002000002000007"
  34. ],
  35. "dps": {
  36. "1561532453": 12
  37. }
  38. }
  39. ]

And from the rule list, verify that the “Matched” column has increased to 1:

image