2024-06-04 19:06:10 +00:00
|
|
|
import os
|
2024-06-05 10:23:17 +00:00
|
|
|
from argparse import ArgumentParser
|
2024-06-04 19:06:10 +00:00
|
|
|
from prometheus_client import start_http_server, Gauge
|
|
|
|
import time
|
|
|
|
|
|
|
|
# Configuration (replace with your actual path)
|
2024-06-05 10:23:17 +00:00
|
|
|
PINS_DIR = "/srv/cryptpad/data/pins/"
|
2024-06-04 19:06:10 +00:00
|
|
|
PORT = 8000
|
|
|
|
|
|
|
|
# Prometheus metric
|
2024-06-05 10:23:17 +00:00
|
|
|
registered_users_gauge = Gauge(
|
|
|
|
"cryptpad_registered_users", "Number of registered users"
|
|
|
|
)
|
2024-06-04 19:06:10 +00:00
|
|
|
|
|
|
|
|
2024-06-05 10:23:17 +00:00
|
|
|
def count_registered_users(pins_dir: os.PathLike = PINS_DIR) -> int:
|
|
|
|
f"""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:
|
2024-06-04 19:06:10 +00:00
|
|
|
https://github.com/cryptpad/cryptpad/blob/7dbec1bd25b46e514ba82adad209961627410025/lib/commands/admin-rpc.js#L105
|
|
|
|
|
|
|
|
Args:
|
2024-06-05 10:23:17 +00:00
|
|
|
pins_dir (os.PathLike): Path to the user pins directory (default: {PINS_DIR})
|
2024-06-04 19:06:10 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-06-05 10:23:17 +00:00
|
|
|
|
|
|
|
def update_metrics(pins_dir: os.PathLike = PINS_DIR):
|
|
|
|
f"""Update the Prometheus metrics with the current number of registered users
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pins_dir (os.PathLike, optional): Path to the user pins directory (default: {PINS_DIR})
|
|
|
|
"""
|
|
|
|
user_count = count_registered_users(pins_dir)
|
2024-06-04 19:06:10 +00:00
|
|
|
registered_users_gauge.set(user_count)
|
|
|
|
print(f"Updated registered users count: {user_count}")
|
|
|
|
|
2024-06-05 10:23:17 +00:00
|
|
|
|
2024-06-04 19:06:10 +00:00
|
|
|
def main():
|
2024-06-05 10:23:17 +00:00
|
|
|
parser = ArgumentParser(description="CryptPad Prometheus exporter")
|
|
|
|
parser.add_argument(
|
|
|
|
"--pins-dir",
|
|
|
|
type=str,
|
|
|
|
default=PINS_DIR,
|
|
|
|
help=f"Path to the user pins directory (default: {PINS_DIR})",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--port",
|
|
|
|
type=int,
|
|
|
|
default=PORT,
|
|
|
|
help=f"Port to expose the Prometheus metrics (default: {PORT})",
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
pins_dir = args.pins_dir or os.environ.get("PINS_DIR") or PINS_DIR
|
|
|
|
port = int(args.port or os.environ.get("PORT") or PORT)
|
|
|
|
|
2024-06-04 19:06:10 +00:00
|
|
|
# Start up the server to expose the metrics.
|
2024-06-05 10:23:17 +00:00
|
|
|
start_http_server(port)
|
|
|
|
print("Prometheus exporter started on port " + port)
|
2024-06-04 19:06:10 +00:00
|
|
|
|
|
|
|
# Update metrics every 30 seconds
|
|
|
|
while True:
|
2024-06-05 10:23:17 +00:00
|
|
|
update_metrics(pins_dir)
|
2024-06-04 19:06:10 +00:00
|
|
|
time.sleep(30)
|
|
|
|
|
|
|
|
|
2024-06-05 10:23:17 +00:00
|
|
|
if __name__ == "__main__":
|
2024-06-04 19:06:10 +00:00
|
|
|
main()
|