fluent-plugin-mqtt-io 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +5 -0
- data/README-old.md +333 -0
- data/README.md +44 -203
- data/fluent-plugin-mqtt-io.gemspec +3 -3
- data/lib/fluent/plugin/in_mqtt.rb +16 -6
- data/lib/fluent/plugin/mqtt_proxy.rb +69 -26
- data/lib/fluent/plugin/out_mqtt.rb +58 -18
- data/test/plugin/test_in_mqtt.rb +7 -4
- data/test/plugin/test_out_mqtt.rb +13 -4
- metadata +14 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ed837b9979b990f051238be99dac2c17ba2feb7b35c0f56910f706c3c426944a
|
4
|
+
data.tar.gz: 52628c53314f60215b6b97854103a3e99c0f313fe5a5031b1e6ed87e16cfc918
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2b38c5196fa566997d647fe63d3b2af44b323d218b5ec2c8e613cfa6662bd64401fbc99a68935950ddeb9b1b128f7571357e885276dfae5c6fbaaab62c088e9
|
7
|
+
data.tar.gz: 2d96a742d31d3b638f989999ea5c4919c95f61c64156d46d2dc01263842b036cf584bccc596c11634a3b95f358e84c112ce7b4c64b9eafc6fe48c367f0518381
|
data/CHANGELOG.md
CHANGED
data/README-old.md
ADDED
@@ -0,0 +1,333 @@
|
|
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
|
+
- **clean_session**: Setting for clean session client option (false persists session offline)
|
76
|
+
- **keep_alive**: An interval of sending keep alive packet (default 15 sec)
|
77
|
+
- **ssl**: set true if you want to use SSL/TLS. If set to true, the following parameter must be provided
|
78
|
+
- **ca_file**: CA certificate file path
|
79
|
+
- **key_file**: private key file path
|
80
|
+
- **cert_file**: certificate file path
|
81
|
+
- **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
|
82
|
+
- **recv_time_key**: An attribute of recv_time. only for fluent-plugin-mqtt-io <= 0.2.3
|
83
|
+
- **monitor**: monitor section. only for fluent-plugin-mqtt-io >= 0.3.0
|
84
|
+
- **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
|
85
|
+
- **recv_time_key**: An attribute of recv_time (default: "recv_time"). only for fluent-plugin-mqtt-io >= 0.3.0
|
86
|
+
- **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io >= 0.3.0
|
87
|
+
- **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io >= 0.3.0
|
88
|
+
- **initial_interval**: An initial value of retry interval (s) (default 1)
|
89
|
+
- **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.
|
90
|
+
- **max_retry_interval**: Maximum value of retry interval (default 300) only for fluent-plugin-mqtt-io >= 0.3.0
|
91
|
+
|
92
|
+
Input Plugin supports @label directive.
|
93
|
+
|
94
|
+
### Output Plugin (Fluent::MqttOutput, Fluent::MqttBufferedOutput)
|
95
|
+
|
96
|
+
Output Plugin can be used via match directive.
|
97
|
+
|
98
|
+
For fluent-plugin-mqtt-io <= 0.2.3
|
99
|
+
|
100
|
+
```
|
101
|
+
|
102
|
+
<match topic.**>
|
103
|
+
type mqtt
|
104
|
+
host 127.0.0.1
|
105
|
+
port 1883
|
106
|
+
</match>
|
107
|
+
|
108
|
+
```
|
109
|
+
|
110
|
+
For fluent-plugin-mqtt-io >= v0.3.0
|
111
|
+
|
112
|
+
```
|
113
|
+
|
114
|
+
<match topic.**>
|
115
|
+
@type mqtt
|
116
|
+
host 127.0.0.1
|
117
|
+
port 1883
|
118
|
+
<format>
|
119
|
+
@type json
|
120
|
+
add_newline false
|
121
|
+
</format>
|
122
|
+
</match>
|
123
|
+
|
124
|
+
```
|
125
|
+
|
126
|
+
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.
|
127
|
+
|
128
|
+
- **time_key**: An attribute name used for timestamp field genarated from fluentd time field. Default is nil (omitted).
|
129
|
+
If this option is omitted, timestamp field is not appended to the output record.
|
130
|
+
- **time_format**: Output format of timestamp field. Default is ISO8601. You can specify your own format by using TimeParser.
|
131
|
+
- **topic_rewrite_pattern**: Regexp pattern to extract replacement words from received topic or tag name
|
132
|
+
- **topic_rewrite_replacement**: Topic name used for the publish using extracted pattern
|
133
|
+
- **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
|
134
|
+
- **send_time_key**: An attribute of send_time. only for fluent-plugin-mqtt-io <= 0.2.3
|
135
|
+
- **monitor**: monitor section. only for fluent-plugin-mqtt-io >= 0.3.0
|
136
|
+
- **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
|
137
|
+
- **send_time_key**: An attribute of send_time. only for fluent-plugin-mqtt-io >= 0.3.0
|
138
|
+
- **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io >= 0.3.0
|
139
|
+
- **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io >= 0.3.0
|
140
|
+
|
141
|
+
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.
|
142
|
+
|
143
|
+
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.
|
144
|
+
|
145
|
+
For fluent-plugin-mqtt-io <= v0.2.3
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
<match topic.**>
|
150
|
+
type mqtt
|
151
|
+
host 127.0.0.1
|
152
|
+
port 1883
|
153
|
+
topic_rewrite_pattern '^([\w\/]+)$'
|
154
|
+
topic_rewrite_replacement '\1/rewritten'
|
155
|
+
</match>
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
For fluent-plugin-mqtt-io >= v0.3.0
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
<match topic.**>
|
164
|
+
@type mqtt
|
165
|
+
host 127.0.0.1
|
166
|
+
port 1883
|
167
|
+
<format>
|
168
|
+
@type json
|
169
|
+
add_newline false
|
170
|
+
</format>
|
171
|
+
topic_rewrite_pattern '^([\w\/]+)$'
|
172
|
+
topic_rewrite_replacement '\1/rewritten'
|
173
|
+
</match>
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
You can also use mqtt_buf type which is implemented as Fluent::MqttBufferedOutput.
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
<match topic.**>
|
182
|
+
type mqtt_buf
|
183
|
+
host 127.0.0.1
|
184
|
+
port 1883
|
185
|
+
topic_rewrite_pattern '^([\w\/]+)$'
|
186
|
+
topic_rewrite_replacement '\1/rewritten'
|
187
|
+
# You can specify Buffer Plugin options
|
188
|
+
buffer_type memory
|
189
|
+
flush_interval 1s
|
190
|
+
</match>
|
191
|
+
|
192
|
+
```
|
193
|
+
|
194
|
+
For fluent-plugin-mqtt-io >= v0.3.0
|
195
|
+
|
196
|
+
```
|
197
|
+
|
198
|
+
<match topic.**>
|
199
|
+
@type mqtt
|
200
|
+
host 127.0.0.1
|
201
|
+
port 1883
|
202
|
+
<format>
|
203
|
+
@type json
|
204
|
+
</format>
|
205
|
+
topic_rewrite_pattern '^([\w\/]+)$'
|
206
|
+
topic_rewrite_replacement '\1/rewritten'
|
207
|
+
# You can specify Buffer Plugin options
|
208
|
+
<buffer>
|
209
|
+
buffer_type memory
|
210
|
+
flush_interval 1s
|
211
|
+
</buffer>
|
212
|
+
</match>
|
213
|
+
|
214
|
+
```
|
215
|
+
|
216
|
+
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.
|
217
|
+
|
218
|
+
## Use case examples
|
219
|
+
|
220
|
+
### Sensor data collection (not for Libelium now)
|
221
|
+
|
222
|
+
*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.
|
223
|
+
|
224
|
+
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.
|
225
|
+
|
226
|
+
![Libelium sensor data collection](https://github.com/toyokazu/fluent-plugin-mqtt-io/blob/master/images/libelium_sensor_data_collection.png "Libelium sensor data")
|
227
|
+
|
228
|
+
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.
|
229
|
+
|
230
|
+
```
|
231
|
+
<source>
|
232
|
+
type mqtt
|
233
|
+
host 192.168.1.100
|
234
|
+
port 1883
|
235
|
+
topic 'Libelium/+/+'
|
236
|
+
format xml
|
237
|
+
time_xpath '["cap:alert/cap:info/cap:onset", "text"]'
|
238
|
+
time_key '@timestamp'
|
239
|
+
attr_xpaths '[["cap:alert/cap:info/cap:parameter/cap:valueName", "text"]]'
|
240
|
+
value_xpaths '[["cap:alert/cap:info/cap:parameter/cap:value", "text"]]'
|
241
|
+
@label @MQTT_OUT
|
242
|
+
</source>
|
243
|
+
|
244
|
+
<label @MQTT_OUT>
|
245
|
+
<match **>
|
246
|
+
<store>
|
247
|
+
type elasticsearch
|
248
|
+
host localhost
|
249
|
+
port 9200
|
250
|
+
index_name libelium
|
251
|
+
type_name smartcity
|
252
|
+
include_tag_key true
|
253
|
+
tag_key sensor_id
|
254
|
+
logstash_format false
|
255
|
+
</store>
|
256
|
+
</match>
|
257
|
+
</label>
|
258
|
+
|
259
|
+
```
|
260
|
+
|
261
|
+
The following mapping is assumed to be created at ElasticSearch.
|
262
|
+
|
263
|
+
```
|
264
|
+
|
265
|
+
curl -XPUT 'http://localhost:9200/libelium/_mapping/smartcity' -d '
|
266
|
+
{"smartcity":
|
267
|
+
{"properties":
|
268
|
+
{
|
269
|
+
"sensor_id":{"type": "string"},
|
270
|
+
"DUST":{"type": "float"},
|
271
|
+
"MCP":{"type": "float"},
|
272
|
+
"HUMA":{"type": "float"},
|
273
|
+
"TCA":{"type": "float"},
|
274
|
+
"BAT":{"type": "float"},
|
275
|
+
"@timestamp":{"type":"date","format":"dateOptionalTime"}
|
276
|
+
}
|
277
|
+
}
|
278
|
+
}'
|
279
|
+
|
280
|
+
```
|
281
|
+
|
282
|
+
### MQTT message conversion
|
283
|
+
|
284
|
+
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.
|
285
|
+
|
286
|
+
![MQTT message conversion](https://github.com/toyokazu/fluent-plugin-mqtt-io/blob/master/images/mqtt_message_conversion.png "MQTT message conversion")
|
287
|
+
|
288
|
+
```
|
289
|
+
<source>
|
290
|
+
type mqtt
|
291
|
+
host 192.168.1.100
|
292
|
+
port 1883
|
293
|
+
topic 'Libelium/+/+'
|
294
|
+
format xml
|
295
|
+
time_xpath '["cap:alert/cap:info/cap:onset", "text"]'
|
296
|
+
time_key '@timestamp'
|
297
|
+
attr_xpaths '[["cap:alert/cap:info/cap:parameter/cap:valueName", "text"]]'
|
298
|
+
value_xpaths '[["cap:alert/cap:info/cap:parameter/cap:value", "text"]]'
|
299
|
+
@label @MQTT_OUT
|
300
|
+
</source>
|
301
|
+
|
302
|
+
<label @MQTT_OUT>
|
303
|
+
<match **>
|
304
|
+
type mqtt
|
305
|
+
host 192.168.1.100
|
306
|
+
port 1883
|
307
|
+
topic_rewrite_pattern '^([\w\/]+)$'
|
308
|
+
topic_rewrite_replacement '\1/rewritten'
|
309
|
+
</match>
|
310
|
+
</label>
|
311
|
+
|
312
|
+
```
|
313
|
+
|
314
|
+
|
315
|
+
### Sensor data uploads from tiny computers, e.g. Raspberry Pi, Edison, etc
|
316
|
+
|
317
|
+
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.
|
318
|
+
|
319
|
+
![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")
|
320
|
+
|
321
|
+
|
322
|
+
## Contributing
|
323
|
+
|
324
|
+
1. Fork it ( http://github.com/toyokazu/fluent-plugin-mqtt-io/fork )
|
325
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
326
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
327
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
328
|
+
5. Create new Pull Request
|
329
|
+
|
330
|
+
|
331
|
+
## License
|
332
|
+
|
333
|
+
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
@@ -5,7 +5,7 @@ Mqtt::IO Plugin is deeply inspired by Fluent::Plugin::Mqtt.
|
|
5
5
|
|
6
6
|
https://github.com/yuuna/fluent-plugin-mqtt
|
7
7
|
|
8
|
-
Mqtt::IO plugin focus on federating components, e.g. sensors, messaging platform and databases.
|
8
|
+
Mqtt::IO plugin focus on federating components, e.g. sensors, messaging platform and databases. Connection encryption/decryption (TLS) is supported by ruby-mqtt but end-to-end encryption/decryption is not supported in this plugin. [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
9
|
|
10
10
|
## Installation
|
11
11
|
|
@@ -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,48 @@ 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
|
-
- **
|
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
|
-
- **
|
72
|
-
- **
|
73
|
-
- **
|
74
|
-
- **
|
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
|
76
|
+
- **clean_session**: Setting for clean session client option (false persists session offline)
|
75
77
|
- **keep_alive**: An interval of sending keep alive packet (default 15 sec)
|
76
|
-
- **
|
77
|
-
- **
|
78
|
-
- **
|
79
|
-
- **
|
80
|
-
- **
|
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
|
78
|
+
- **monitor**: monitor section. only for fluent-plugin-mqtt-io
|
79
|
+
- **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
|
80
|
+
- **recv_time_key**: An attribute of recv_time (default: "recv_time"). only for fluent-plugin-mqtt-io
|
81
|
+
- **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io
|
82
|
+
- **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io
|
87
83
|
- **initial_interval**: An initial value of retry interval (s) (default 1)
|
88
84
|
- **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)
|
85
|
+
- **max_retry_interval**: Maximum value of retry interval (default 300)
|
86
|
+
- **max_retry_freq**: Threshold of retry frequency described by a number of retries per minutes. This option is provided for detecting failure via proxy services, e.g. ssh port forwarding. When the thresold is exceeded, MqttProxy::ExceedRetryFrequencyThresholdException is raised and the fluentd will be restarted. So, it is enough to be specified once for a MQTT server at a source/match directive in your configuration (default 10)
|
90
87
|
|
91
88
|
Input Plugin supports @label directive.
|
92
89
|
|
@@ -94,22 +91,7 @@ Input Plugin supports @label directive.
|
|
94
91
|
|
95
92
|
Output Plugin can be used via match directive.
|
96
93
|
|
97
|
-
For fluent-plugin-mqtt-io <= 0.2.3
|
98
|
-
|
99
94
|
```
|
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
95
|
<match topic.**>
|
114
96
|
@type mqtt
|
115
97
|
host 127.0.0.1
|
@@ -119,46 +101,31 @@ For fluent-plugin-mqtt-io ~> v0.3.0
|
|
119
101
|
add_newline false
|
120
102
|
</format>
|
121
103
|
</match>
|
122
|
-
|
123
104
|
```
|
124
105
|
|
125
|
-
The options are basically the same as Input Plugin except for "
|
106
|
+
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
107
|
|
108
|
+
- **qos**: Quality of Service (QoS) for message publishing, 0 or 1 is valid. 2 is not supported by mqtt client. Default is 1.
|
109
|
+
- **retain**: If set true the broker will keep the message even after sending it to all current subscribers. Default is false
|
127
110
|
- **time_key**: An attribute name used for timestamp field genarated from fluentd time field. Default is nil (omitted).
|
128
111
|
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
112
|
- **topic_rewrite_pattern**: Regexp pattern to extract replacement words from received topic or tag name
|
131
113
|
- **topic_rewrite_replacement**: Topic name used for the publish using extracted pattern
|
132
|
-
- **
|
133
|
-
- **
|
134
|
-
- **
|
135
|
-
- **
|
136
|
-
- **
|
137
|
-
- **
|
138
|
-
|
114
|
+
- **format**: Formatter plugin can be specified. [Formatter Plugin](https://docs.fluentd.org/v1.0/articles/formatter-plugin-overview)
|
115
|
+
- **monitor**: monitor section. only for fluent-plugin-mqtt-io
|
116
|
+
- **send_time**: Add send time to message in millisecond (ms) as integer for debug and performance/delay analysis. only for fluent-plugin-mqtt-io
|
117
|
+
- **send_time_key**: An attribute of send_time. only for fluent-plugin-mqtt-io
|
118
|
+
- **time_type**: Type of time format (string (default), unixtime, float) only for fluent-plugin-mqtt-io
|
119
|
+
- **time_format**: Time format e.g. %FT%T.%N%:z (refer strftime) only for fluent-plugin-mqtt-io
|
120
|
+
- **buffer**: synchronous/asynchronous buffer is supported. Refer [Buffer section configurations](https://docs.fluentd.org/v1.0/articles/buffer-section) for detailed configuration.
|
121
|
+
- **async**: Enable asynchronous buffer transfer. Default is false.
|
139
122
|
|
140
123
|
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
124
|
|
142
125
|
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
126
|
|
144
|
-
For fluent-plugin-mqtt-io <= v0.2.3
|
145
127
|
|
146
128
|
```
|
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
129
|
<match topic.**>
|
163
130
|
@type mqtt
|
164
131
|
host 127.0.0.1
|
@@ -170,30 +137,10 @@ For fluent-plugin-mqtt-io ~> v0.3.0
|
|
170
137
|
topic_rewrite_pattern '^([\w\/]+)$'
|
171
138
|
topic_rewrite_replacement '\1/rewritten'
|
172
139
|
</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
140
|
```
|
192
141
|
|
193
|
-
For fluent-plugin-mqtt-io ~> v0.3.0
|
194
142
|
|
195
143
|
```
|
196
|
-
|
197
144
|
<match topic.**>
|
198
145
|
@type mqtt
|
199
146
|
host 127.0.0.1
|
@@ -209,113 +156,7 @@ For fluent-plugin-mqtt-io ~> v0.3.0
|
|
209
156
|
flush_interval 1s
|
210
157
|
</buffer>
|
211
158
|
</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
159
|
```
|
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
160
|
|
320
161
|
|
321
162
|
## 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.
|
7
|
+
spec.version = "0.5.0"
|
8
8
|
spec.authors = ["Toyokazu Akiyama"]
|
9
9
|
spec.email = ["toyokazu@gmail.com"]
|
10
10
|
|
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_dependency 'fluentd', [">= 0.14.0", "< 2"]
|
25
25
|
spec.add_dependency "mqtt", "~> 0.5"
|
26
26
|
|
27
|
-
spec.add_development_dependency "bundler", "
|
28
|
-
spec.add_development_dependency "rake", "~>
|
27
|
+
spec.add_development_dependency "bundler", [">= 1.14", "< 2.3"]
|
28
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
29
29
|
spec.add_development_dependency "test-unit"
|
30
30
|
end
|
@@ -20,11 +20,6 @@ module Fluent::Plugin
|
|
20
20
|
config_param :@type, :string, default: 'none'
|
21
21
|
end
|
22
22
|
|
23
|
-
# bulk_trans is deprecated
|
24
|
-
# multiple entries must be inputted as an Array
|
25
|
-
#config_param :bulk_trans, :bool, default: true
|
26
|
-
#config_param :bulk_trans_sep, :string, default: "\t"
|
27
|
-
|
28
23
|
config_section :monitor, required: false, multi: false do
|
29
24
|
desc 'Record received time into message or not.'
|
30
25
|
config_param :recv_time, :bool, default: false
|
@@ -66,7 +61,22 @@ module Fluent::Plugin
|
|
66
61
|
:in_mqtt
|
67
62
|
end
|
68
63
|
|
69
|
-
def
|
64
|
+
def exit_thread
|
65
|
+
@get_thread.exit if !@get_thread.nil?
|
66
|
+
end
|
67
|
+
|
68
|
+
def disconnect
|
69
|
+
begin
|
70
|
+
@client.disconnect if @client.connected?
|
71
|
+
rescue => e
|
72
|
+
log.error "Error in in_mqtt#disconnect,#{e.class},#{e.message}"
|
73
|
+
end
|
74
|
+
exit_thread
|
75
|
+
end
|
76
|
+
|
77
|
+
def terminate
|
78
|
+
exit_thread
|
79
|
+
super
|
70
80
|
end
|
71
81
|
|
72
82
|
def after_connection
|
@@ -12,14 +12,18 @@ module Fluent::Plugin
|
|
12
12
|
base.config_param :port, :integer, default: MQTT_PORT
|
13
13
|
base.desc 'Client ID of MQTT Connection'
|
14
14
|
base.config_param :client_id, :string, default: nil
|
15
|
+
base.desc 'Specify clean session value.'
|
16
|
+
base.config_param :clean_session, :bool, default: true
|
15
17
|
base.desc 'Specify keep alive interval.'
|
16
18
|
base.config_param :keep_alive, :integer, default: 15
|
17
19
|
base.desc 'Specify initial connection retry interval.'
|
18
20
|
base.config_param :initial_interval, :integer, default: 1
|
19
21
|
base.desc 'Specify increasing ratio of connection retry interval.'
|
20
22
|
base.config_param :retry_inc_ratio, :integer, default: 2
|
21
|
-
base.desc 'Specify maximum connection retry interval.'
|
23
|
+
base.desc 'Specify the maximum connection retry interval.'
|
22
24
|
base.config_param :max_retry_interval, :integer, default: 300
|
25
|
+
base.desc 'Specify threshold of retry frequency as number of retries per minutes. Frequency is monitored per retry.'
|
26
|
+
base.config_param :max_retry_freq, :integer, default: 10
|
23
27
|
|
24
28
|
base.config_section :security, required: false, multi: false do
|
25
29
|
### User based authentication
|
@@ -40,20 +44,29 @@ module Fluent::Plugin
|
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
43
|
-
class
|
44
|
-
|
47
|
+
class MqttError < StandardError; end
|
48
|
+
|
49
|
+
class ExceedRetryFrequencyThresholdException < StandardError; end
|
45
50
|
|
46
51
|
def current_plugin_name
|
47
52
|
# should be implemented
|
48
53
|
end
|
49
54
|
|
50
55
|
def start_proxy
|
56
|
+
# Start a thread from main thread for handling a thread generated
|
57
|
+
# by MQTT::Client#get (in_mqtt). Dummy thread is used for out_mqtt
|
58
|
+
# to keep the same implementation style.
|
59
|
+
@proxy_thread = thread_create("#{current_plugin_name}_proxy".to_sym, &method(:proxy))
|
60
|
+
end
|
61
|
+
|
62
|
+
def proxy
|
51
63
|
log.debug "start mqtt proxy for #{current_plugin_name}"
|
52
64
|
log.debug "start to connect mqtt broker #{@host}:#{@port}"
|
53
65
|
opts = {
|
54
66
|
host: @host,
|
55
67
|
port: @port,
|
56
68
|
client_id: @client_id,
|
69
|
+
clean_session: @clean_session,
|
57
70
|
keep_alive: @keep_alive
|
58
71
|
}
|
59
72
|
opts[:username] = @security.username if @security.to_h.has_key?(:username)
|
@@ -66,12 +79,13 @@ module Fluent::Plugin
|
|
66
79
|
end
|
67
80
|
|
68
81
|
init_retry_interval
|
82
|
+
@retry_sequence = []
|
69
83
|
@client = MQTT::Client.new(opts)
|
70
84
|
connect
|
71
85
|
end
|
72
86
|
|
73
87
|
def shutdown_proxy
|
74
|
-
|
88
|
+
disconnect
|
75
89
|
end
|
76
90
|
|
77
91
|
def init_retry_interval
|
@@ -79,37 +93,68 @@ module Fluent::Plugin
|
|
79
93
|
end
|
80
94
|
|
81
95
|
def increment_retry_interval
|
82
|
-
return @
|
96
|
+
return @max_retry_interval if @retry_interval >= @max_retry_interval
|
83
97
|
@retry_interval = @retry_interval * @retry_inc_ratio
|
84
98
|
end
|
85
99
|
|
100
|
+
def update_retry_sequence(e)
|
101
|
+
@retry_sequence << {time: Time.now, error: "#{e.class}: #{e.message}"}
|
102
|
+
# delete old retry records
|
103
|
+
while @retry_sequence[0][:time] < Time.now - 60
|
104
|
+
@retry_sequence.shift
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def check_retry_frequency
|
109
|
+
return if @retry_sequence.size <= 1
|
110
|
+
if @retry_sequence.size > @max_retry_freq
|
111
|
+
log.error "Retry frequency threshold is exceeded: #{@retry_sequence}"
|
112
|
+
raise ExceedRetryFrequencyThresholdException
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
86
116
|
def retry_connect(e, message)
|
87
117
|
log.error "#{message},#{e.class},#{e.message}"
|
88
118
|
log.error "Retry in #{@retry_interval} sec"
|
89
|
-
|
119
|
+
update_retry_sequence(e)
|
120
|
+
check_retry_frequency
|
121
|
+
disconnect
|
122
|
+
sleep @retry_interval
|
90
123
|
increment_retry_interval
|
91
|
-
|
92
|
-
|
124
|
+
connect
|
125
|
+
# never reach here
|
93
126
|
end
|
94
127
|
|
95
|
-
def
|
128
|
+
def disconnect
|
96
129
|
# should be implemented
|
97
130
|
end
|
98
131
|
|
99
|
-
def
|
132
|
+
def terminate
|
133
|
+
end
|
134
|
+
|
135
|
+
def rescue_disconnection
|
136
|
+
# Errors cannot be caught by fluentd core must be caught here.
|
137
|
+
# Since fluentd core retries write method for buffered output
|
138
|
+
# when it caught Errors during the previous write,
|
139
|
+
# caughtable Error, e.g. MqttError, should be raised here.
|
100
140
|
begin
|
101
|
-
yield
|
141
|
+
yield
|
102
142
|
rescue MQTT::ProtocolException => e
|
103
|
-
#
|
104
|
-
# Currently MQTT::ProtocolException cannot be caught during @client.get
|
105
|
-
# and @client.publish. The reason must be investigated...
|
106
|
-
retry_connect(e, "Protocol error occurs.")
|
143
|
+
retry_connect(e, "Protocol error occurs in #{current_plugin_name}.")
|
107
144
|
rescue Timeout::Error => e
|
108
|
-
retry_connect(e, "Timeout error occurs.")
|
145
|
+
retry_connect(e, "Timeout error occurs in #{current_plugin_name}.")
|
109
146
|
rescue SystemCallError => e
|
110
|
-
retry_connect(e, "System call error occurs.")
|
147
|
+
retry_connect(e, "System call error occurs in #{current_plugin_name}.")
|
111
148
|
rescue StandardError=> e
|
112
|
-
retry_connect(e, "The other error occurs.")
|
149
|
+
retry_connect(e, "The other error occurs in #{current_plugin_name}.")
|
150
|
+
rescue MQTT::NotConnectedException=> e
|
151
|
+
# Since MQTT::NotConnectedException is raised only on publish,
|
152
|
+
# connection error should be catched before this error.
|
153
|
+
# So, reconnection process is omitted for this Exception
|
154
|
+
# to prevent waistful increment of retry interval.
|
155
|
+
#log.error "MQTT not connected exception occurs.,#{e.class},#{e.message}"
|
156
|
+
#retry_connect(e, "MQTT not connected exception occurs.")
|
157
|
+
#raise MqttError, "MQTT not connected exception occurs in #{current_plugin_name}."
|
113
158
|
end
|
114
159
|
end
|
115
160
|
|
@@ -120,14 +165,12 @@ module Fluent::Plugin
|
|
120
165
|
end
|
121
166
|
|
122
167
|
def connect
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
thread.join
|
130
|
-
end
|
168
|
+
rescue_disconnection do
|
169
|
+
@client.connect
|
170
|
+
log.debug "connected to mqtt broker #{@host}:#{@port} for #{current_plugin_name}"
|
171
|
+
init_retry_interval
|
172
|
+
thread = after_connection
|
173
|
+
thread.join
|
131
174
|
end
|
132
175
|
end
|
133
176
|
end
|
@@ -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,6 +89,25 @@ module Fluent::Plugin
|
|
75
89
|
# Shutdown the thread and close sockets or files here.
|
76
90
|
def shutdown
|
77
91
|
shutdown_proxy
|
92
|
+
exit_thread
|
93
|
+
super
|
94
|
+
end
|
95
|
+
|
96
|
+
def exit_thread
|
97
|
+
@dummy_thread.exit if !@dummy_thread.nil?
|
98
|
+
end
|
99
|
+
|
100
|
+
def disconnect
|
101
|
+
begin
|
102
|
+
@client.disconnect if @client.connected?
|
103
|
+
rescue => e
|
104
|
+
log.error "Error in out_mqtt#disconnect,#{e.class},#{e.message}"
|
105
|
+
end
|
106
|
+
exit_thread
|
107
|
+
end
|
108
|
+
|
109
|
+
def terminate
|
110
|
+
exit_thread
|
78
111
|
super
|
79
112
|
end
|
80
113
|
|
@@ -99,23 +132,11 @@ module Fluent::Plugin
|
|
99
132
|
end
|
100
133
|
|
101
134
|
def publish_event_stream(tag, es)
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
135
|
+
log.debug "publish_event_stream: #{es.class}"
|
136
|
+
es = inject_values_to_event_stream(tag, es)
|
137
|
+
es.each do |time, record|
|
117
138
|
rescue_disconnection do
|
118
|
-
|
139
|
+
publish(tag, time, record)
|
119
140
|
end
|
120
141
|
end
|
121
142
|
log.flush
|
@@ -134,13 +155,32 @@ module Fluent::Plugin
|
|
134
155
|
true
|
135
156
|
end
|
136
157
|
|
158
|
+
def publish(tag, time, record)
|
159
|
+
log.debug "MqttOutput::#{caller_locations(1,1)[0].label}: #{rewrite_tag(tag)}, #{time}, #{add_send_time(record)}"
|
160
|
+
@client.publish(
|
161
|
+
rewrite_tag(tag),
|
162
|
+
@formatter.format(tag, time, add_send_time(record)),
|
163
|
+
@retain,
|
164
|
+
@qos
|
165
|
+
)
|
166
|
+
end
|
167
|
+
|
137
168
|
def write(chunk)
|
138
169
|
return if chunk.empty?
|
139
170
|
chunk.each do |tag, time, record|
|
140
171
|
rescue_disconnection do
|
141
|
-
|
142
|
-
|
172
|
+
publish(tag, time, record)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def try_write(chunk)
|
178
|
+
return if chunk.empty?
|
179
|
+
rescue_disconnection do
|
180
|
+
chunk.each do |tag, time, record|
|
181
|
+
publish(tag, time, record)
|
143
182
|
end
|
183
|
+
commit_write(chunk.unique_id)
|
144
184
|
end
|
145
185
|
end
|
146
186
|
end
|
data/test/plugin/test_in_mqtt.rb
CHANGED
@@ -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
|
-
<
|
25
|
-
|
26
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
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
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toyokazu Akiyama
|
8
8
|
autorequire:
|
9
9
|
bindir: []
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fluentd
|
@@ -48,30 +48,36 @@ dependencies:
|
|
48
48
|
name: bundler
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '1.14'
|
54
|
+
- - "<"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '2.3'
|
54
57
|
type: :development
|
55
58
|
prerelease: false
|
56
59
|
version_requirements: !ruby/object:Gem::Requirement
|
57
60
|
requirements:
|
58
|
-
- - "
|
61
|
+
- - ">="
|
59
62
|
- !ruby/object:Gem::Version
|
60
63
|
version: '1.14'
|
64
|
+
- - "<"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '2.3'
|
61
67
|
- !ruby/object:Gem::Dependency
|
62
68
|
name: rake
|
63
69
|
requirement: !ruby/object:Gem::Requirement
|
64
70
|
requirements:
|
65
71
|
- - "~>"
|
66
72
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
73
|
+
version: '13.0'
|
68
74
|
type: :development
|
69
75
|
prerelease: false
|
70
76
|
version_requirements: !ruby/object:Gem::Requirement
|
71
77
|
requirements:
|
72
78
|
- - "~>"
|
73
79
|
- !ruby/object:Gem::Version
|
74
|
-
version: '
|
80
|
+
version: '13.0'
|
75
81
|
- !ruby/object:Gem::Dependency
|
76
82
|
name: test-unit
|
77
83
|
requirement: !ruby/object:Gem::Requirement
|
@@ -98,6 +104,7 @@ files:
|
|
98
104
|
- CHANGELOG.md
|
99
105
|
- Gemfile
|
100
106
|
- LICENSE
|
107
|
+
- README-old.md
|
101
108
|
- README.md
|
102
109
|
- Rakefile
|
103
110
|
- fluent-plugin-mqtt-io.gemspec
|
@@ -126,8 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
133
|
- !ruby/object:Gem::Version
|
127
134
|
version: '0'
|
128
135
|
requirements: []
|
129
|
-
|
130
|
-
rubygems_version: 2.6.13
|
136
|
+
rubygems_version: 3.1.4
|
131
137
|
signing_key:
|
132
138
|
specification_version: 4
|
133
139
|
summary: fluentd input/output plugin for mqtt broker
|