-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor.py
103 lines (87 loc) · 2.94 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import requests
from datetime import datetime
from flask import Flask, render_template, jsonify
app = Flask(__name__)
@app.route("/")
def perma_status(up="up!", message=""):
"""
Show the status page with charts
The required template is generated by index.py
"""
try:
with open(os.getenv('MAINTENANCE_FILE', 'maintenance.txt'), 'r') as f:
up = f.readline()
message = f.read()
except FileNotFoundError:
pass
try:
return render_template("index.html", up=up, message=message)
except Exception:
return jsonify({"error": "missing template"}), 500
def age(objects, now, f):
return (
now - datetime.strptime(
f(
[
o['creation_timestamp']
for o in objects if o['capture_time']
]
), "%Y-%m-%dT%H:%M:%S.%fZ"
)
).total_seconds()
@app.route("/monitor")
def perma_monitor():
""" hit the Perma API and get the last {limit} captures for analysis """
thresholds = {"unfinished": 25, "statistic": 0.9}
base = 'https://api.perma.cc/v1/public/archives'
limit = 50
offset = 0
url = f'{base}?limit={limit}&offset={offset}'
report = {"status": [], "messages": []}
try:
objects = requests.get(url).json()['objects']
except Exception as e:
report["status"].append("PROBLEM_PENDING")
report["messages"].append(f"API unavailable: {e}")
return jsonify(report=report)
pending = sum(
[
any([
"pending" in c['status']
for c in o['captures']
if not c['user_upload']
])
for o in objects
]
)
# what is the ratio of seconds from now to the last completed capture to
# the seconds from now to the oldest capture within {limit} captures ago?
now = datetime.utcnow()
if pending != limit:
try:
oldest = age(objects, now, min)
newest = age(objects, now, max)
statistic = newest / oldest
except Exception as e:
report["status"].append("PROBLEM_PENDING")
report["messages"].append(f"Couldn't get capture ages: {e}")
return jsonify(report=report)
if statistic != 1.0 and statistic > thresholds["statistic"]:
report["status"].append("PROBLEM_LOWUSAGE")
msg = f"statistic for time to last successful capture) is {statistic}"
report["messages"].append(msg)
else:
statistic = "n/a"
if pending > thresholds["unfinished"]:
report["status"].append("PROBLEM_PENDING")
msg = f"{pending} uncompleted captures in the last {limit}"
report["messages"].append(msg)
if not report["status"]:
report["status"] = ["OK"]
output = {
"unfinished": pending,
"statistic": statistic,
"report": report
}
return jsonify(**output)