blob: aa9de6625f439a5983ce52ca0469b3f359ff1cce [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 Bavier4021a2f2020-07-29 12:39:47 -070061cp_status = prom.Gauge("aetheredge_status_control_plane", "Control plane status code", ["name"])
62up_status = prom.Gauge("aetheredge_status_user_plane", "User plane status code", ["name"])
63last_update = prom.Gauge("aetheredge_last_update", "Last reported test result", ["name"])
Andy Bavier614af142020-08-07 14:49:56 -070064maint_window = prom.Gauge("aetheredge_in_maintenance_window", "Currently in a maintenance window", ["name"])
65
66def is_my_event(event, name):
67 for field in ["summary", "location", "description"]:
Andy Bavier8a5c9872020-10-21 13:17:53 -070068 fullname = name
69 if name.startswith("ace-"):
70 fullname = "%s-%s" % (name, AETHER_ENV)
71 if fullname in getattr(event, field, ""):
Andy Bavier614af142020-08-07 14:49:56 -070072 return True
Andy Bavier0423cbd2020-10-23 10:50:29 -070073 if fullname in room_mapping and room_mapping[fullname] in getattr(event, field, ""):
74 return True
Andy Bavier614af142020-08-07 14:49:56 -070075 return False
76
Andy Bavierc41cf0c2020-09-02 14:49:21 -070077def is_naive_datetime(d):
78 return d.tzinfo is None or d.tzinfo.utcoffset(d) is None
79
80def process_all_day_events(es):
81 for event in es:
82 if event.all_day:
83 # All day events have naive datetimes, which breaks comparisons
84 pacific = pytz.timezone('US/Pacific')
85 if is_naive_datetime(event.start):
86 event.start = pacific.localize(event.start)
87 if is_naive_datetime(event.end):
88 event.end = pacific.localize(event.end)
89
Andy Bavier614af142020-08-07 14:49:56 -070090def in_maintenance_window(events, name, now):
91 for event in events:
92 if event.start < now and event.end > now:
93 if is_my_event(event, name):
94 return True
Andy Bavier614af142020-08-07 14:49:56 -070095 return False
96
97def pull_maintenance_events():
98 while(True):
99 now = datetime.datetime.now(pytz.utc)
100 try:
101 es = events(SECRET_ICAL_URL, start = now)
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700102 process_all_day_events(es)
Andy Bavier614af142020-08-07 14:49:56 -0700103 except Exception as e:
104 print(e)
105 else:
106 for edge in edges:
107 if 'maintenance' not in edge:
108 edge['maintenance'] = {}
109 edge['maintenance']['in_window'] = in_maintenance_window(es, edge['name'], now)
110 edge['maintenance']['last_update'] = time.time()
111 time.sleep(60)
Andy Bavier4021a2f2020-07-29 12:39:47 -0700112
113def time_out_stale_results():
114 for edge in edges:
115 time_elapsed = time.time() - edge["last_update"]
116 if time_elapsed > NO_RESULT_THRESHOLD:
117 edge['status']['control_plane'] = "no result"
118 edge['status']['user_plane'] = "no result"
119
Andy Baviere47157d2020-12-11 14:13:12 -0700120def remove_edge_from_metrics(name):
121 try:
122 cp_status.remove(name)
123 up_status.remove(name)
124 last_update.remove(name)
125 except:
126 pass
127
128 try:
129 maint_window.remove(name)
130 except:
131 pass
Andy Bavier4021a2f2020-07-29 12:39:47 -0700132
133@app.route('/edges/metrics', methods=['GET'])
134def get_prometheus_metrics():
135 res = []
136 time_out_stale_results()
137 for edge in edges:
Andy Bavier8a5c9872020-10-21 13:17:53 -0700138 if edge['name'] == "ace-example":
Andy Bavier4021a2f2020-07-29 12:39:47 -0700139 continue
140
141 cp_status.labels(edge['name']).set(status_codes[edge['status']['control_plane']])
142 up_status.labels(edge['name']).set(status_codes[edge['status']['user_plane']])
143 last_update.labels(edge['name']).set(edge['last_update'])
Andy Bavier614af142020-08-07 14:49:56 -0700144 if 'maintenance' in edge:
145 maint_window.labels(edge['name']).set(int(edge['maintenance']['in_window']))
Andy Bavier4021a2f2020-07-29 12:39:47 -0700146
147 res.append(prom.generate_latest(cp_status))
148 res.append(prom.generate_latest(up_status))
149 res.append(prom.generate_latest(last_update))
Andy Bavier614af142020-08-07 14:49:56 -0700150 res.append(prom.generate_latest(maint_window))
151
Andy Bavier4021a2f2020-07-29 12:39:47 -0700152 return Response(res, mimetype="text/plain")
153
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700154
155@app.route('/edges/healthz', methods=['GET'])
156def get_health():
157 return {'message': 'healthy'}
158
159
160@app.route('/edges', methods=['GET'])
161def get_edges():
Andy Bavier4021a2f2020-07-29 12:39:47 -0700162 time_out_stale_results()
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700163 return jsonify({'edges': edges})
164
165
166@app.route('/edges/<string:name>', methods=['GET'])
167def get_edge(name):
Andy Bavier4021a2f2020-07-29 12:39:47 -0700168 time_out_stale_results()
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700169 edge = [edge for edge in edges if edge['name'] == name]
170 if len(edge) == 0:
171 abort(404)
172 return jsonify({'edge': edge[0]})
173
174
175@app.route('/edges', methods=['POST'])
176def create_or_update_edge():
177 if not request.json:
178 abort(400)
179 if 'name' not in request.json:
180 abort(400)
181 if 'status' not in request.json:
182 abort(400)
183
184 req_edge = {
185 'name': request.json['name'],
186 'status': {
187 'control_plane': request.json['status']['control_plane'],
188 'user_plane': request.json['status']['user_plane']
189 },
190 'last_update': time.time()
191 }
192
193 edge = [edge for edge in edges if edge['name'] == req_edge['name']]
194 if len(edge) == 0:
195 print("new edge request " + req_edge['name'])
196 edges.append(req_edge)
197 else:
198 edge[0]['status']['control_plane'] = req_edge['status']['control_plane']
199 edge[0]['status']['user_plane'] = req_edge['status']['user_plane']
200 edge[0]['last_update'] = req_edge['last_update']
201
202 return jsonify({'edge': req_edge}), 201
203
204
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700205@app.route('/edges/<string:name>', methods=['DELETE'])
206def delete_edge(name):
207 print("delete edge request " + name)
208 result = False
209 for i in range(len(edges)):
210 if edges[i]['name'] == name:
211 del edges[i]
Andy Baviere47157d2020-12-11 14:13:12 -0700212 remove_edge_from_metrics(name)
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700213 result = True
214 break
215 if not result:
216 abort(404)
217 return jsonify({'result': True})
218
219
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700220if __name__ == '__main__':
Andy Bavier8a5c9872020-10-21 13:17:53 -0700221 if SECRET_ICAL_URL and AETHER_ENV:
222 print(" * Starting maintenance calendar polling thread (Aether env: %s)" % AETHER_ENV)
Andy Bavier614af142020-08-07 14:49:56 -0700223 t = threading.Thread(target=pull_maintenance_events)
224 t.start()
Hyunsun Moonf32ae9a2020-05-28 13:17:45 -0700225 app.run(debug=True, host='0.0.0.0', port=80)