Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 1 | """ |
Shad Ansari | b046c15 | 2022-06-07 14:34:14 -0700 | [diff] [blame] | 2 | SPDX-FileCopyrightText: 2022-present Intel Corporation |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 3 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
Shad Ansari | b046c15 | 2022-06-07 14:34:14 -0700 | [diff] [blame] | 4 | SPDX-License-Identifier: Apache-2.0 |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 5 | """ |
| 6 | import sys |
Shad Ansari | 115b3f9 | 2022-03-28 19:40:01 +0000 | [diff] [blame] | 7 | import os |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 8 | from datetime import datetime |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 9 | |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 10 | from flask import Flask, request |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 11 | import logging as log |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 12 | from argparse import ArgumentParser, SUPPRESS |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 13 | import threading |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 14 | |
| 15 | from roc import Roc |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 16 | from prom import Prometheus |
| 17 | from ping import ping |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 18 | |
| 19 | app = Flask(__name__) |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 20 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 21 | devices = {} # dict imsi:device |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 22 | lock = threading.Lock() |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame] | 23 | probe_start = threading.Event() |
| 24 | probe_stop = threading.Event() |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 25 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 26 | |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 27 | @app.route("/devices") |
| 28 | def get_devices(): |
| 29 | global devices, lock |
| 30 | with lock: |
| 31 | all = {} |
| 32 | for _, device in devices.items(): |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 33 | all[device.imsi_id] = {'ip': device.ip, 'imsi': device.imsi, 'last_reachable': '{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 34 | return all |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 35 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 36 | |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 37 | @app.route("/devices/reachable") |
| 38 | def get_devices_reachable(): |
| 39 | global devices, lock |
| 40 | with lock: |
| 41 | reachable = {} |
| 42 | for _, device in devices.items(): |
| 43 | if device.reachable is True: |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 44 | reachable[device.imsi_id] = {'ip': device.ip, 'imsi': device.imsi, 'last_reachable': '{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 45 | return reachable |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 46 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 47 | |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 48 | @app.route("/devices/unreachable") |
| 49 | def get_devices_unreachable(): |
| 50 | global devices, lock |
| 51 | with lock: |
| 52 | unreachable = {} |
| 53 | for _, device in devices.items(): |
| 54 | if device.reachable is False: |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 55 | unreachable[device.imsi_id] = {'ip': device.ip, 'imsi': device.imsi, 'last_reachable': '{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 56 | return unreachable |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 57 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 58 | |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame] | 59 | @app.route("/probe") |
| 60 | def probe(): |
| 61 | update_and_probe() |
| 62 | return get_devices_reachable() |
| 63 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 64 | |
| 65 | # curl http://localhost:3333/config?period=0 |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 66 | @app.route("/config") |
| 67 | def config(): |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame] | 68 | global args, probe_stop |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 69 | period = request.args.get('period') |
| 70 | if period is not None: |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame] | 71 | period = int(period) |
| 72 | if period == 0: |
| 73 | log.info("Stopping probes...") |
| 74 | args.period = period |
| 75 | probe_stop.set() |
| 76 | else: |
| 77 | log.info("Starting probes...") |
| 78 | args.period = period |
| 79 | probe_start.set() |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 80 | config = vars(args) |
| 81 | config.pop('token', None) |
| 82 | config.pop('user', None) |
| 83 | config.pop('password', None) |
| 84 | return config |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 85 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 86 | |
| 87 | def build_argparser(): |
| 88 | parser = ArgumentParser(add_help=False) |
| 89 | args = parser.add_argument_group('Options') |
| 90 | args.add_argument('-h', '--help', |
| 91 | action='help', |
| 92 | default=SUPPRESS, |
| 93 | help='Show this help message and exit.') |
| 94 | args.add_argument("--user", |
| 95 | help="ROC username", |
| 96 | type=str) |
| 97 | args.add_argument("--password", |
| 98 | help="ROC password", |
| 99 | type=str) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 100 | args.add_argument("--token", |
| 101 | help="Rancher bearer token", |
| 102 | type=str) |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 103 | args.add_argument("--port", |
| 104 | help="Service port", |
| 105 | type=str, |
| 106 | default="3333") |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 107 | args.add_argument("--period", |
| 108 | help="Probing period in sec", |
| 109 | type=int, |
| 110 | default=180) |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 111 | args.add_argument("--url", |
| 112 | help="ROC url", |
| 113 | type=str, |
| 114 | default="https://roc.menlo.aetherproject.org/aether-roc-api/aether/v2.0.0/connectivity-service-v2/") |
| 115 | args.add_argument("--enterprise", |
| 116 | help="Enterprise Id", |
| 117 | type=str, |
| 118 | default="aether-onf") |
| 119 | args.add_argument("--site", |
| 120 | help="Site Id", |
| 121 | type=str, |
| 122 | default="menlo-4g") |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 123 | return parser |
| 124 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 125 | |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 126 | def update(roc, prom, old): |
| 127 | new = roc.update_devices(old) |
Shad Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 128 | if new is not None: |
| 129 | new = prom.update_devices(new) |
| 130 | else: |
| 131 | new = old |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 132 | return new |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 133 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 134 | |
| 135 | def do_probe(devices): |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 136 | for imsi_id, device in devices.items(): |
| 137 | if device.ip is None: |
| 138 | continue |
| 139 | if ping(device.ip): |
| 140 | device.reachable = True |
| 141 | device.last_reachable = datetime.now() |
| 142 | log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip)) |
| 143 | else: |
| 144 | device.reachable = False |
| 145 | log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip)) |
| 146 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 147 | |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame] | 148 | def update_and_probe(): |
| 149 | global devices, lock |
| 150 | new = update(roc, prom, devices) |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 151 | do_probe(new) |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame] | 152 | with lock: |
| 153 | devices = new |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 154 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 155 | |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame] | 156 | def work_thread(roc, prom): |
| 157 | global args |
| 158 | while True: |
| 159 | probe_start.wait() |
| 160 | probe_start.clear() |
| 161 | log.info("Probing started") |
| 162 | while True: |
| 163 | update_and_probe() |
| 164 | if probe_stop.wait(timeout=args.period): |
| 165 | log.info("Probing stopped") |
| 166 | probe_stop.clear() |
| 167 | break |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 168 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 169 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 170 | if __name__ == '__main__': |
| 171 | |
| 172 | log.basicConfig( |
| 173 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 174 | level=log.DEBUG, |
| 175 | datefmt='%Y-%m-%d %H:%M:%S', |
| 176 | stream=sys.stdout) |
| 177 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 178 | log.info("Starting network-diag-app...") |
| 179 | |
| 180 | args = build_argparser().parse_args() |
| 181 | |
Shad Ansari | 115b3f9 | 2022-03-28 19:40:01 +0000 | [diff] [blame] | 182 | if not args.user: |
| 183 | args.user = os.environ.get('ROCUSER') |
| 184 | if not args.password: |
| 185 | args.password = os.environ.get('ROCPASSWORD') |
| 186 | if not args.token: |
| 187 | args.token= os.environ.get('KEYCLOAKTOKEN') |
| 188 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 189 | roc = Roc(args.url, args.user, args.password, args.enterprise, args.site) |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 190 | prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1]) |
| 191 | |
| 192 | t = threading.Thread(target=work_thread, args=(roc, prom,)) |
| 193 | t.start() |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame] | 194 | probe_start.set() |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 195 | |
| 196 | app.run('0.0.0.0', args.port) |