blob: 8ebe8a92b6f8011381bca35081da26b1f46a1604 [file] [log] [blame]
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -07001#!/usr/bin/env python
2
3# Copyright 2020-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Andy Bavier614af142020-08-07 14:49:56 -070017import os
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -070018import time
Andy Bavier614af142020-08-07 14:49:56 -070019import datetime
20import pytz
21import threading
22from icalevents.icalevents import events
Andy Bavier4021a2f2020-07-29 12:39:47 -070023from flask import Flask, jsonify, abort, request, Response
24import prometheus_client as prom
25
Andy Bavier8a5c9872020-10-21 13:17:53 -070026# URL of maintenance calendar
Andy Bavier614af142020-08-07 14:49:56 -070027SECRET_ICAL_URL = os.environ.get("SECRET_ICAL_URL")
Andy Bavier8a5c9872020-10-21 13:17:53 -070028
29# Aether environment that the server is monitoring (e.g., "production")
30# To schedule downtime, postfix the cluster name with the env: "ace-tucson-production"
31AETHER_ENV = os.environ.get("AETHER_ENV", "production")
32
33# Move to "no result" status if we don't hear from agent for this many seconds
Andy Bavier4021a2f2020-07-29 12:39:47 -070034NO_RESULT_THRESHOLD = 720
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -070035
36app = Flask(__name__)
37edges = [
38 {
Andy Bavier8a5c9872020-10-21 13:17:53 -070039 'name': 'ace-example',
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -070040 'status': {
41 'control_plane': 'connected',
42 'user_plane': 'connected'
43 },
Andy Bavier614af142020-08-07 14:49:56 -070044 'last_update': time.time(),
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -070045 }
46]
47
Andy Bavier4021a2f2020-07-29 12:39:47 -070048status_codes = {
49 "no result": -2,
50 "error": -1,
51 "disconnected": 0,
52 "connecting": 1,
53 "connected": 2
54}
55
Andy Bavier614af142020-08-07 14:49:56 -070056room_mapping = {
Andy Bavier0423cbd2020-10-23 10:50:29 -070057 "ace-menlo-pixel-production": "(Compute)-MP-1-Aether Production",
58 "ace-menlo-staging": "(Compute)-MP-1-Aether Staging"
Andy Bavier614af142020-08-07 14:49:56 -070059}
60
Andy Bavier5b4e28f2021-03-09 15:48:20 -070061# Legacy test status metrics, reporting a status code between -2 and 2
Andy Bavier4021a2f2020-07-29 12:39:47 -070062cp_status = prom.Gauge("aetheredge_status_control_plane", "Control plane status code", ["name"])
63up_status = prom.Gauge("aetheredge_status_user_plane", "User plane status code", ["name"])
Andy Bavier5b4e28f2021-03-09 15:48:20 -070064
65# Simplified binary test result metrics
Andy Baviera0c40aa2021-03-10 12:09:12 -070066connect_test_ok = prom.Gauge("aetheredge_connect_test_ok", "Last connect test passed", ["name"])
67connect_test_down = prom.Gauge("aetheredge_connect_test_down", "Connect test not reporting", ["name"])
68ping_test_ok = prom.Gauge("aetheredge_ping_test_ok", "Last ping test passed", ["name"])
69ping_test_down = prom.Gauge("aetheredge_ping_test_down", "Ping test not reporting", ["name"])
Andy Bavier5b4e28f2021-03-09 15:48:20 -070070
71# Other metrics
Andy Bavier4021a2f2020-07-29 12:39:47 -070072last_update = prom.Gauge("aetheredge_last_update", "Last reported test result", ["name"])
Andy Bavier614af142020-08-07 14:49:56 -070073maint_window = prom.Gauge("aetheredge_in_maintenance_window", "Currently in a maintenance window", ["name"])
74
75def is_my_event(event, name):
76 for field in ["summary", "location", "description"]:
Andy Bavier8a5c9872020-10-21 13:17:53 -070077 fullname = name
78 if name.startswith("ace-"):
79 fullname = "%s-%s" % (name, AETHER_ENV)
80 if fullname in getattr(event, field, ""):
Andy Bavier614af142020-08-07 14:49:56 -070081 return True
Andy Bavier0423cbd2020-10-23 10:50:29 -070082 if fullname in room_mapping and room_mapping[fullname] in getattr(event, field, ""):
83 return True
Andy Bavier614af142020-08-07 14:49:56 -070084 return False
85
Andy Bavierc41cf0c2020-09-02 14:49:21 -070086def is_naive_datetime(d):
87 return d.tzinfo is None or d.tzinfo.utcoffset(d) is None
88
89def process_all_day_events(es):
90 for event in es:
91 if event.all_day:
92 # All day events have naive datetimes, which breaks comparisons
93 pacific = pytz.timezone('US/Pacific')
94 if is_naive_datetime(event.start):
95 event.start = pacific.localize(event.start)
96 if is_naive_datetime(event.end):
97 event.end = pacific.localize(event.end)
98
Andy Bavier614af142020-08-07 14:49:56 -070099def in_maintenance_window(events, name, now):
100 for event in events:
101 if event.start < now and event.end > now:
102 if is_my_event(event, name):
103 return True
Andy Bavier614af142020-08-07 14:49:56 -0700104 return False
105
106def pull_maintenance_events():
107 while(True):
108 now = datetime.datetime.now(pytz.utc)
109 try:
110 es = events(SECRET_ICAL_URL, start = now)
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700111 process_all_day_events(es)
Andy Bavier614af142020-08-07 14:49:56 -0700112 except Exception as e:
113 print(e)
114 else:
115 for edge in edges:
116 if 'maintenance' not in edge:
117 edge['maintenance'] = {}
118 edge['maintenance']['in_window'] = in_maintenance_window(es, edge['name'], now)
119 edge['maintenance']['last_update'] = time.time()
120 time.sleep(60)
Andy Bavier4021a2f2020-07-29 12:39:47 -0700121
122def time_out_stale_results():
123 for edge in edges:
124 time_elapsed = time.time() - edge["last_update"]
125 if time_elapsed > NO_RESULT_THRESHOLD:
126 edge['status']['control_plane'] = "no result"
127 edge['status']['user_plane'] = "no result"
128
Andy Baviere47157d2020-12-11 14:13:12 -0700129def remove_edge_from_metrics(name):
130 try:
131 cp_status.remove(name)
132 up_status.remove(name)
133 last_update.remove(name)
Andy Baviera0c40aa2021-03-10 12:09:12 -0700134 connect_test_ok.remove(name)
135 connect_test_down.remove(name)
136 ping_test_ok.remove(name)
137 ping_test_down.remove(name)
Andy Bavier5b4e28f2021-03-09 15:48:20 -0700138 except:
139 pass
140
141 try:
Andy Baviere47157d2020-12-11 14:13:12 -0700142 maint_window.remove(name)
143 except:
144 pass
Andy Bavier4021a2f2020-07-29 12:39:47 -0700145
146@app.route('/edges/metrics', methods=['GET'])
147def get_prometheus_metrics():
148 res = []
149 time_out_stale_results()
150 for edge in edges:
Andy Bavier8a5c9872020-10-21 13:17:53 -0700151 if edge['name'] == "ace-example":
Andy Bavier4021a2f2020-07-29 12:39:47 -0700152 continue
153
154 cp_status.labels(edge['name']).set(status_codes[edge['status']['control_plane']])
155 up_status.labels(edge['name']).set(status_codes[edge['status']['user_plane']])
156 last_update.labels(edge['name']).set(edge['last_update'])
Andy Bavier614af142020-08-07 14:49:56 -0700157 if 'maintenance' in edge:
158 maint_window.labels(edge['name']).set(int(edge['maintenance']['in_window']))
Andy Bavier4021a2f2020-07-29 12:39:47 -0700159
Andy Baviera0c40aa2021-03-10 12:09:12 -0700160 connect_test_ok.labels(edge['name']).set(0)
161 connect_test_down.labels(edge['name']).set(0)
162 if edge['status']['control_plane'] == "connected":
163 connect_test_ok.labels(edge['name']).set(1)
164 if edge['status']['control_plane'] in ["error", "no result"]:
165 connect_test_down.labels(edge['name']).set(1)
166
167 ping_test_ok.labels(edge['name']).set(0)
168 ping_test_down.labels(edge['name']).set(0)
169 if edge['status']['user_plane'] == "connected":
170 ping_test_ok.labels(edge['name']).set(1)
171 if edge['status']['user_plane'] in ["error", "no result"]:
172 ping_test_down.labels(edge['name']).set(1)
Andy Bavier5b4e28f2021-03-09 15:48:20 -0700173
Andy Bavier4021a2f2020-07-29 12:39:47 -0700174 res.append(prom.generate_latest(cp_status))
175 res.append(prom.generate_latest(up_status))
176 res.append(prom.generate_latest(last_update))
Andy Bavier614af142020-08-07 14:49:56 -0700177 res.append(prom.generate_latest(maint_window))
Andy Baviera0c40aa2021-03-10 12:09:12 -0700178 res.append(prom.generate_latest(connect_test_ok))
179 res.append(prom.generate_latest(connect_test_down))
180 res.append(prom.generate_latest(ping_test_ok))
181 res.append(prom.generate_latest(ping_test_down))
Andy Bavier614af142020-08-07 14:49:56 -0700182
Andy Bavier4021a2f2020-07-29 12:39:47 -0700183 return Response(res, mimetype="text/plain")
184
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700185
186@app.route('/edges/healthz', methods=['GET'])
187def get_health():
188 return {'message': 'healthy'}
189
190
191@app.route('/edges', methods=['GET'])
192def get_edges():
Andy Bavier4021a2f2020-07-29 12:39:47 -0700193 time_out_stale_results()
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700194 return jsonify({'edges': edges})
195
196
197@app.route('/edges/<string:name>', methods=['GET'])
198def get_edge(name):
Andy Bavier4021a2f2020-07-29 12:39:47 -0700199 time_out_stale_results()
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700200 edge = [edge for edge in edges if edge['name'] == name]
201 if len(edge) == 0:
202 abort(404)
203 return jsonify({'edge': edge[0]})
204
205
206@app.route('/edges', methods=['POST'])
207def create_or_update_edge():
208 if not request.json:
209 abort(400)
210 if 'name' not in request.json:
211 abort(400)
212 if 'status' not in request.json:
213 abort(400)
214
215 req_edge = {
216 'name': request.json['name'],
217 'status': {
218 'control_plane': request.json['status']['control_plane'],
219 'user_plane': request.json['status']['user_plane']
220 },
221 'last_update': time.time()
222 }
223
224 edge = [edge for edge in edges if edge['name'] == req_edge['name']]
225 if len(edge) == 0:
226 print("new edge request " + req_edge['name'])
227 edges.append(req_edge)
228 else:
229 edge[0]['status']['control_plane'] = req_edge['status']['control_plane']
230 edge[0]['status']['user_plane'] = req_edge['status']['user_plane']
231 edge[0]['last_update'] = req_edge['last_update']
232
233 return jsonify({'edge': req_edge}), 201
234
235
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700236@app.route('/edges/<string:name>', methods=['DELETE'])
237def delete_edge(name):
238 print("delete edge request " + name)
239 result = False
240 for i in range(len(edges)):
241 if edges[i]['name'] == name:
242 del edges[i]
Andy Baviere47157d2020-12-11 14:13:12 -0700243 remove_edge_from_metrics(name)
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700244 result = True
245 break
246 if not result:
247 abort(404)
248 return jsonify({'result': True})
249
250
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700251if __name__ == '__main__':
Andy Bavier8a5c9872020-10-21 13:17:53 -0700252 if SECRET_ICAL_URL and AETHER_ENV:
253 print(" * Starting maintenance calendar polling thread (Aether env: %s)" % AETHER_ENV)
Andy Bavier614af142020-08-07 14:49:56 -0700254 t = threading.Thread(target=pull_maintenance_events)
255 t.start()
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700256 app.run(debug=True, host='0.0.0.0', port=80)