Kumi
dc6654d183
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.
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
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()
|