prometheus-piped/prometheus_piped.py
Kumi dd890f1708
feat: Fork exporter to Prometheus Piped
Forked the allkeyshop.com game price exporter for a Prometheus exporter for Piped instances, focusing on tracking registered users. This shift addresses the need for monitoring Piped, a self-hosted alternative to YouTube, instead of game pricing data. The changes include:

- Updated the .gitignore, LICENSE, README.md, and requirements.txt to reflect the new functionality and dependencies.
- Removed the allkeyshop.py script in favor of a new script, prometheus_piped.py, designed to fetch and expose the number of registered users from multiple Piped instances.
- Transitioned configuration management from settings.ini to config.yaml for improved flexibility and readability.
- Updated licensing information to include contributions from the Private.coffee Team, extending copyright to 2024.

This enhancement allows users to monitor the growth and usage of their Piped instances directly from their Prometheus setups, aligning with the community's move towards self-hosted, privacy-respecting services.
2024-05-23 11:50:25 +02:00

55 lines
1.9 KiB
Python
Executable file

from prometheus_client import start_http_server, Gauge
import urllib.request
import re
import time
import yaml
# Create a metric to track the number of registered users per domain
registered_users_gauge = Gauge(
"registered_users", "Number of registered users", ["domain"]
)
headers = {
"User-Agent": "Mozilla/5.0 (compatible; prometheus-piped/dev; +https://git.private.coffee/PrivateCoffee/prometheus-piped)",
}
def fetch_registered_users(domain):
try:
# Construct the full API URL
api_url = f"https://{domain}/registered/badge"
# Fetch the badge URL
req = urllib.request.Request(api_url, headers=headers)
with urllib.request.urlopen(req) as response:
final_url = response.geturl()
# Extract the number of registered users from the redirect URL
match = re.search(r"Registered%20Users-(\d+)-blue", final_url)
if match:
return int(match.group(1))
except Exception as e:
print(f"Error fetching registered users from {domain}: {e}")
return 0
def update_registered_users(domains, update_interval):
while True:
for domain in domains:
# Fetch the number of registered users and update the gauge
registered_users = fetch_registered_users(domain)
registered_users_gauge.labels(domain=domain).set(registered_users)
# Sleep for a while before fetching the data again
time.sleep(update_interval)
if __name__ == "__main__":
# Load configuration from YAML file
with open("config.yaml", "r") as file:
config = yaml.safe_load(file)
domains = config["piped"]
update_interval = config.get("update_interval", 60)
# Start up the server to expose the metrics
start_http_server(8098)
# Update the registered users gauge periodically
update_registered_users(domains, update_interval)