fluent-plugin-mqtt-io 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fc0cec014b6bf816dd2d0b1772b88a1a070eacd
4
- data.tar.gz: ea134724065a4c5bb99f7e3ee8fdaab0ca7a05b9
3
+ metadata.gz: 910f2e9e505f0b9ae26ac26f29357a849b2e11df
4
+ data.tar.gz: ede83bec8dac5beda12524e0ba2d939460c664da
5
5
  SHA512:
6
- metadata.gz: e33d620814d9c7f5284163f8f22985fc583c2be740cc1f2dad6693540f72396b51c46fe7675232ce7a89418e47bdfaafb3ff0c5e39d6c8ab8394af1c52a19bda
7
- data.tar.gz: e7c78a5f2b9b8da3c9a46345b0ae1ae40244e6911f1c89b9b0f1278f09eb833290be60c013d49f03b03820560b96da552b1fceebd57ffbe80f8b39710dc7f3a3
6
+ metadata.gz: 8f40b5f402574cf81877f0bfd70a9d9331f724e0b3b667b9404f9d71e4cf732c3321200b219f1922e9cae835543f2a8c2cec55c3e2b33018f376e5277a09773d
7
+ data.tar.gz: 81e7227607e0ed5d95fc33b4e4015a5c1e323813a6c8604259cbcf578a74b7c7f89879d872e0e39d86062031c8c67540edcef84be8a2d4808f4f99e2d0255f24
@@ -1,3 +1,8 @@
1
+ ## 0.4.0 (2018-01-05)
2
+ Features:
3
+ - change fluentd dependency from ~> 0.14 to >= 0.14
4
+
5
+
1
6
  ## 0.3.0 (2017-03-20)
2
7
 
3
8
  Features:
@@ -0,0 +1,332 @@
1
+ # Fluent::Plugin::Mqtt::IO
2
+
3
+ Fluent plugin for MQTT Input/Output.
4
+ Mqtt::IO Plugin is deeply inspired by Fluent::Plugin::Mqtt.
5
+
6
+ https://github.com/yuuna/fluent-plugin-mqtt
7
+
8
+ Mqtt::IO plugin focus on federating components, e.g. sensors, messaging platform and databases. Encryption/Decryption is not supported in this plugin but [fluent-plugin-jwt-filter](https://github.com/toyokazu/fluent-plugin-jwt-filter) can be used to encrypt/decrypt messages using JSON Web Token technology.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'fluent-plugin-mqtt-io'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install fluent-plugin-mqtt-io
23
+
24
+
25
+ ## Usage
26
+
27
+ fluent-plugin-mqtt-io provides Input and Output Plugins for MQTT.
28
+
29
+ ### Input Plugin (Fluet::MqttInput)
30
+
31
+ Input Plugin can be used via source directive in the configuration.
32
+
33
+ For fluent-plugin-mqtt-io <= 0.2.3
34
+
35
+ ```
36
+
37
+ <source>
38
+ type mqtt
39
+ host 127.0.0.1
40
+ port 1883
41
+ format json
42
+ </source>
43
+
44
+ ```
45
+
46
+ For fluent-plugin-mqtt-io >= v0.3.0
47
+
48
+ ```
49
+
50
+ <source>
51
+ @type mqtt
52
+ host 127.0.0.1
53
+ port 1883
54
+ <parse>
55
+ @type json
56
+ </parse>
57
+ </source>
58
+
59
+ ```
60
+
61
+
62
+ The default MQTT topic is "#". Configurable options are the following:
63
+
64
+ - **host**: IP address of MQTT broker
65
+ - **port**: Port number of MQTT broker
66
+ - **client_id**: Client ID that to connect to MQTT broker
67
+ - **format** (mandatory): Input parser can be chosen, e.g. json, xml
68
+ - In order to use xml format, you need to install [fluent-plugin-xml-parser](https://github.com/toyokazu/fluent-plugin-xml-parser).
69
+ - Default time_key field for json format is 'time'
70
+ - **topic**: Topic name to be subscribed
71
+ - **bulk_trans**: Enable bulk transfer to support buffered output (mqtt_buf, Fluent::MqttBufferedOutput, defaut: true) only for fluent-plugin-mqtt-io <= 0.2.3
72
+ - **bulk_trans_sep**: A message separator for bulk transfer. The default separator is "\t". only for fluent-plugin-mqtt-io <= 0.2.3
73
+ - **username**: User name for authentication
74
+ - **password**: Password for authentication
75
+ - **keep_alive**: An interval of sending keep alive packet (default 15 sec)
76
+ - **ssl**: set true if you want to use SSL/TLS. If set to true, the following parameter must be provided
77
+ - **ca_file**: CA certificate file path
78
+ - **key_file**: private key file path
79
+ - **cert_file**: certificate file path
80
+ - **recv_time**: Add receive time to message in millisecond (ms) as integer for debug and performance/delay analysis. only for fluent-plugin-mqtt-io <= 0.2.3
81
+ - **recv_time_key**: An attribute of recv_time. only for fluent-plugin-mqtt-io <= 0.2.3
82
+ - **monitor**: monitor section. only for fluent-plugin-mqtt-io >= 0.3.0
83
+ - **recv_time**: Add receive time to message in millisecond (ms) as integer for debug and performance/delay analysis (default: false). only for fluent-plugin-mqtt-io >= 0.3.0
84
+ - **recv_time_key**: An attribute of recv_time (default: "recv_time"). only for fluent-plugin-mqtt-io >= 0.3.0
85
+ - **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io >= 0.3.0
86
+ - **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io >= 0.3.0
87
+ - **initial_interval**: An initial value of retry interval (s) (default 1)
88
+ - **retry_inc_ratio**: An increase ratio of retry interval per connection failure (default 2 (double)). It may be better to set the value to 1 in a mobile environment for eager reconnection.
89
+ - **max_retry_interval**: Maximum value of retry interval (default 300) only for fluent-plugin-mqtt-io >= 0.3.0
90
+
91
+ Input Plugin supports @label directive.
92
+
93
+ ### Output Plugin (Fluent::MqttOutput, Fluent::MqttBufferedOutput)
94
+
95
+ Output Plugin can be used via match directive.
96
+
97
+ For fluent-plugin-mqtt-io <= 0.2.3
98
+
99
+ ```
100
+
101
+ <match topic.**>
102
+ type mqtt
103
+ host 127.0.0.1
104
+ port 1883
105
+ </match>
106
+
107
+ ```
108
+
109
+ For fluent-plugin-mqtt-io >= v0.3.0
110
+
111
+ ```
112
+
113
+ <match topic.**>
114
+ @type mqtt
115
+ host 127.0.0.1
116
+ port 1883
117
+ <format>
118
+ @type json
119
+ add_newline false
120
+ </format>
121
+ </match>
122
+
123
+ ```
124
+
125
+ The options are basically the same as Input Plugin except for "format" and "bulk_trans" (only for Input). Additional options for Output Plugin are the following.
126
+
127
+ - **time_key**: An attribute name used for timestamp field genarated from fluentd time field. Default is nil (omitted).
128
+ If this option is omitted, timestamp field is not appended to the output record.
129
+ - **time_format**: Output format of timestamp field. Default is ISO8601. You can specify your own format by using TimeParser.
130
+ - **topic_rewrite_pattern**: Regexp pattern to extract replacement words from received topic or tag name
131
+ - **topic_rewrite_replacement**: Topic name used for the publish using extracted pattern
132
+ - **send_time**: Add send time to message in millisecond (ms) as integer for debug and performance/delay analysis. only for fluent-plugin-mqtt-io <= 0.2.3
133
+ - **send_time_key**: An attribute of send_time. only for fluent-plugin-mqtt-io <= 0.2.3
134
+ - **monitor**: monitor section. only for fluent-plugin-mqtt-io >= 0.3.0
135
+ - **send_time**: Add send time to message in millisecond (ms) as integer for debug and performance/delay analysis. only for fluent-plugin-mqtt-io >= 0.3.0
136
+ - **send_time_key**: An attribute of send_time. only for fluent-plugin-mqtt-io >= 0.3.0
137
+ - **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io >= 0.3.0
138
+ - **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io >= 0.3.0
139
+
140
+ If you use different source, e.g. the other MQTT broker, log file and so on, there is no need to specifie topic rewriting. Skip the following descriptions.
141
+
142
+ The topic name or tag name, e.g. "topic", received from an event can not be published without modification because if MQTT input plugin connecting to the identical MQTT broker is used as a source, the same message will become an input repeatedly. In order to support data conversion in single MQTT domain, simple topic rewriting should be supported. Since topic is rewritten using #gsub method, 'pattern' and 'replacement' are the same as #gsub arguments.
143
+
144
+ For fluent-plugin-mqtt-io <= v0.2.3
145
+
146
+ ```
147
+
148
+ <match topic.**>
149
+ type mqtt
150
+ host 127.0.0.1
151
+ port 1883
152
+ topic_rewrite_pattern '^([\w\/]+)$'
153
+ topic_rewrite_replacement '\1/rewritten'
154
+ </match>
155
+
156
+ ```
157
+
158
+ For fluent-plugin-mqtt-io >= v0.3.0
159
+
160
+ ```
161
+
162
+ <match topic.**>
163
+ @type mqtt
164
+ host 127.0.0.1
165
+ port 1883
166
+ <format>
167
+ @type json
168
+ add_newline false
169
+ </format>
170
+ topic_rewrite_pattern '^([\w\/]+)$'
171
+ topic_rewrite_replacement '\1/rewritten'
172
+ </match>
173
+
174
+ ```
175
+
176
+ You can also use mqtt_buf type which is implemented as Fluent::MqttBufferedOutput.
177
+
178
+ ```
179
+
180
+ <match topic.**>
181
+ type mqtt_buf
182
+ host 127.0.0.1
183
+ port 1883
184
+ topic_rewrite_pattern '^([\w\/]+)$'
185
+ topic_rewrite_replacement '\1/rewritten'
186
+ # You can specify Buffer Plugin options
187
+ buffer_type memory
188
+ flush_interval 1s
189
+ </match>
190
+
191
+ ```
192
+
193
+ For fluent-plugin-mqtt-io >= v0.3.0
194
+
195
+ ```
196
+
197
+ <match topic.**>
198
+ @type mqtt
199
+ host 127.0.0.1
200
+ port 1883
201
+ <format>
202
+ @type json
203
+ </format>
204
+ topic_rewrite_pattern '^([\w\/]+)$'
205
+ topic_rewrite_replacement '\1/rewritten'
206
+ # You can specify Buffer Plugin options
207
+ <buffer>
208
+ buffer_type memory
209
+ flush_interval 1s
210
+ </buffer>
211
+ </match>
212
+
213
+ ```
214
+
215
+ When a plugin implemented as Fluent::BufferedOutput, fluentd stores the received messages into buffers (Fluent::MemoryBuffer is used as default) separated by tag names. Writer Threads emit those stored messages periodically in the specified interval. In MqttBufferedOutput, the stored messages are concatenated with 'bulk_trans_sep' (default: "\t"). This function reduces the number of messages sent via MQTT if data producing interval of sensors are smaller than publish interval (flush_interval). The concatinated messages can be properly handled by Fluent::MqttInput plugin by specifying 'bulk_trans' option.
216
+
217
+ ## Use case examples
218
+
219
+ ### Sensor data collection (not for Libelium now)
220
+
221
+ *additional description (2015-12-25)*: Thanks to the updates by Libelium, Meshlium farmware now supports flexible output format configuration including JSON. As a result, data conversion by XmlParser is not required for this use case. The following description is kept just for sharing know-how of data conversion in fluentd.
222
+
223
+ There are many kinds of commercial sensor products on the market, e.g. [Libelium](http://www.libelium.com/). Major sensor products support MQTT to upload sensor data. The following example shows how to store uploaded Libelium sensor data into ElasticSearch.
224
+
225
+ ![Libelium sensor data collection](https://github.com/toyokazu/fluent-plugin-mqtt-io/blob/master/images/libelium_sensor_data_collection.png "Libelium sensor data")
226
+
227
+ As described in the figure, fluent-plugin-mqtt-io, fluent-plugin-xml-parser and fluent-plugin-elasticsearch are used. The following is an example configuration.
228
+
229
+ ```
230
+ <source>
231
+ type mqtt
232
+ host 192.168.1.100
233
+ port 1883
234
+ topic 'Libelium/+/+'
235
+ format xml
236
+ time_xpath '["cap:alert/cap:info/cap:onset", "text"]'
237
+ time_key '@timestamp'
238
+ attr_xpaths '[["cap:alert/cap:info/cap:parameter/cap:valueName", "text"]]'
239
+ value_xpaths '[["cap:alert/cap:info/cap:parameter/cap:value", "text"]]'
240
+ @label @MQTT_OUT
241
+ </source>
242
+
243
+ <label @MQTT_OUT>
244
+ <match **>
245
+ <store>
246
+ type elasticsearch
247
+ host localhost
248
+ port 9200
249
+ index_name libelium
250
+ type_name smartcity
251
+ include_tag_key true
252
+ tag_key sensor_id
253
+ logstash_format false
254
+ </store>
255
+ </match>
256
+ </label>
257
+
258
+ ```
259
+
260
+ The following mapping is assumed to be created at ElasticSearch.
261
+
262
+ ```
263
+
264
+ curl -XPUT 'http://localhost:9200/libelium/_mapping/smartcity' -d '
265
+ {"smartcity":
266
+ {"properties":
267
+ {
268
+ "sensor_id":{"type": "string"},
269
+ "DUST":{"type": "float"},
270
+ "MCP":{"type": "float"},
271
+ "HUMA":{"type": "float"},
272
+ "TCA":{"type": "float"},
273
+ "BAT":{"type": "float"},
274
+ "@timestamp":{"type":"date","format":"dateOptionalTime"}
275
+ }
276
+ }
277
+ }'
278
+
279
+ ```
280
+
281
+ ### MQTT message conversion
282
+
283
+ Sometimes, MQTT message conversion must be done in the network because the processing entities does not have the conversion function. In that case, the configuration similar to the above example can be used. The difference resides output configuration. In this example, since the same MQTT broker is used to upload converted data, topic rewriting function is used for separating messages before and after conversion.
284
+
285
+ ![MQTT message conversion](https://github.com/toyokazu/fluent-plugin-mqtt-io/blob/master/images/mqtt_message_conversion.png "MQTT message conversion")
286
+
287
+ ```
288
+ <source>
289
+ type mqtt
290
+ host 192.168.1.100
291
+ port 1883
292
+ topic 'Libelium/+/+'
293
+ format xml
294
+ time_xpath '["cap:alert/cap:info/cap:onset", "text"]'
295
+ time_key '@timestamp'
296
+ attr_xpaths '[["cap:alert/cap:info/cap:parameter/cap:valueName", "text"]]'
297
+ value_xpaths '[["cap:alert/cap:info/cap:parameter/cap:value", "text"]]'
298
+ @label @MQTT_OUT
299
+ </source>
300
+
301
+ <label @MQTT_OUT>
302
+ <match **>
303
+ type mqtt
304
+ host 192.168.1.100
305
+ port 1883
306
+ topic_rewrite_pattern '^([\w\/]+)$'
307
+ topic_rewrite_replacement '\1/rewritten'
308
+ </match>
309
+ </label>
310
+
311
+ ```
312
+
313
+
314
+ ### Sensor data uploads from tiny computers, e.g. Raspberry Pi, Edison, etc
315
+
316
+ MQTT output plugin can be used as the following. If you have tiny computers like Raspberry Pi equipped with sensors and their data are outputted as files, you can use fluent-plugin-mqtt-io for uploading those data.
317
+
318
+ ![Sensor data uploads from tiny computers](https://github.com/toyokazu/fluent-plugin-mqtt-io/blob/master/images/sensor_data_uploads_from_tiny_computers.png "Sensor data uploads from tiny computers")
319
+
320
+
321
+ ## Contributing
322
+
323
+ 1. Fork it ( http://github.com/toyokazu/fluent-plugin-mqtt-io/fork )
324
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
325
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
326
+ 4. Push to the branch (`git push origin my-new-feature`)
327
+ 5. Create new Pull Request
328
+
329
+
330
+ ## License
331
+
332
+ The gem is available as open source under the terms of the [Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).
data/README.md CHANGED
@@ -30,23 +30,7 @@ fluent-plugin-mqtt-io provides Input and Output Plugins for MQTT.
30
30
 
31
31
  Input Plugin can be used via source directive in the configuration.
32
32
 
33
- For fluent-plugin-mqtt-io <= 0.2.3
34
-
35
- ```
36
-
37
- <source>
38
- type mqtt
39
- host 127.0.0.1
40
- port 1883
41
- format json
42
- </source>
43
-
44
- ```
45
-
46
- For fluent-plugin-mqtt-io ~> v0.3.0
47
-
48
33
  ```
49
-
50
34
  <source>
51
35
  @type mqtt
52
36
  host 127.0.0.1
@@ -58,35 +42,46 @@ For fluent-plugin-mqtt-io ~> v0.3.0
58
42
 
59
43
  ```
60
44
 
45
+ When using security options, specify them in security section; for example:
46
+
47
+ ```
48
+ <match>
49
+ @type mqtt
50
+ host 'your_host'
51
+ port 'your_port'
52
+ <security>
53
+ username 'your_username'
54
+ password 'your_password'
55
+ </security>
56
+ <format>
57
+ @type json
58
+ </format>
59
+ </match>
60
+ ```
61
61
 
62
62
  The default MQTT topic is "#". Configurable options are the following:
63
63
 
64
64
  - **host**: IP address of MQTT broker
65
65
  - **port**: Port number of MQTT broker
66
66
  - **client_id**: Client ID that to connect to MQTT broker
67
- - **format** (mandatory): Input parser can be chosen, e.g. json, xml
68
- - In order to use xml format, you need to install [fluent-plugin-xml-parser](https://github.com/toyokazu/fluent-plugin-xml-parser).
69
- - Default time_key field for json format is 'time'
67
+ - **parser**: Parser plugin can be specified ![Parser Plugin](https://docs.fluentd.org/v1.0/articles/parser-plugin-overview)
70
68
  - **topic**: Topic name to be subscribed
71
- - **bulk_trans**: Enable bulk transfer to support buffered output (mqtt_buf, Fluent::MqttBufferedOutput, defaut: true) only for fluent-plugin-mqtt-io <= 0.2.3
72
- - **bulk_trans_sep**: A message separator for bulk transfer. The default separator is "\t". only for fluent-plugin-mqtt-io <= 0.2.3
73
- - **username**: User name for authentication
74
- - **password**: Password for authentication
69
+ - **security**
70
+ - **username**: User name for authentication
71
+ - **password**: Password for authentication
72
+ - **use_tls**: set true if you want to use SSL/TLS. If set to true, the following parameter must be provided
73
+ - **ca_file**: CA certificate file path
74
+ - **key_file**: private key file path
75
+ - **cert_file**: certificate file path
75
76
  - **keep_alive**: An interval of sending keep alive packet (default 15 sec)
76
- - **ssl**: set true if you want to use SSL/TLS. If set to true, the following parameter must be provided
77
- - **ca_file**: CA certificate file path
78
- - **key_file**: private key file path
79
- - **cert_file**: certificate file path
80
- - **recv_time**: Add receive time to message in millisecond (ms) as integer for debug and performance/delay analysis. only for fluent-plugin-mqtt-io <= 0.2.3
81
- - **recv_time_key**: An attribute of recv_time. only for fluent-plugin-mqtt-io <= 0.2.3
82
- - **monitor**: monitor section. only for fluent-plugin-mqtt-io ~> 0.3.0
83
- - **recv_time**: Add receive time to message in millisecond (ms) as integer for debug and performance/delay analysis (default: false). only for fluent-plugin-mqtt-io ~> 0.3.0
84
- - **recv_time_key**: An attribute of recv_time (default: "recv_time"). only for fluent-plugin-mqtt-io ~> 0.3.0
85
- - **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io ~> 0.3.0
86
- - **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io ~> 0.3.0
77
+ - **monitor**: monitor section. only for fluent-plugin-mqtt-io
78
+ - **recv_time**: Add receive time to message in millisecond (ms) as integer for debug and performance/delay analysis (default: false). only for fluent-plugin-mqtt-io
79
+ - **recv_time_key**: An attribute of recv_time (default: "recv_time"). only for fluent-plugin-mqtt-io
80
+ - **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io
81
+ - **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io
87
82
  - **initial_interval**: An initial value of retry interval (s) (default 1)
88
83
  - **retry_inc_ratio**: An increase ratio of retry interval per connection failure (default 2 (double)). It may be better to set the value to 1 in a mobile environment for eager reconnection.
89
- - **max_retry_interval**: Maximum value of retry interval (default 300) only for fluent-plugin-mqtt-io ~> 0.3.0
84
+ - **max_retry_interval**: Maximum value of retry interval (default 300)
90
85
 
91
86
  Input Plugin supports @label directive.
92
87
 
@@ -94,22 +89,7 @@ Input Plugin supports @label directive.
94
89
 
95
90
  Output Plugin can be used via match directive.
96
91
 
97
- For fluent-plugin-mqtt-io <= 0.2.3
98
-
99
92
  ```
100
-
101
- <match topic.**>
102
- type mqtt
103
- host 127.0.0.1
104
- port 1883
105
- </match>
106
-
107
- ```
108
-
109
- For fluent-plugin-mqtt-io ~> v0.3.0
110
-
111
- ```
112
-
113
93
  <match topic.**>
114
94
  @type mqtt
115
95
  host 127.0.0.1
@@ -119,46 +99,31 @@ For fluent-plugin-mqtt-io ~> v0.3.0
119
99
  add_newline false
120
100
  </format>
121
101
  </match>
122
-
123
102
  ```
124
103
 
125
- The options are basically the same as Input Plugin except for "format" and "bulk_trans" (only for Input). Additional options for Output Plugin are the following.
104
+ The options are basically the same as Input Plugin except for "parser (for Input)/format (for Output)". Additional options for Output Plugin are the following.
126
105
 
106
+ - **qos**: Quality of Service (QoS) for message publishing, 0 or 1 is valid. 2 is not supported by mqtt client. Default is 1.
107
+ - **retain**: If set true the broker will keep the message even after sending it to all current subscribers. Default is false
127
108
  - **time_key**: An attribute name used for timestamp field genarated from fluentd time field. Default is nil (omitted).
128
109
  If this option is omitted, timestamp field is not appended to the output record.
129
- - **time_format**: Output format of timestamp field. Default is ISO8601. You can specify your own format by using TimeParser.
130
110
  - **topic_rewrite_pattern**: Regexp pattern to extract replacement words from received topic or tag name
131
111
  - **topic_rewrite_replacement**: Topic name used for the publish using extracted pattern
132
- - **send_time**: Add send time to message in millisecond (ms) as integer for debug and performance/delay analysis. only for fluent-plugin-mqtt-io <= 0.2.3
133
- - **send_time_key**: An attribute of send_time. only for fluent-plugin-mqtt-io <= 0.2.3
134
- - **monitor**: monitor section. only for fluent-plugin-mqtt-io ~> 0.3.0
135
- - **send_time**: Add send time to message in millisecond (ms) as integer for debug and performance/delay analysis. only for fluent-plugin-mqtt-io ~> 0.3.0
136
- - **send_time_key**: An attribute of send_time. only for fluent-plugin-mqtt-io ~> 0.3.0
137
- - **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io ~> 0.3.0
138
- - **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io ~> 0.3.0
112
+ - **format**: Formatter plugin can be specified. ![Formatter Plugin](https://docs.fluentd.org/v1.0/articles/formatter-plugin-overview)
113
+ - **monitor**: monitor section. only for fluent-plugin-mqtt-io
114
+ - **send_time**: Add send time to message in millisecond (ms) as integer for debug and performance/delay analysis. only for fluent-plugin-mqtt-io
115
+ - **send_time_key**: An attribute of send_time. only for fluent-plugin-mqtt-io
116
+ - **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io
117
+ - **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io
118
+ - **buffer**: synchronous/asynchronous buffer is supported. Refer ![Buffer section configurations](https://docs.fluentd.org/v1.0/articles/buffer-section) for detailed configuration.
119
+ - **async**: Enable asynchronous buffer transfer. Default is false.
139
120
 
140
121
  If you use different source, e.g. the other MQTT broker, log file and so on, there is no need to specifie topic rewriting. Skip the following descriptions.
141
122
 
142
123
  The topic name or tag name, e.g. "topic", received from an event can not be published without modification because if MQTT input plugin connecting to the identical MQTT broker is used as a source, the same message will become an input repeatedly. In order to support data conversion in single MQTT domain, simple topic rewriting should be supported. Since topic is rewritten using #gsub method, 'pattern' and 'replacement' are the same as #gsub arguments.
143
124
 
144
- For fluent-plugin-mqtt-io <= v0.2.3
145
125
 
146
126
  ```
147
-
148
- <match topic.**>
149
- type mqtt
150
- host 127.0.0.1
151
- port 1883
152
- topic_rewrite_pattern '^([\w\/]+)$'
153
- topic_rewrite_replacement '\1/rewritten'
154
- </match>
155
-
156
- ```
157
-
158
- For fluent-plugin-mqtt-io ~> v0.3.0
159
-
160
- ```
161
-
162
127
  <match topic.**>
163
128
  @type mqtt
164
129
  host 127.0.0.1
@@ -170,30 +135,10 @@ For fluent-plugin-mqtt-io ~> v0.3.0
170
135
  topic_rewrite_pattern '^([\w\/]+)$'
171
136
  topic_rewrite_replacement '\1/rewritten'
172
137
  </match>
173
-
174
- ```
175
-
176
- You can also use mqtt_buf type which is implemented as Fluent::MqttBufferedOutput.
177
-
178
- ```
179
-
180
- <match topic.**>
181
- type mqtt_buf
182
- host 127.0.0.1
183
- port 1883
184
- topic_rewrite_pattern '^([\w\/]+)$'
185
- topic_rewrite_replacement '\1/rewritten'
186
- # You can specify Buffer Plugin options
187
- buffer_type memory
188
- flush_interval 1s
189
- </match>
190
-
191
138
  ```
192
139
 
193
- For fluent-plugin-mqtt-io ~> v0.3.0
194
140
 
195
141
  ```
196
-
197
142
  <match topic.**>
198
143
  @type mqtt
199
144
  host 127.0.0.1
@@ -209,113 +154,7 @@ For fluent-plugin-mqtt-io ~> v0.3.0
209
154
  flush_interval 1s
210
155
  </buffer>
211
156
  </match>
212
-
213
- ```
214
-
215
- When a plugin implemented as Fluent::BufferedOutput, fluentd stores the received messages into buffers (Fluent::MemoryBuffer is used as default) separated by tag names. Writer Threads emit those stored messages periodically in the specified interval. In MqttBufferedOutput, the stored messages are concatenated with 'bulk_trans_sep' (default: "\t"). This function reduces the number of messages sent via MQTT if data producing interval of sensors are smaller than publish interval (flush_interval). The concatinated messages can be properly handled by Fluent::MqttInput plugin by specifying 'bulk_trans' option.
216
-
217
- ## Use case examples
218
-
219
- ### Sensor data collection (not for Libelium now)
220
-
221
- *additional description (2015-12-25)*: Thanks to the updates by Libelium, Meshlium farmware now supports flexible output format configuration including JSON. As a result, data conversion by XmlParser is not required for this use case. The following description is kept just for sharing know-how of data conversion in fluentd.
222
-
223
- There are many kinds of commercial sensor products on the market, e.g. [Libelium](http://www.libelium.com/). Major sensor products support MQTT to upload sensor data. The following example shows how to store uploaded Libelium sensor data into ElasticSearch.
224
-
225
- ![Libelium sensor data collection](https://github.com/toyokazu/fluent-plugin-mqtt-io/blob/master/images/libelium_sensor_data_collection.png "Libelium sensor data")
226
-
227
- As described in the figure, fluent-plugin-mqtt-io, fluent-plugin-xml-parser and fluent-plugin-elasticsearch are used. The following is an example configuration.
228
-
229
157
  ```
230
- <source>
231
- type mqtt
232
- host 192.168.1.100
233
- port 1883
234
- topic 'Libelium/+/+'
235
- format xml
236
- time_xpath '["cap:alert/cap:info/cap:onset", "text"]'
237
- time_key '@timestamp'
238
- attr_xpaths '[["cap:alert/cap:info/cap:parameter/cap:valueName", "text"]]'
239
- value_xpaths '[["cap:alert/cap:info/cap:parameter/cap:value", "text"]]'
240
- @label @MQTT_OUT
241
- </source>
242
-
243
- <label @MQTT_OUT>
244
- <match **>
245
- <store>
246
- type elasticsearch
247
- host localhost
248
- port 9200
249
- index_name libelium
250
- type_name smartcity
251
- include_tag_key true
252
- tag_key sensor_id
253
- logstash_format false
254
- </store>
255
- </match>
256
- </label>
257
-
258
- ```
259
-
260
- The following mapping is assumed to be created at ElasticSearch.
261
-
262
- ```
263
-
264
- curl -XPUT 'http://localhost:9200/libelium/_mapping/smartcity' -d '
265
- {"smartcity":
266
- {"properties":
267
- {
268
- "sensor_id":{"type": "string"},
269
- "DUST":{"type": "float"},
270
- "MCP":{"type": "float"},
271
- "HUMA":{"type": "float"},
272
- "TCA":{"type": "float"},
273
- "BAT":{"type": "float"},
274
- "@timestamp":{"type":"date","format":"dateOptionalTime"}
275
- }
276
- }
277
- }'
278
-
279
- ```
280
-
281
- ### MQTT message conversion
282
-
283
- Sometimes, MQTT message conversion must be done in the network because the processing entities does not have the conversion function. In that case, the configuration similar to the above example can be used. The difference resides output configuration. In this example, since the same MQTT broker is used to upload converted data, topic rewriting function is used for separating messages before and after conversion.
284
-
285
- ![MQTT message conversion](https://github.com/toyokazu/fluent-plugin-mqtt-io/blob/master/images/mqtt_message_conversion.png "MQTT message conversion")
286
-
287
- ```
288
- <source>
289
- type mqtt
290
- host 192.168.1.100
291
- port 1883
292
- topic 'Libelium/+/+'
293
- format xml
294
- time_xpath '["cap:alert/cap:info/cap:onset", "text"]'
295
- time_key '@timestamp'
296
- attr_xpaths '[["cap:alert/cap:info/cap:parameter/cap:valueName", "text"]]'
297
- value_xpaths '[["cap:alert/cap:info/cap:parameter/cap:value", "text"]]'
298
- @label @MQTT_OUT
299
- </source>
300
-
301
- <label @MQTT_OUT>
302
- <match **>
303
- type mqtt
304
- host 192.168.1.100
305
- port 1883
306
- topic_rewrite_pattern '^([\w\/]+)$'
307
- topic_rewrite_replacement '\1/rewritten'
308
- </match>
309
- </label>
310
-
311
- ```
312
-
313
-
314
- ### Sensor data uploads from tiny computers, e.g. Raspberry Pi, Edison, etc
315
-
316
- MQTT output plugin can be used as the following. If you have tiny computers like Raspberry Pi equipped with sensors and their data are outputted as files, you can use fluent-plugin-mqtt-io for uploading those data.
317
-
318
- ![Sensor data uploads from tiny computers](https://github.com/toyokazu/fluent-plugin-mqtt-io/blob/master/images/sensor_data_uploads_from_tiny_computers.png "Sensor data uploads from tiny computers")
319
158
 
320
159
 
321
160
  ## Contributing
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-mqtt-io"
7
- spec.version = "0.4.0"
7
+ spec.version = "0.4.1"
8
8
  spec.authors = ["Toyokazu Akiyama"]
9
9
  spec.email = ["toyokazu@gmail.com"]
10
10
 
@@ -66,13 +66,20 @@ module Fluent::Plugin
66
66
  :in_mqtt
67
67
  end
68
68
 
69
+ def kill_thread
70
+ @get_thread.kill if !@get_thread.nil?
71
+ end
72
+
69
73
  def after_disconnection
74
+ kill_thread
75
+ super
70
76
  end
71
77
 
72
78
  def after_connection
73
79
  if @client.connected?
74
80
  @client.subscribe(@topic)
75
- @get_thread = thread_create(:in_mqtt_get) do
81
+ #@get_thread = thread_create(:in_mqtt_get) do
82
+ @get_thread = Thread.new do
76
83
  @client.get do |topic, message|
77
84
  emit(topic, message)
78
85
  end
@@ -92,13 +92,18 @@ module Fluent::Plugin
92
92
  @client.disconnect if @client.connected?
93
93
  end
94
94
 
95
+ def kill_connect_thread
96
+ @connect_thread.kill if !@connect_thread.nil?
97
+ end
98
+
95
99
  def after_disconnection
96
100
  # should be implemented
101
+ kill_connect_thread
97
102
  end
98
103
 
99
- def rescue_disconnection(*block)
104
+ def rescue_disconnection
100
105
  begin
101
- yield *block
106
+ yield
102
107
  rescue MQTT::ProtocolException => e
103
108
  # TODO:
104
109
  # Currently MQTT::ProtocolException cannot be caught during @client.get
@@ -110,6 +115,12 @@ module Fluent::Plugin
110
115
  retry_connect(e, "System call error occurs.")
111
116
  rescue StandardError=> e
112
117
  retry_connect(e, "The other error occurs.")
118
+ rescue MQTT::NotConnectedException=> e
119
+ # Since MQTT::NotConnectedException is raised only on publish,
120
+ # connection error should be catched before this error.
121
+ # So, reconnection process is omitted for this Exception
122
+ # to prevent waistful increment of retry interval.
123
+ log.error "MQTT not connected exception occurs.,#{e.class},#{e.message}"
113
124
  end
114
125
  end
115
126
 
@@ -120,7 +131,8 @@ module Fluent::Plugin
120
131
  end
121
132
 
122
133
  def connect
123
- thread_create("#{current_plugin_name}_monitor".to_sym) do
134
+ #@connect_thread = thread_create("#{current_plugin_name}_monitor".to_sym) do
135
+ @connect_thread = Thread.new do
124
136
  rescue_disconnection do
125
137
  @client.connect
126
138
  log.debug "connected to mqtt broker #{@host}:#{@port} for #{current_plugin_name}"
@@ -18,6 +18,11 @@ module Fluent::Plugin
18
18
  desc 'Topic rewrite replacement string.'
19
19
  config_param :topic_rewrite_replacement, :string, default: nil
20
20
 
21
+ desc 'Retain option which publishing'
22
+ config_param :retain, :bool, default: false
23
+ desc 'QoS option which publishing'
24
+ config_param :qos, :integer, default: 1
25
+
21
26
  config_section :format do
22
27
  desc 'The format to publish'
23
28
  config_param :@type, :string, default: 'single_value'
@@ -36,6 +41,11 @@ module Fluent::Plugin
36
41
  config_param :time_format, :string, default: nil
37
42
  end
38
43
 
44
+ config_section :buffer, required: false, multi: false do
45
+ desc 'Prefer asynchronous buffering'
46
+ config_param :async, :bool, default: false
47
+ end
48
+
39
49
  # This method is called before starting.
40
50
  # 'conf' is a Hash that includes configuration parameters.
41
51
  # If the configuration is invalid, raise Fluent::ConfigError.
@@ -64,6 +74,10 @@ module Fluent::Plugin
64
74
  @has_buffer_section
65
75
  end
66
76
 
77
+ def prefer_delayed_commit
78
+ @has_buffer_section && @buffer_config.async
79
+ end
80
+
67
81
  # This method is called when starting.
68
82
  # Open sockets or files here.
69
83
  def start
@@ -75,16 +89,27 @@ module Fluent::Plugin
75
89
  # Shutdown the thread and close sockets or files here.
76
90
  def shutdown
77
91
  shutdown_proxy
92
+ kill_thread
78
93
  super
79
94
  end
80
95
 
96
+ def kill_thread
97
+ @dummy_thread.kill if !@dummy_thread.nil?
98
+ end
99
+
81
100
  def after_connection
82
- @dummy_thread = thread_create(:out_mqtt_dummy) do
101
+ #@dummy_thread = thread_create(:out_mqtt_dummy) do
102
+ @dummy_thread = Thread.new do
83
103
  Thread.stop
84
104
  end
85
105
  @dummy_thread
86
106
  end
87
107
 
108
+ def after_disconnection
109
+ kill_thread
110
+ super
111
+ end
112
+
88
113
  def current_plugin_name
89
114
  :out_mqtt
90
115
  end
@@ -99,23 +124,11 @@ module Fluent::Plugin
99
124
  end
100
125
 
101
126
  def publish_event_stream(tag, es)
102
- if es.class == Fluent::OneEventStream
103
- es = inject_values_to_event_stream(tag, es)
104
- es.each do |time, record|
105
- log.debug "MqttOutput#publish_event_stream: #{rewrite_tag(tag)}, #{time}, #{add_send_time(record)}"
106
- rescue_disconnection do
107
- @client.publish(rewrite_tag(tag), @formatter.format(tag, time, add_send_time(record)))
108
- end
109
- end
110
- else
111
- es = inject_values_to_event_stream(tag, es)
112
- array = []
113
- es.each do |time, record|
114
- log.debug "MqttOutput#publish_event_stream: #{rewrite_tag(tag)}, #{time}, #{add_send_time(record)}"
115
- array << add_send_time(record)
116
- end
127
+ log.debug "publish_event_stream: #{es.class}"
128
+ es = inject_values_to_event_stream(tag, es)
129
+ es.each do |time, record|
117
130
  rescue_disconnection do
118
- @client.publish(rewrite_tag(tag), @formatter.format(tag, Fluent::EventTime.now, array))
131
+ publish(tag, time, record)
119
132
  end
120
133
  end
121
134
  log.flush
@@ -134,13 +147,32 @@ module Fluent::Plugin
134
147
  true
135
148
  end
136
149
 
150
+ def publish(tag, time, record)
151
+ log.debug "MqttOutput::#{caller_locations(1,1)[0].label}: #{rewrite_tag(rewrite_tag(tag))}, #{time}, #{add_send_time(record)}"
152
+ @client.publish(
153
+ rewrite_tag(tag),
154
+ @formatter.format(tag, time, add_send_time(record)),
155
+ @retain,
156
+ @qos
157
+ )
158
+ end
159
+
137
160
  def write(chunk)
138
161
  return if chunk.empty?
139
162
  chunk.each do |tag, time, record|
140
163
  rescue_disconnection do
141
- log.debug "MqttOutput#write: #{rewrite_tag(rewrite_tag(tag))}, #{time}, #{add_send_time(record)}"
142
- @client.publish(rewrite_tag(tag), @formatter.format(tag, time, add_send_time(record)))
164
+ publish(tag, time, record)
165
+ end
166
+ end
167
+ end
168
+
169
+ def try_write(chunk)
170
+ return if chunk.empty?
171
+ rescue_disconnection do
172
+ chunk.each do |tag, time, record|
173
+ publish(tag, time, record)
143
174
  end
175
+ commit_write(chunk.unique_id)
144
176
  end
145
177
  end
146
178
  end
@@ -21,10 +21,9 @@ class MqttInputTest < Test::Unit::TestCase
21
21
  host 127.0.0.1
22
22
  port 1300
23
23
  client_id aa-bb-cc-dd
24
- <parse>
25
- @type json
26
- time_format %FT%T%:z
27
- </parse>
24
+ <monitor>
25
+ recv_time true
26
+ </monitor>
28
27
  <security>
29
28
  use_tls true
30
29
  <tls>
@@ -38,6 +37,10 @@ class MqttInputTest < Test::Unit::TestCase
38
37
  assert_equal '127.0.0.1', d.instance.host
39
38
  assert_equal 1300, d.instance.port
40
39
  assert_equal 'aa-bb-cc-dd', d.instance.client_id
40
+
41
+ assert_equal 'none', d.instance.parser_configs.first[:@type]
42
+
43
+ assert_equal true, d.instance.monitor.recv_time
41
44
 
42
45
  assert_equal true, d.instance.security.use_tls
43
46
  assert_equal '/cert/cacert.pem', d.instance.security.tls.ca_file
@@ -21,9 +21,11 @@ class MqttOutputTest < Test::Unit::TestCase
21
21
  host 127.0.0.1
22
22
  port 1300
23
23
  client_id aa-bb-cc-dd
24
- <format>
25
- @type json
26
- </format>
24
+ retain true
25
+ qos 2
26
+ <buffer>
27
+ async true
28
+ </buffer>
27
29
  <monitor>
28
30
  send_time true
29
31
  </monitor>
@@ -38,8 +40,15 @@ class MqttOutputTest < Test::Unit::TestCase
38
40
  ]
39
41
  assert_equal '127.0.0.1', d.instance.host
40
42
  assert_equal 1300, d.instance.port
41
- assert_equal true, d.instance.monitor.send_time
42
43
  assert_equal 'aa-bb-cc-dd', d.instance.client_id
44
+ assert_equal true, d.instance.retain
45
+ assert_equal 2, d.instance.qos
46
+
47
+ assert_equal true, d.instance.buffer_config.async
48
+
49
+ assert_equal true, d.instance.monitor.send_time
50
+
51
+ # cannot test formatter configuration (default: single_value)
43
52
 
44
53
  assert_equal true, d.instance.security.use_tls
45
54
  assert_equal '/cert/cacert.pem', d.instance.security.tls.ca_file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-mqtt-io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toyokazu Akiyama
8
8
  autorequire:
9
9
  bindir: []
10
10
  cert_chain: []
11
- date: 2018-01-05 00:00:00.000000000 Z
11
+ date: 2018-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -98,6 +98,7 @@ files:
98
98
  - CHANGELOG.md
99
99
  - Gemfile
100
100
  - LICENSE
101
+ - README-old.md
101
102
  - README.md
102
103
  - Rakefile
103
104
  - fluent-plugin-mqtt-io.gemspec