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