Skip to main content

A file system monitor that can publish file change events to MQTT broker.

Project description

fs-monitor-mqtt

Overview

The monitor script listens to filesystem events such as created, modified, deleted, moved, and publishes them to a MQTT broker.

The topic of the published message is the absolute path of the file or directory that is being monitored. An example payload is a JSON object as follows:

{
    "timestamp": "2023-10-03T15:42:20.373182",
    "event_type": "modified",
    "is_directory": false,
    "src_path": "/tmp/1"
}

Dependencies

  • watchdog: Cross-platform file system events monitoring.
  • paho-mqtt: MQTT client library.
  • typer: CLI application framework based on Python type hints.

PyPI Package

The package is published on PyPI.

Getting Started

Prerequisites

  • Docker

Instructions (Run as a Docker Container)

  1. Build the Docker image including the application and its dependencies.

    docker build -t fs-monitor-mqtt .
    
  2. Start a MQTT broker.

    docker run -d -p 1883:1883 --name mosquitto eclipse-mosquitto mosquitto -c /mosquitto-no-auth.conf
    
  3. Start a MQTT client that subscribes to all MQTT topics (so that messages can be received and verified easily).

    docker run --network=host eclipse-mosquitto mosquitto_sub -t '#' -h 'localhost' -p 1883
    
  4. In a separate terminal, start fs-monitor-mqtt docker container in the background. The default configuration is to monitor the /tmp directory, publish to the MQTT broker running on the host machine with the default port of 1883.

    docker run -d --network=host --name fs-monitor-mqtt fs-monitor-mqtt --path /tmp --address localhost --port 1883
    
  5. (optional) Follow the output of the fs-monitor-mqtt container.

    docker logs fs-monitor-mqtt -f
    
  6. Make a change to the file system that is being monitored. For example, create a new file.

    docker exec fs-monitor-mqtt touch /tmp/1
    
  7. Verify that the MQTT client receives the message. For example,

    {"timestamp": "2023-10-03T20:03:55.026184", "event_type": "created", "is_directory": false, "src_path": "/tmp/1"}
    {"timestamp": "2023-10-03T20:03:55.026759", "event_type": "modified", "is_directory": true, "src_path": "/tmp"}
    {"timestamp": "2023-10-03T20:03:55.026860", "event_type": "modified", "is_directory": false, "src_path": "/tmp/1"}
    {"timestamp": "2023-10-03T20:03:55.026920", "event_type": "modified", "is_directory": true, "src_path": "/tmp"}
    
  8. Stop the fs-monitor-mqtt container.

    docker rm -f fs-monitor-mqtt
    
  9. Stop the MQTT broker.

    docker rm -f mosquitto
    

Development

Prerequisites

  • Python > 3.9

Instructions

Run default make command which does the following:

  • install poetry
  • install dependencies
  • install pre-commit hooks
  • run unit tests (Pytest)
  • start the MQTT broker
  • run integration tests (Robot)
  • finally stopping the MQTT broker.

demo

Check more commands in the Makefile.

Unit Tests

Run unit tests with make test. The unit tests are written using Pytest.

Integration Tests

Run integration tests with make integration_test. The integration tests are written using Robot Framework.

Future Work

  • Performance tests.
  • GitHub Actions for CI/CD.
  • Add more unit tests.
  • Add more integration tests.

MIT License

Copyright (c) 2023 Likai R.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

fs_monitor_mqtt-0.2.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fs_monitor_mqtt-0.2.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file fs_monitor_mqtt-0.2.1.tar.gz.

File metadata

  • Download URL: fs_monitor_mqtt-0.2.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.8.12 Darwin/23.0.0

File hashes

Hashes for fs_monitor_mqtt-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5b8fe5829fe603b20a64e2bf8a9ae7f8813e7a34cbeb5a72b141880dfce6d9c5
MD5 095c5365f0eab77b1432c60242339c49
BLAKE2b-256 2d6c9bd04e77dca893e6c297eaad8096ddee894dd4096554ad2e273c7849e963

See more details on using hashes here.

File details

Details for the file fs_monitor_mqtt-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: fs_monitor_mqtt-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.8.12 Darwin/23.0.0

File hashes

Hashes for fs_monitor_mqtt-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a7df20b003b61042c853ea35ef4113ccb616d5085c8f5f5fc29eed483277f12
MD5 fdfce15a3f394489c283a61f7a80271e
BLAKE2b-256 a73465da4283179dd8d523429565819e2f6c14b30b62c8320b96ba0056c7b57d

See more details on using hashes here.

Supported by

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