Metadata-Version: 2.1
Name: fs-monitor-mqtt
Version: 0.1.3
Summary: A file system monitor that can publish file change events to MQTT broker.
Home-page: https://github.com/likair/fs-monitor-mqtt
License: MIT
Keywords: filesystem,mqtt,monitor
Author: Likai Ren
Author-email: lebsfi@gmail.com
Requires-Python: >=3.9,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Requires-Dist: paho-mqtt (>=1.6.1,<2.0.0)
Requires-Dist: typer (>=0.9.0,<0.10.0)
Requires-Dist: watchdog (>=3.0.0,<4.0.0)
Project-URL: Repository, https://github.com/likair/fs-monitor-mqtt
Description-Content-Type: text/markdown

# fs-monitor-mqtt

## Overview

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

## Dependencies

- [watchdog](https://github.com/gorakhargosh/watchdog): Cross-platform file system events monitoring.
- [paho-mqtt](https://github.com/eclipse/paho.mqtt.python): MQTT client library.
- [typer](https://github.com/tiangolo/typer): CLI application framework based on Python type hints.

## Getting Started

### Prerequisites

- Docker

### Instructions (Run as a Docker Container)

Run the following commands in the root directory of the project.

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

    ```
    docker build -t fs-monitor .
    ```

2. Start the 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` 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 fs-monitor --path /tmp --address localhost --port 1883
    ```

5. (optional) Follow the output of the `fs-monitor` container.

    ```
    docker logs fs-monitor -f
    ```

6. Make a change to the file system that is being monitored. For example, create a new file.

    ```
    docker exec fs-monitor touch /tmp/1
    ```

7. Verify that the MQTT client receives the message. For example,

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

8. Stop the `fs-monitor` container.

    ```
    docker stop fs-monitor
    ```

9. Stop the MQTT broker.

    ```
    docker stop 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](assets/demo.png)

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.

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.

