Skip to content

Commit c2ea3b8

Browse files
committed
Add 'statuspage/' from commit '57d847adde7f27a9499db0215fef21e9647b034f'
git-subtree-dir: statuspage git-subtree-mainline: 459887a git-subtree-split: 57d847a
2 parents 459887a + 57d847a commit c2ea3b8

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

statuspage/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM python:3.7-alpine
2+
RUN pip install requests
3+
ADD statuspage.py statuspage.py
4+
CMD ["python", "statuspage.py"]

statuspage/statuspage.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)