|
| 1 | +# Send data securely to Arm Treasure Data |
| 2 | + |
| 3 | +This tutorial introduces securely sending data from Mbed OS devices to Arm Treasure Data using two methods: |
| 4 | + |
| 5 | +- HTTPS library - Send data directly to the Treasure Data REST API. |
| 6 | +- Fluentd using fluent logger library - Send data to a hosted Fluentd instance that aggregates and forwards the data on to your treasure data account. |
| 7 | + |
| 8 | +Arm Mbed TLS provides equal in-transit security to both libraries. We recommend the HTTPS library for development and the Fluentd library for production. The tradeoff between the two is size of code on chip, size of data in transit and setup complexity: |
| 9 | + |
| 10 | +- Code size on chip - The HTTPS library is ~50KB of ROM space on chip. This is due to the HTTP stack. Both libraries use Mbed TLS to secure the connections, which is ~7KB per connection on your stack for both libraries. |
| 11 | +- Data size in transit - The HTTPS library sends data as an ASCII JSON string. The Fluend library uses MessagePack (binary encoded JSON) across a TLS connection. This means that on average the Fluentd library uses less bandwidth to send an equivalent message. When you pay per byte transmitted from both your power budget and data plan it matters. |
| 12 | +- Maintenance - Initially, setting up the HTTPS library on a device and having it send data directly to Treasure Data is easier, but what if you want to change what the device is doing or how its data is reported? If you are using the HTTPS library, you must issue a firmware update to every device to change how it formats its data, whereas if you are using a Fluend server, you can modify the Fluentd config file on the server to change how data is formatted and processed. |
| 13 | + |
| 14 | +The following steps show how to send data using first the HTTPS library and then Fluentd. |
| 15 | + |
| 16 | +## HTTPS library |
| 17 | + |
| 18 | +To use the HTTPS library, use the [mbed-os-example-treasuredata-rest](https://github.com/blackstoneengineering/mbed-os-example-treasuredata-rest) program. This program turns on Mbed OS device statistics by enabling the `MBED_ALL_STATS_ENABLED` macro and then sends heap/CPU/stack/system information to Treasure Data. |
| 19 | + |
| 20 | +<span class="images">[](https://youtu.be/ky-HDNFZw8w)</span> |
| 21 | + |
| 22 | +### Import code |
| 23 | + |
| 24 | +You can compile the program using any of the following development tools: |
| 25 | + |
| 26 | +- Arm Mbed CLI (offline): |
| 27 | + |
| 28 | +``` |
| 29 | +mbed import https://github.com/blackstoneengineering/mbed-os-example-treasuredata-rest |
| 30 | +``` |
| 31 | + |
| 32 | +- Arm Online Compiler: |
| 33 | + |
| 34 | +[](https://github.com/BlackstoneEngineering/mbed-os-example-treasuredata-rest/blob/master/main.cpp) |
| 35 | + |
| 36 | +Or use the link [ide.mbed.com/compiler?import=https://github.com/blackstoneengineering/mbed-os-example-treasuredata-rest](ide.mbed.com/compiler?import=https://github.com/blackstoneengineering/mbed-os-example-treasuredata-rest) |
| 37 | + |
| 38 | + |
| 39 | +### Set up variables |
| 40 | + |
| 41 | +1. Configure the Treasure Data API key in `mbed_app.json` by changing the `api-key` variable: |
| 42 | + |
| 43 | + ``` |
| 44 | + "api-key":{ |
| 45 | +
|
| 46 | +
|
| 47 | + "help": "REST API Key for Treasure Data", |
| 48 | +
|
| 49 | + "value": "\"REPLACE_WITH_YOUR_KEY\"" |
| 50 | +
|
| 51 | + }, |
| 52 | +
|
| 53 | + ``` |
| 54 | + |
| 55 | +1. Wi-Fi credentials: If you're using Wi-Fi, add your SSID/password. If you are using ethernet, you do not need to add Wi-Fi credentials. |
| 56 | + |
| 57 | +1. Create a database called `test_database` in Treasure Data. |
| 58 | + <span class="notes">**Note:** The tables are created automatically.</span> |
| 59 | + |
| 60 | +### Compile and load |
| 61 | + |
| 62 | +Next, you can compile and load your code onto your board. If you are not familiar with how to compile and load code, please look at the Mbed OS quick start tutorial. |
| 63 | + |
| 64 | +After you have compiled your code and loaded it onto your board, open a serial terminal, and connect it to the board. View the output: |
| 65 | + |
| 66 | +``` |
| 67 | +--- Terminal on /dev/tty.usbmodem146103 - 9600,8,N,1 --- |
| 68 | +Treasure Data REST API Demo |
| 69 | +Connecting to the network using the default network interface... |
| 70 | +Connected to the network successfully. IP address: 192.168.43.202 |
| 71 | +Success |
| 72 | +
|
| 73 | +MAC: C4:7F:51:02:D9:5D |
| 74 | +IP: 192.168.43.202 |
| 75 | +Netmask: 255.255.255.0 |
| 76 | +Gateway: 192.168.43.249 |
| 77 | +
|
| 78 | + Sending CPU Data: '{"uptime":6918609,"idle_time":0,"sleep_time":509277,"deep_sleep_time":0}' |
| 79 | +
|
| 80 | + Sending Heap Data: '{"current_size":15260,"max_size":75334,"total_size":747954,"reserved_size":307232,"alloc_cnt":12,"alloc_fail_cnt":0}' |
| 81 | +
|
| 82 | + Sending Stack Data: '{"thread_id":0,"max_size":4820,"reserved_size":12632,"stack_cnt":4}' |
| 83 | +
|
| 84 | + Sending System Data: '{"os_version":51104,"cpu_id":1091551809,"compiler_id":2,"compiler_version":60300}' |
| 85 | +
|
| 86 | +``` |
| 87 | + |
| 88 | +### Verify data in Treasure Data |
| 89 | + |
| 90 | +Go to the [Database list in Treasure Data](https://console.treasuredata.com/app/databases), and open the `test_database` you created earlier. You can see the data from the board in the database. There is a 3- to 5-minute delay from when the data is sent to the database until the visualization system lets you see it, so please be patient, and wait for it to arrive. Be sure to refresh the page. |
| 91 | + |
| 92 | +<span class="images"></span> |
| 93 | + |
| 94 | +<span class="notes">**Note:** The database tab shows how much data you have in the database and gives a few samples, but it does not show all your data. For that, you need to run queries.</span> |
| 95 | + |
| 96 | +### Run queries |
| 97 | + |
| 98 | +Now that you have data in Treasure Data, it's time to analyze and use it. |
| 99 | + |
| 100 | +1. Go to the [Queries tab] (https://console.treasuredata.com/app/queries/editor). |
| 101 | +2. Select the `test_database`, and run some queries. To learn more about how to run queries, please read the [Treasure Data documentation](https://support.treasuredata.com/hc/en-us/articles/360007995693). |
| 102 | + |
| 103 | +#### Select all fields |
| 104 | + |
| 105 | +Run `select * from cpu_info` to get a full list of all fields in the table. |
| 106 | + |
| 107 | +<span class="images"></span> |
| 108 | + |
| 109 | +#### Select certain fields, order by time |
| 110 | + |
| 111 | +This query selects only certain columns from the table and orders them by the time field in ascending value. You can also replace `asc` with `desc` to get the order reversed. |
| 112 | + |
| 113 | +``` |
| 114 | +select time, current_size, total_size, alloc_cnt, max_size, reserved_size, alloc_fail_cnt from heap_info |
| 115 | +order by time asc; |
| 116 | +``` |
| 117 | + |
| 118 | +<span class="images"></span> |
| 119 | + |
| 120 | +### Troubleshooting |
| 121 | + |
| 122 | +If you experience issues, ensure you have at least 10KB of space left on your stack. You can also change the `TD_DEBUG` macro to `true` to turn on the Treasure Data debug printfs. |
| 123 | + |
| 124 | +## Fluentd |
| 125 | + |
| 126 | +For mass deployments, we recommend you use Fluentd or fluentbit to aggregate and forward the data into Treasure Data. Depending on where you host your Fluentd instance, you will need to follow slightly different setup instructions. (localhost on your machine with self signed certificates or at a public IP address in the cloud with Certificate Authority (CA) signed certificates). This example uses MessagePack (a binary encoded JSON) to encode the data. |
| 127 | + |
| 128 | +<span class="images"></span> |
| 129 | + |
| 130 | +### Set up Fluentd |
| 131 | + |
| 132 | +#### Install |
| 133 | + |
| 134 | +First, install Fluentd. Please see the [Fluentd quick start](https://docs.fluentd.org/v1.0/articles/quickstart) for details. |
| 135 | + |
| 136 | +Experienced users can use `gem install Fluentd fluent-plugin-td`. |
| 137 | + |
| 138 | +#### Download example code |
| 139 | + |
| 140 | +Download the [example code](https://github.com/BlackstoneEngineering/mbed-os-example-fluentlogger). This repository contains both the embedded example code and the Fluentd configuration files. |
| 141 | + |
| 142 | +#### Set configuration file |
| 143 | + |
| 144 | +Run Fluentd using the provided configuration file `fluentd --config ./fluentd-setup/fluentd.conf -vv`. This file opens two ports: port 24227 for unencrypted TCP traffic and port 24228 for TLS encrypted traffic. The configuration is provided for reference. We strongly suggest using TLS encryption on port 24228 to secure your data in transit. |
| 145 | + |
| 146 | +You can either run Fluentd on a public IP address with CA signed certificates (suggested for deployments), or locally on your machine using self signed certificates (recommended for prototyping/testing). |
| 147 | + |
| 148 | +##### Signed by CA, running in cloud |
| 149 | + |
| 150 | +If you have valid certificates from a CA, replace the `fluentd.crt` and `fluentd.key` files with the CA certificates. Then uncomment the lines in the `fluentd.conf` file for CA trusted certificates, comment out the lines for self-signed certificates and change the passphrase to match for your certificate: |
| 151 | + |
| 152 | +``` |
| 153 | + # cert_path ~/mbed-os-example-fluentlogger/fluentd-setup/fluentd.crt |
| 154 | + # private_key_path ~mbed-os-example-fluentlogger/fluentd-setup/fluentd.key |
| 155 | + # private_key_passphrase YOUR_PASSPHRASE |
| 156 | +``` |
| 157 | + |
| 158 | +##### Self-signed certificates on localhost |
| 159 | + |
| 160 | +<span class="images">[](https://youtu.be/DpnNaVxEhvA)</span> |
| 161 | + |
| 162 | +If you are running the Fluentd server locally on your machine to develop a proof of concept (PoC), you need to generate a new self-signed certificate (where the Common Name (CN) is the IP address of your machine), and modify the `fluentd.conf` file with the IP address of your machine. Each time you restart the Fluentd instance, it generates a new certificate that you need to copy and paste into your embedded code. |
| 163 | + |
| 164 | +1. Change the `generate_cert_common_name` parameter in `fluentd.conf` to be the IP address of the computer running the Fluentd server. |
| 165 | +1. Run ` openssl req -new -x509 -sha256 -days 1095 -newkey rsa:2048 -keyout fluentd.key -out fluentd.crt` to generate new certificates. When entering the prompted values, make sure to match the parameters in the `fluentd.conf` file (US, CA, Mountain View and so on). **Make sure the CN field is set to the IP address of the Fluentd server**. |
| 166 | + |
| 167 | +For example: |
| 168 | + |
| 169 | +``` |
| 170 | +Country Name (2 letter code) []:US |
| 171 | +State or Province Name (full name) []:CA |
| 172 | +Locality Name (eg, city) []:Mountain View |
| 173 | +Organization Name (eg, company) []: |
| 174 | +Organizational Unit Name (eg, section) []: |
| 175 | +Common Name (eg, fully qualified host name) []:192.168.1.85 |
| 176 | +Email Address []: |
| 177 | +``` |
| 178 | + |
| 179 | +<span class="images"></span> |
| 180 | + |
| 181 | +### Mbed OS setup |
| 182 | + |
| 183 | +Run the example code on your device. You can either [import to the Mbed Online Compiler](http://os.mbed.com/compiler/?import=https%3A%2F%2Fi.8713187.xyz%2FBlackstoneEngineering%2Fmbed-os-example-fluentlogger) or use Mbed CLI to clone it locally, compile and load it to the board: |
| 184 | + |
| 185 | +```shell |
| 186 | +$ mbed import https://github.com/BlackstoneEngineering/mbed-os-example-fluentlogger |
| 187 | +$ mbed compile --target auto --toolchain GCC_ARM --flash --sterm |
| 188 | +``` |
| 189 | + |
| 190 | +#### Secure (TLS) |
| 191 | + |
| 192 | +To send data to Fluentd over TLS (securely): |
| 193 | + |
| 194 | +1. Run `openssl s_client -connect localhost:24228 -showcerts`. |
| 195 | +1. Copy the certificate to `fluentd-sslcert.h`. If you are running the Fluentd server on localhost, this certificate will change every time you restart Fluentd. You need to rerun this command and recompile your embedded code every time you restart Fluentd. |
| 196 | +1. Modify the call in `main.cpp` to the FluentLogger object. |
| 197 | +1. Change the IP address to the IP address of the Fluentd server, or if you are hosting it in the cloud, change it to the web address where it is hosted. **It is important that the IP address in the main.cpp file matches the IP address set in the CN field of the Fluentd server. Otherwise, it will not work because Mbed TLS uses strict CN verification.** |
| 198 | +1. Compile the code and load it onto your board. |
| 199 | + |
| 200 | +### Success |
| 201 | + |
| 202 | +Successful output on the Fluentd terminal: |
| 203 | + |
| 204 | +<span class="images"></span> |
| 205 | + |
| 206 | + |
| 207 | +```sterm |
| 208 | + -0500 debug.test: ["sint",0,1,-1,-128,-32768,-2147483648] |
| 209 | + -0500 [trace]: #0 fluent/log.rb:281:trace: connected fluent socket addr="192.168.1.95" port=5522 |
| 210 | + -0500 [trace]: #0 fluent/log.rb:281:trace: accepted fluent socket addr="192.168.1.95" port=5522 |
| 211 | + -0500 debug.test: ["uint",0,1,128,255,65535,4294967295] |
| 212 | + -0500 [trace]: #0 fluent/log.rb:281:trace: connected fluent socket addr="192.168.1.95" port=5523 |
| 213 | + -0500 [trace]: #0 fluent/log.rb:281:trace: accepted fluent socket addr="192.168.1.95" port=5523 |
| 214 | + -0500 [trace]: #0 fluent/log.rb:281:trace: enqueueing all chunks in buffer instance=70248976563020 |
| 215 | + -0500 debug.test: {"string":"Hi!","float":0.3333333432674408,"double":0.3333333333333333} |
| 216 | + -0500 [trace]: #0 fluent/log.rb:281:trace: connected fluent socket addr="192.168.1.95" port=5524 |
| 217 | + -0500 [trace]: #0 fluent/log.rb:281:trace: accepted fluent socket addr="192.168.1.95" port=5524 |
| 218 | + -0500 debug.test: {"string":"Hi!","float":0.3333333432674408,"double":0.3333333333333333} |
| 219 | + -0500 [trace]: #0 fluent/log.rb:281:trace: connected fluent socket addr="192.168.1.95" port=5525 |
| 220 | + -0500 [trace]: #0 fluent/log.rb:281:trace: accepted fluent socket addr="192.168.1.95" port=5525 |
| 221 | + -0500 [trace]: #0 fluent/log.rb:281:trace: adding metadata instance=70248976563020 metadata=#<struct Fluent::Plugin::Buffer::Metadata timekey=nil, tag="td.fluentd_database.test", variables=nil> |
| 222 | + -0500 [trace]: #0 fluent/log.rb:281:trace: writing events into buffer instance=70248976563020 metadata_size=1 |
| 223 | + -0500 [debug]: #0 fluent/log.rb:302:debug: Created new chunk chunk_id="585c249fd2ebe20867267de2fde7c4bc" metadata=#<struct Fluent::Plugin::Buffer::Metadata timekey=nil, tag="td.fluentd_database.test", variables=nil> |
| 224 | + -0500 [trace]: #0 fluent/log.rb:281:trace: connected fluent socket addr="192.168.1.95" port=5526 |
| 225 | + -0500 [trace]: #0 fluent/log.rb:281:trace: accepted fluent socket addr="192.168.1.95" port=5526 |
| 226 | + -0500 debug.test: {"string":"Hi!","float":0.3333333432674408,"double":0.3333333333333333} |
| 227 | +
|
| 228 | +``` |
| 229 | + |
| 230 | +### Setting Treasure Data databases and tables |
| 231 | + |
| 232 | +The second field in the tag of your embedded code determines the database. For example, sending data to a tag called `td.mydatabase.mytable` logs the data to the database called `mydatabase` in the table `mytable`. You can modify the example configuration file to see this. |
| 233 | + |
| 234 | +### Debugging |
| 235 | + |
| 236 | +For more verbose debug messages, turn on the following flags in `mbed_app.json`: |
| 237 | + |
| 238 | +```json |
| 239 | +{ |
| 240 | + "macros": [ |
| 241 | + "MBEDTLS_SSL_DEBUG_ALL = 1" |
| 242 | + ], |
| 243 | + "target_overrides": { |
| 244 | + "*": { |
| 245 | + "mbed-trace.enable": true |
| 246 | + } |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +``` |
0 commit comments