commit dc6654d1834d95c85c706f4d0079c17cb2685894 Author: Kumi Date: Tue Jun 4 21:06:10 2024 +0200 feat: Initialize CryptPad Prometheus Exporter Initialized a new Prometheus exporter for CryptPad with foundational setup including the creation of a `.gitignore` file, adding the MIT License, and a descriptive `README.md`. Developed the main functionality in `exporter.py` to expose the count of registered users as a Prometheus metric. Also, a `requirements.txt` file was set up with the necessary Python package. This initial setup lays the groundwork for future enhancements and metrics to be added, making it easier to monitor CryptPad's usage. The decision to count registered users by their data folders mirrors CryptPad's own administrative dashboard approach, ensuring consistency in reported metrics. The inclusion of the `.gitignore` file ensures that development environments remain clean, and by choosing the MIT License, the project is kept open and permissive for broad use and contribution. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eba74f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bdfd5f1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Private.coffee Team + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..41c12ad --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# CryptPad Prometheus Exporter + +This is a simple Prometheus exporter for CryptPad. Currently it only exports the number of registered users, but it is still a work in progress. + +## Installation + +```bash +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt +``` + +## Usage + +```bash +source venv/bin/activate +python exporter.py +``` + +## Configuration + +Currently, the exporter can only be configured by modifying the source code. The following variables can be changed: + +- `PINS_DIR`: The directory where the CryptPad pins are stored. Default: `/srv/cryptpad/data/pins` +- `PORT`: The port the exporter listens on. Default: `8000` + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/exporter.py b/exporter.py new file mode 100644 index 0000000..13db130 --- /dev/null +++ b/exporter.py @@ -0,0 +1,54 @@ +import os +from prometheus_client import start_http_server, Gauge +import time + +# Configuration (replace with your actual path) +PINS_DIR = '/srv/cryptpad/data/pins/' +PORT = 8000 + +# Prometheus metric +registered_users_gauge = Gauge('cryptpad_registered_users', 'Number of registered users') + +def count_registered_users(pins_dir: os.PathLike) -> int: + """Returns the number of registered users by counting the number of folders in the user data directory + + This emulates the way CryptPad counts registered users in the admin dashboard, see: + https://github.com/cryptpad/cryptpad/blob/7dbec1bd25b46e514ba82adad209961627410025/lib/commands/admin-rpc.js#L105 + + Args: + pins_dir (os.PathLike): Path to the user pins directory (e.g. /srv/cryptpad/data/pins/) + + Returns: + int: Number of registered users + """ + + try: + users_count = 0 + for folder in os.listdir(pins_dir): + folder_path = os.path.join(pins_dir, folder) + if os.path.isdir(folder_path): + users_count += len(os.listdir(folder_path)) + return users_count + except Exception as e: + print(f"Error counting registered users: {e}") + return 0 + +def update_metrics(): + """Update the Prometheus metrics with the current number of registered users""" + user_count = count_registered_users(PINS_DIR) + registered_users_gauge.set(user_count) + print(f"Updated registered users count: {user_count}") + +def main(): + # Start up the server to expose the metrics. + start_http_server(PORT) + print("Prometheus exporter started on port " + PORT) + + # Update metrics every 30 seconds + while True: + update_metrics() + time.sleep(30) + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a73ea1c --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +prometheus_client \ No newline at end of file