APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
'\" t
.\"     Title: mosquitto
.\"    Author: [see the "Author" section]
.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
.\"      Date: 06/18/2019
.\"    Manual: System management commands
.\"    Source: Mosquitto Project
.\"  Language: English
.\"
.TH "MOSQUITTO" "8" "06/18/2019" "Mosquitto Project" "System management commands"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
mosquitto \- an MQTT broker
.SH "SYNOPSIS"
.HP \w'\fBmosquitto\fR\ 'u
\fBmosquitto\fR [\-c\ \fIconfig\ file\fR] [\-d | \-\-daemon] [\-p\ \fIport\ number\fR] [\-v]
.SH "DESCRIPTION"
.PP
\fBmosquitto\fR
is a broker for the MQTT protocol version 3\&.1\&.1/3\&.1\&.
.SH "OPTIONS"
.PP
\fB\-c\fR, \fB\-\-config\-file\fR
.RS 4
Load configuration from a file\&. If not given, the default values as described in
\fBmosquitto.conf\fR(5)
are used\&.
.RE
.PP
\fB\-d\fR, \fB\-\-daemon\fR
.RS 4
Run
\fBmosquitto\fR
in the background as a daemon\&. All other behaviour remains the same\&.
.RE
.PP
\fB\-p\fR, \fB\-\-port\fR
.RS 4
Listen on the port specified instead of the default 1883\&. This acts in addition to the port setting in the config file\&. May be specified multiple times to open multiple sockets listening on different ports\&. This socket will be bound to all network interfaces\&.
.RE
.PP
\fB\-v\fR, \fB\-\-verbose\fR
.RS 4
Use verbose logging\&. This is equivalent to setting
\fBlog_type\fR
to
\fBall\fR
in the configuration file\&. This overrides and logging options given in the configuration file\&.
.RE
.SH "CONFIGURATION"
.PP
The broker can be configured using a configuration file as described in
\fBmosquitto.conf\fR(5)
and this is the main point of information for mosquitto\&. The files required for SSL/TLS support are described in
\fBmosquitto-tls\fR(7)\&.
.SH "BROKER STATUS"
.PP
Clients can find information about the broker by subscribing to topics in the $SYS hierarchy as follows\&. Topics marked as static are only sent once per client on subscription\&. All other topics are updated every
\fBsys_interval\fR
seconds\&. If
\fBsys_interval\fR
is 0, then updates are not sent\&.
.PP
Note that if you are using a command line client to interact with the $SYS topics and your shell interprets $ as an environment variable, you need to place the topic in single quotes \*(Aq$SYS/\&.\&.\&.\*(Aq or to escape the dollar symbol: \e$SYS/\&.\&.\&. otherwise the $SYS will be treated as an environment variable\&.
.PP
\fB$SYS/broker/bytes/received\fR
.RS 4
The total number of bytes received since the broker started\&.
.RE
.PP
\fB$SYS/broker/bytes/sent\fR
.RS 4
The total number of bytes sent since the broker started\&.
.RE
.PP
\fB$SYS/broker/clients/connected\fR, \fB$SYS/broker/clients/active\fR (deprecated)
.RS 4
The number of currently connected clients\&.
.RE
.PP
\fB$SYS/broker/clients/expired\fR
.RS 4
The number of disconnected persistent clients that have been expired and removed through the persistent_client_expiration option\&.
.RE
.PP
\fB$SYS/broker/clients/disconnected\fR, \fB$SYS/broker/clients/inactive\fR (deprecated)
.RS 4
The total number of persistent clients (with clean session disabled) that are registered at the broker but are currently disconnected\&.
.RE
.PP
\fB$SYS/broker/clients/maximum\fR
.RS 4
The maximum number of clients that have been connected to the broker at the same time\&.
.RE
.PP
\fB$SYS/broker/clients/total\fR
.RS 4
The total number of active and inactive clients currently connected and registered on the broker\&.
.RE
.PP
\fB$SYS/broker/connection/#\fR
.RS 4
When bridges are configured to/from the broker, common practice is to provide a status topic that indicates the state of the connection\&. This is provided within $SYS/broker/connection/ by default\&. If the value of the topic is 1 the connection is active, if 0 then it is not active\&. See the Bridges section below for more information on bridges\&.
.RE
.PP
\fB$SYS/broker/heap/current size\fR
.RS 4
The current size of the heap memory in use by mosquitto\&. Note that this topic may be unavailable depending on compile time options\&.
.RE
.PP
\fB$SYS/broker/heap/maximum size\fR
.RS 4
The largest amount of heap memory used by mosquitto\&. Note that this topic may be unavailable depending on compile time options\&.
.RE
.PP
\fB$SYS/broker/load/connections/+\fR
.RS 4
The moving average of the number of CONNECT packets received by the broker over different time intervals\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of connections received in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/load/bytes/received/+\fR
.RS 4
The moving average of the number of bytes received by the broker over different time intervals\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of bytes received in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/load/bytes/sent/+\fR
.RS 4
The moving average of the number of bytes sent by the broker over different time intervals\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of bytes sent in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/load/messages/received/+\fR
.RS 4
The moving average of the number of all types of MQTT messages received by the broker over different time intervals\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of messages received in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/load/messages/sent/+\fR
.RS 4
The moving average of the number of all types of MQTT messages sent by the broker over different time intervals\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of messages send in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/load/publish/dropped/+\fR
.RS 4
The moving average of the number of publish messages dropped by the broker over different time intervals\&. This shows the rate at which durable clients that are disconnected are losing messages\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of messages dropped in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/load/publish/received/+\fR
.RS 4
The moving average of the number of publish messages received by the broker over different time intervals\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of publish messages received in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/load/publish/sent/+\fR
.RS 4
The moving average of the number of publish messages sent by the broker over different time intervals\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of publish messages sent in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/load/sockets/+\fR
.RS 4
The moving average of the number of socket connections opened to the broker over different time intervals\&. The final "+" of the hierarchy can be 1min, 5min or 15min\&. The value returned represents the number of socket connections in 1 minute, averaged over 1, 5 or 15 minutes\&.
.RE
.PP
\fB$SYS/broker/messages/inflight\fR
.RS 4
The number of messages with QoS>0 that are awaiting acknowledgments\&.
.RE
.PP
\fB$SYS/broker/messages/received\fR
.RS 4
The total number of messages of any type received since the broker started\&.
.RE
.PP
\fB$SYS/broker/messages/sent\fR
.RS 4
The total number of messages of any type sent since the broker started\&.
.RE
.PP
\fB$SYS/broker/publish/messages/dropped\fR
.RS 4
The total number of publish messages that have been dropped due to inflight/queuing limits\&. See the max_inflight_messages and max_queued_messages options in
\fBmosquitto.conf\fR(5)
for more information\&.
.RE
.PP
\fB$SYS/broker/publish/messages/received\fR
.RS 4
The total number of PUBLISH messages received since the broker started\&.
.RE
.PP
\fB$SYS/broker/publish/messages/sent\fR
.RS 4
The total number of PUBLISH messages sent since the broker started\&.
.RE
.PP
\fB$SYS/broker/retained messages/count\fR
.RS 4
The total number of retained messages active on the broker\&.
.RE
.PP
\fB$SYS/broker/store/messages/count\fR, \fB$SYS/broker/messages/stored\fR (deprecated)
.RS 4
The number of messages currently held in the message store\&. This includes retained messages and messages queued for durable clients\&.
.RE
.PP
\fB$SYS/broker/store/messages/bytes\fR
.RS 4
The number of bytes currently held by message payloads in the message store\&. This includes retained messages and messages queued for durable clients\&.
.RE
.PP
\fB$SYS/broker/subscriptions/count\fR
.RS 4
The total number of subscriptions active on the broker\&.
.RE
.PP
\fB$SYS/broker/version\fR
.RS 4
The version of the broker\&. Static\&.
.RE
.SH "WILDCARD TOPIC SUBSCRIPTIONS"
.PP
In addition to allowing clients to subscribe to specific topics, mosquitto also allows the use of two wildcards in subscriptions\&.
\fB+\fR
is the wildcard used to match a single level of hierarchy\&. For example, for a topic of "a/b/c/d", the following example subscriptions will match:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a/b/c/d
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
+/b/c/d
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a/+/c/d
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a/+/+/d
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
+/+/+/+
.RE
.PP
The following subscriptions will not match:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a/b/c
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
b/+/c/d
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
+/+/+
.RE
.PP
The second wildcard is
\fB#\fR
and is used to match all subsequent levels of hierarchy\&. With a topic of "a/b/c/d", the following example subscriptions will match:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a/b/c/d
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
#
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a/#
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a/b/#
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a/b/c/#
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
+/b/c/#
.RE
.PP
The $SYS hierarchy does not match a subscription of "#"\&. If you want to observe the entire $SYS hierarchy, subscribe to $SYS/#\&.
.PP
Note that the wildcards must be only ever used on their own, so a subscription of "a/b+/c" is not valid use of a wildcard\&. The
\fB#\fR
wildcard must only ever be used as the final character of a subscription\&.
.SH "BRIDGES"
.PP
Multiple brokers can be connected together with the bridging functionality\&. This is useful where it is desirable to share information between locations, but where not all of the information needs to be shared\&. An example could be where a number of users are running a broker to help record power usage and for a number of other reasons\&. The power usage could be shared through bridging all of the user brokers to a common broker, allowing the power usage of all users to be collected and compared\&. The other information would remain local to each broker\&.
.PP
For information on configuring bridges, see
\fBmosquitto.conf\fR(5)\&.
.SH "SIGNALS"
.PP
SIGHUP
.RS 4
Upon receiving the SIGHUP signal, mosquitto will attempt to reload configuration file data, assuming that the
\fB\-c\fR
argument was provided when mosquitto was started\&. Not all configuration parameters can be reloaded without restarting\&. See
\fBmosquitto.conf\fR(5)
for details\&.
.RE
.PP
SIGUSR1
.RS 4
Upon receiving the SIGUSR1 signal, mosquitto will write the persistence database to disk\&. This signal is only acted upon if persistence is enabled\&.
.RE
.PP
SIGUSR2
.RS 4
The SIGUSR2 signal causes mosquitto to print out the current subscription tree, along with information about where retained messages exist\&. This is intended as a testing feature only and may be removed at any time\&.
.RE
.SH "FILES"
.PP
/etc/mosquitto/mosquitto\&.conf
.RS 4
Configuration file\&. See
\fBmosquitto.conf\fR(5)\&.
.RE
.PP
/var/lib/mosquitto/mosquitto\&.db
.RS 4
Persistent message data storage location if persist enabled\&.
.RE
.PP
/etc/hosts\&.allow, /etc/hosts\&.deny
.RS 4
Host access control via tcp\-wrappers as described in
\fBhosts_access\fR(5)\&.
.RE
.SH "BUGS"
.PP
\fBmosquitto\fR
bug information can be found at
\m[blue]\fB\%https://github.com/eclipse/mosquitto/issues\fR\m[]
.SH "SEE ALSO"
\fBmqtt\fR(7), \fBmosquitto-tls\fR(7), \fBmosquitto.conf\fR(5), \fBhosts_access\fR(5), \fBmosquitto_passwd\fR(1), \fBmosquitto_pub\fR(1), \fBmosquitto_rr\fR(1), \fBmosquitto_sub\fR(1), \fBlibmosquitto\fR(3)
.SH "THANKS"
.PP
Thanks to Andy Stanford\-Clark for being one of the people who came up with MQTT in the first place\&. Thanks to Andy and Nicholas O\*(AqLeary for providing clarifications of the protocol\&.
.PP
Thanks also to everybody at the Ubuntu UK Podcast and Linux Outlaws for organising OggCamp, where Andy gave a talk that inspired mosquitto\&.
.SH "AUTHOR"
.PP
Roger Light
<roger@atchoo\&.org>