Skip to content

Commit 36b1936

Browse files
committed
Add feature to publish mqttwarn status information
1 parent bf6f840 commit 36b1936

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in progress
1212
- Allow parameters of the ``file`` service to be defined on a per-file basis.
1313
Thanks, @Gulaschcowboy!
1414
- Add software tests for ``file`` service.
15+
- Add feature to publish mqttwarn status information. Thanks, @psyciknz!
1516

1617

1718
2021-09-29 0.26.2

HANDBOOK.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ I've written an introductory post, explaining [what mqttwarn can be used for](ht
132132
; name the service providers you will be using.
133133
launch = file, log, osxnotify, mysql, smtp
134134

135-
; Publish the current mqttwarn version to the topic (retained)
136-
publishversion = mqttwarn/version
135+
; Publish mqttwarn status information (retained)
136+
status_publish = True
137+
; status_topic = mqttwarn/$SYS
137138

138139

139140
; optional: TLS parameters. (Don't forget to set the port number for
@@ -167,15 +168,21 @@ I've written an introductory post, explaining [what mqttwarn can be used for](ht
167168
168169
You should launch every service you want to use from your topic/target definitions here.
169170
170-
###### `publishversion`
171+
###### `status_publish`
171172
172-
A topic that, when it exists, mqttwarn will publish it's current version as a retained payload.
173-
Useful for automated updates (docker watchtower and the like)
173+
Like with Mosquitto's `$SYS` topic, `mqttwarn` can publish status information to the broker.
174+
This is useful for automated updates (Docker Swarm, Watchtower, etc.).
175+
176+
To enable status information publishing, configure `status_publish = True`. The other option,
177+
`status_topic`, defaults to `status_topic = mqttwarn/$SYS`.
178+
The messages will be published with the `retained` flag.
174179

175180
For example:
176181
```
177-
root@mymachine:~$ mosquitto_sub -v -t mqttwarn/version
178-
mqttwarn/version 0.25.0
182+
root@mymachine:~$ mosquitto_sub -t 'mqttwarn/$SYS/#' -v
183+
mqttwarn/$SYS/version 0.26.2
184+
mqttwarn/$SYS/platform darwin
185+
mqttwarn/$SYS/python/version 3.9.7
179186
```
180187

181188

mqttwarn/core.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# (c) 2014-2021 The mqttwarn developers
3+
import platform
34
from builtins import object
45
from past.builtins import cmp
56
from builtins import chr
@@ -629,11 +630,8 @@ def connect():
629630
# Update our runtime context (used by functions etc) now we have a connected MQTT client
630631
context.invoker.srv.mqttc = mqttc
631632

632-
try:
633-
if cf.publishversion:
634-
mqttc.publish(cf.publishversion,__version__,retain=True)
635-
except:
636-
pass
633+
# Publish status information to `mqttwarn/$SYS` topic.
634+
publish_status_information()
637635

638636
# Launch worker threads to operate on queue
639637
start_workers()
@@ -654,6 +652,45 @@ def connect():
654652
time.sleep(reconnect_interval)
655653

656654

655+
def publish_status_information():
656+
"""
657+
Implement `$SYS` topic, like Mosquitto's "Broker Status".
658+
https://mosquitto.org/man/mosquitto-8.html#idm289
659+
660+
Idea from Mosquitto::
661+
662+
$ mosquitto_sub -t '$SYS/broker/version' -v
663+
$SYS/broker/version mosquitto version 2.0.10
664+
665+
Synopsis::
666+
667+
$ mosquitto_sub -t 'mqttwarn/$SYS/#' -v
668+
669+
mqttwarn/$SYS/version 0.26.2
670+
mqttwarn/$SYS/platform darwin
671+
mqttwarn/$SYS/python/version 3.9.7
672+
673+
"""
674+
if cf.has_option("defaults", "status_publish") and cf.status_publish:
675+
676+
status_topic = cf.g("defaults", "status_topic", "mqttwarn/$SYS")
677+
logger.info(f"Publishing status information to {status_topic}")
678+
679+
# Items are tuples of (subtopic, message)
680+
publications = [
681+
("version", __version__),
682+
("platform", sys.platform),
683+
("python/version", platform.python_version()),
684+
]
685+
try:
686+
for publication in publications:
687+
subtopic, message = publication
688+
mqttc.publish(status_topic + "/" + subtopic, message, retain=True)
689+
690+
except:
691+
logger.exception("Unable to publish status information")
692+
693+
657694
def start_workers():
658695

659696
# Launch worker threads to operate on queue

mqttwarn/examples/basic/mqttwarn.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ functions = 'samplefuncs.py'
5555
; name the service providers you will be using.
5656
launch = file, log
5757

58-
; Publish the current mqttwarn version to the topic (retained)
59-
publishversion = mqttwarn/version
58+
; Publish mqttwarn status information (retained)
59+
status_publish = True
60+
; status_topic = mqttwarn/$SYS
6061

6162

6263

0 commit comments

Comments
 (0)