Skip to content

Instantly share code, notes, and snippets.

@marcus-crane
Created April 30, 2025 23:38
Show Gist options
  • Save marcus-crane/90bec324f9ad8bade71a6ce9384ebf54 to your computer and use it in GitHub Desktop.
Save marcus-crane/90bec324f9ad8bade71a6ce9384ebf54 to your computer and use it in GitHub Desktop.
A small Prometheus Exporter that exposes DownDetector statuses
import time
from bs4 import BeautifulSoup
from prometheus_client import start_http_server, Gauge
import requests
ga = Gauge('anomalies', 'A description', ['category', 'site'])
ga_categories = ['telecom', 'finance', 'online-services', 'social-media', 'gaming']
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'}
def poll_downdetector(gauge):
gauge.clear()
print("- Processing DownDetector")
for category in ga_categories:
url = f"https://downdetector.co.nz/{category}/"
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
for card in soup.find_all('div', class_='company-card'):
title = card.h5.text
for cl in card.svg.attrs.get('class'):
if cl == 'sparkline':
continue
if cl == 'zero':
gauge.labels(category, title).set(0)
continue
if cl == 'success':
gauge.labels(category, title).set(1)
continue
if cl == 'warning':
gauge.labels(category, title).set(2)
continue
if cl == 'danger':
gauge.labels(category, title).set(3)
continue
gauge.labels(category, title).set(-1)
print(f"-- Processed category {category}")
if __name__ == '__main__':
start_http_server(8000)
print("Listening on http://localhost:8000/metrics")
while True:
poll_downdetector(ga)
time.sleep(300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment