Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 1 | #!/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 Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 17 | import os |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 18 | import time |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 19 | import datetime |
| 20 | import pytz |
| 21 | import threading |
| 22 | from icalevents.icalevents import events |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 23 | from flask import Flask, jsonify, abort, request, Response |
| 24 | import prometheus_client as prom |
| 25 | |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 26 | SECRET_ICAL_URL = os.environ.get("SECRET_ICAL_URL") |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 27 | NO_RESULT_THRESHOLD = 720 |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 28 | |
| 29 | app = Flask(__name__) |
| 30 | edges = [ |
| 31 | { |
| 32 | 'name': 'production-edge-example', |
| 33 | 'status': { |
| 34 | 'control_plane': 'connected', |
| 35 | 'user_plane': 'connected' |
| 36 | }, |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 37 | 'last_update': time.time(), |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 38 | } |
| 39 | ] |
| 40 | |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 41 | status_codes = { |
| 42 | "no result": -2, |
| 43 | "error": -1, |
| 44 | "disconnected": 0, |
| 45 | "connecting": 1, |
| 46 | "connected": 2 |
| 47 | } |
| 48 | |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 49 | room_mapping = { |
| 50 | "production-edge-onf-menlo": "(Compute)-MP-1-Aether Production", |
| 51 | "production-edge-example": "(Compute)-MP-1-Aether Production" # for testing |
| 52 | } |
| 53 | |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 54 | cp_status = prom.Gauge("aetheredge_status_control_plane", "Control plane status code", ["name"]) |
| 55 | up_status = prom.Gauge("aetheredge_status_user_plane", "User plane status code", ["name"]) |
| 56 | last_update = prom.Gauge("aetheredge_last_update", "Last reported test result", ["name"]) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 57 | maint_window = prom.Gauge("aetheredge_in_maintenance_window", "Currently in a maintenance window", ["name"]) |
| 58 | |
| 59 | def is_my_event(event, name): |
| 60 | for field in ["summary", "location", "description"]: |
| 61 | if name in getattr(event, field, ""): |
| 62 | return True |
| 63 | return False |
| 64 | |
| 65 | def in_maintenance_window(events, name, now): |
| 66 | for event in events: |
| 67 | if event.start < now and event.end > now: |
| 68 | if is_my_event(event, name): |
| 69 | return True |
| 70 | if name in room_mapping and is_my_event(event, room_mapping[name]): |
| 71 | return True |
| 72 | return False |
| 73 | |
| 74 | def pull_maintenance_events(): |
| 75 | while(True): |
| 76 | now = datetime.datetime.now(pytz.utc) |
| 77 | try: |
| 78 | es = events(SECRET_ICAL_URL, start = now) |
| 79 | except Exception as e: |
| 80 | print(e) |
| 81 | else: |
| 82 | for edge in edges: |
| 83 | if 'maintenance' not in edge: |
| 84 | edge['maintenance'] = {} |
| 85 | edge['maintenance']['in_window'] = in_maintenance_window(es, edge['name'], now) |
| 86 | edge['maintenance']['last_update'] = time.time() |
| 87 | time.sleep(60) |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 88 | |
| 89 | def time_out_stale_results(): |
| 90 | for edge in edges: |
| 91 | time_elapsed = time.time() - edge["last_update"] |
| 92 | if time_elapsed > NO_RESULT_THRESHOLD: |
| 93 | edge['status']['control_plane'] = "no result" |
| 94 | edge['status']['user_plane'] = "no result" |
| 95 | |
| 96 | |
| 97 | @app.route('/edges/metrics', methods=['GET']) |
| 98 | def get_prometheus_metrics(): |
| 99 | res = [] |
| 100 | time_out_stale_results() |
| 101 | for edge in edges: |
| 102 | if edge['name'] == "production-edge-example": |
| 103 | continue |
| 104 | |
| 105 | cp_status.labels(edge['name']).set(status_codes[edge['status']['control_plane']]) |
| 106 | up_status.labels(edge['name']).set(status_codes[edge['status']['user_plane']]) |
| 107 | last_update.labels(edge['name']).set(edge['last_update']) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 108 | if 'maintenance' in edge: |
| 109 | maint_window.labels(edge['name']).set(int(edge['maintenance']['in_window'])) |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 110 | |
| 111 | res.append(prom.generate_latest(cp_status)) |
| 112 | res.append(prom.generate_latest(up_status)) |
| 113 | res.append(prom.generate_latest(last_update)) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 114 | res.append(prom.generate_latest(maint_window)) |
| 115 | |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 116 | return Response(res, mimetype="text/plain") |
| 117 | |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 118 | |
| 119 | @app.route('/edges/healthz', methods=['GET']) |
| 120 | def get_health(): |
| 121 | return {'message': 'healthy'} |
| 122 | |
| 123 | |
| 124 | @app.route('/edges', methods=['GET']) |
| 125 | def get_edges(): |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 126 | time_out_stale_results() |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 127 | return jsonify({'edges': edges}) |
| 128 | |
| 129 | |
| 130 | @app.route('/edges/<string:name>', methods=['GET']) |
| 131 | def get_edge(name): |
Andy Bavier | 4021a2f | 2020-07-29 12:39:47 -0700 | [diff] [blame] | 132 | time_out_stale_results() |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 133 | edge = [edge for edge in edges if edge['name'] == name] |
| 134 | if len(edge) == 0: |
| 135 | abort(404) |
| 136 | return jsonify({'edge': edge[0]}) |
| 137 | |
| 138 | |
| 139 | @app.route('/edges', methods=['POST']) |
| 140 | def create_or_update_edge(): |
| 141 | if not request.json: |
| 142 | abort(400) |
| 143 | if 'name' not in request.json: |
| 144 | abort(400) |
| 145 | if 'status' not in request.json: |
| 146 | abort(400) |
| 147 | |
| 148 | req_edge = { |
| 149 | 'name': request.json['name'], |
| 150 | 'status': { |
| 151 | 'control_plane': request.json['status']['control_plane'], |
| 152 | 'user_plane': request.json['status']['user_plane'] |
| 153 | }, |
| 154 | 'last_update': time.time() |
| 155 | } |
| 156 | |
| 157 | edge = [edge for edge in edges if edge['name'] == req_edge['name']] |
| 158 | if len(edge) == 0: |
| 159 | print("new edge request " + req_edge['name']) |
| 160 | edges.append(req_edge) |
| 161 | else: |
| 162 | edge[0]['status']['control_plane'] = req_edge['status']['control_plane'] |
| 163 | edge[0]['status']['user_plane'] = req_edge['status']['user_plane'] |
| 164 | edge[0]['last_update'] = req_edge['last_update'] |
| 165 | |
| 166 | return jsonify({'edge': req_edge}), 201 |
| 167 | |
| 168 | |
| 169 | if __name__ == '__main__': |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame^] | 170 | if SECRET_ICAL_URL: |
| 171 | print(" * Starting maintenance calendar polling thread") |
| 172 | t = threading.Thread(target=pull_maintenance_events) |
| 173 | t.start() |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 174 | app.run(debug=True, host='0.0.0.0', port=80) |