|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +import json |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import time |
| 8 | + |
| 9 | +import requests |
| 10 | + |
| 11 | +api_key = os.environ['STATUSPAGE_API_KEY'] |
| 12 | +page_id = os.environ['STATUSPAGE_PAGE_ID'] |
| 13 | +metric_id = os.environ['STATUSPAGE_METRIC_ID'] |
| 14 | +api_base = 'api.statuspage.io' |
| 15 | + |
| 16 | +github_id = os.environ['GITHUB_OAUTH_KEY'] |
| 17 | +github_secret = os.environ['GITHUB_OAUTH_SECRET'] |
| 18 | + |
| 19 | + |
| 20 | +def get_rate_limit(): |
| 21 | + """Retrieve the current GitHub rate limit for our auth tokens""" |
| 22 | + r = requests.get( |
| 23 | + 'https://api.github.com/rate_limit', |
| 24 | + auth=(github_id, github_secret) |
| 25 | + ) |
| 26 | + r.raise_for_status() |
| 27 | + resp = r.json() |
| 28 | + return resp['resources']['core'] |
| 29 | + |
| 30 | + |
| 31 | +def post_data(limit, remaining, **ignore): |
| 32 | + """Send the percent-remaining GitHub rate limit to statuspage""" |
| 33 | + percent = 100 * remaining / limit |
| 34 | + now = int(datetime.utcnow().timestamp()) |
| 35 | + url = "https://api.statuspage.io/v1/pages/{page_id}/metrics/{metric_id}/data.json".format( |
| 36 | + page_id=page_id, metric_id=metric_id, |
| 37 | + ) |
| 38 | + |
| 39 | + r = requests.post(url, |
| 40 | + headers={ |
| 41 | + "Content-Type": "application/x-www-form-urlencoded", |
| 42 | + "Authorization": "OAuth " + api_key, |
| 43 | + }, |
| 44 | + data={ |
| 45 | + 'data[timestamp]': now, |
| 46 | + 'data[value]': percent, |
| 47 | + } |
| 48 | + ) |
| 49 | + r.raise_for_status() |
| 50 | + |
| 51 | + |
| 52 | +def get_and_post(): |
| 53 | + data = get_rate_limit() |
| 54 | + print(json.dumps(data)) |
| 55 | + post_data(limit=data['limit'], remaining=data['remaining']) |
| 56 | + |
| 57 | + |
| 58 | +while True: |
| 59 | + try: |
| 60 | + get_and_post() |
| 61 | + except Exception as e: |
| 62 | + print("Error: %s" % e, file=sys.stderr) |
| 63 | + # post every two minutes |
| 64 | + time.sleep(120) |
0 commit comments