Files
upstream-zulip/puppet/kandra/files/statuspage-pusher
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

81 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
import configparser
import http.client
import json
import logging
import time
from urllib.parse import urlencode
def fetch_metric(metric_id: str, query: str) -> float:
logging.info("Fetching %s", metric_id)
params = urlencode({"query": query})
conn = http.client.HTTPConnection("localhost:9090")
conn.request("GET", "/api/v1/query?" + params)
response = conn.getresponse()
if response.status != 200:
raise Exception(
f"Failed to request {metric_id} from Prometheus: {response.status} {response.reason}"
)
raw_bytes = response.read()
conn.close()
data = json.loads(raw_bytes)
if data["status"] != "success":
raise Exception(f"Failed to request {metric_id}: {raw_bytes.decode()}")
return float(data["data"]["result"][0]["value"][1])
def push_metric(metric_id: str, metric_value: float, page_id: str, oauth_token: str) -> None:
conn = http.client.HTTPSConnection("api.statuspage.io")
params = json.dumps({"data": {"timestamp": time.time(), "value": metric_value}})
headers = {"Content-Type": "application/json", "Authorization": "OAuth " + oauth_token}
conn.request(
"POST", "/v1/pages/" + page_id + "/metrics/" + metric_id + "/data.json", params, headers
)
response = conn.getresponse()
if not (response.status >= 200 and response.status < 300):
raise Exception(
f"Failed to push {metric_id} to statuspage.io: {response.status} {response.reason}"
)
logging.info("Wrote %s: %s", metric_id, response.read().decode())
conn.close()
def update_metric(metric_id: str, query: str, page_id: str, oauth_token: str) -> None:
metric_value = fetch_metric(metric_id, query)
push_metric(metric_id, metric_value, page_id, oauth_token)
def main() -> None:
logging.basicConfig(format="%(asctime)s statuspage-pusher: %(message)s", level=logging.INFO)
secrets_file = configparser.RawConfigParser()
secrets_file.read("/etc/zulip/zulip-secrets.conf")
oauth_token = secrets_file.get("secrets", "statuspage_token")
if oauth_token is None:
raise RuntimeError("statuspage_token secret is required")
config_file = configparser.RawConfigParser()
config_file.read("/etc/zulip/zulip.conf")
page_id = config_file.get("statuspage", "page_id")
if page_id is None:
raise RuntimeError("statuspage.page_id in zulip.conf is required")
metrics_file = configparser.RawConfigParser()
metrics_file.read("/etc/zulip/statuspage.conf")
metrics = metrics_file["metrics"]
while True:
for metric_id, query in metrics.items():
try:
update_metric(metric_id, query, page_id, oauth_token)
except Exception:
logging.exception("Error while updating metric")
time.sleep(30)
if __name__ == "__main__":
main()