Skip to main content

Recursively watch a directory for modifications and publish file content to an MQTT broker

Project description

This simple Python program portably watches a directory recursively and publishes the content of newly created and modified files as payload to an MQTT broker. Files which are deleted are published with a NULL payload.

The path to the directory to watch recursively (default .), as well as a list of files to ignore (*.swp, *.o, *.pyc), the broker host (localhost) and port number (1883) must be specified in via environment variables , together with the topic prefix to which to publish to (watch/).

Installation

git clone https://github.com/jpmens/mqtt-watchdir.git
cd mqtt-watchdir
virtualenv watchdir
source watchdir/bin/activate
pip install -e .

Configuration

Set the following optional environment variables before invoking mqtt-watchdir.py:

  • MQTTHOST (default "localhost") is the name/address of the MQTT broker.

  • MQTTPORT (default 1883) is the TCP port number of the broker.

  • MQTTWATCHDIR (default: ".") is the path to the directory to watch.

  • MQTTQOS (default: 0) is the MQTT Quality of Service (QoS) to use on publish. Allowed values are 0, 1, or 2.

  • MQTTRETAIN (default: 0) specifies whether the “retain” flag should be set on publish. Set to 1 if you want messages to be retained.

  • MQTTPREFIX (default: "watch") is the prefix to be prepended (with a slash) to the MQTT topic name. The topic name is formed from this prefix plus the path name of the file that is being modified (i.e. watched). You can set this to an empty string ("") to avoid prefixing the topic name.

  • Set WATCHDEBUG (default: 0) to 1 to show debugging information.

Testing

Launch mosquitto_sub:

mosquitto_sub -v -t 'watch/#'

Launch this program and, in another terminal, try something like this:

echo Hello World > message
echo JP > myname
rm myname

whereupon, on the first window, you should see:

watch/message Hello World
watch/myname JP
watch/myname (null)

Requirements

  • watchdog, a Python library to monitor file-system events.

  • Mosquitto’s Python module

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mqtt-watchdir-2.0-8-ge6ac542.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file mqtt-watchdir-2.0-8-ge6ac542.tar.gz.

File metadata

File hashes

Hashes for mqtt-watchdir-2.0-8-ge6ac542.tar.gz
Algorithm Hash digest
SHA256 bd88a1bef096bfef6812b32b94303ce92f6755d229e390b9e1af551255598437
MD5 458776d50ef812e6cda204e1fc4da3d0
BLAKE2b-256 ca148d4592c1c0650fbad3628a86a932448e0e9919df43171e99871fc5ae7989

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page