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 | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 6 | import re |
| 7 | from datetime import datetime |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 8 | import time |
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 |
| 18 | import device |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 19 | |
| 20 | app = Flask(__name__) |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 21 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 22 | devices = {} # dict imsi:device |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 23 | lock = threading.Lock() |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame^] | 24 | probe_start = threading.Event() |
| 25 | probe_stop = threading.Event() |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 26 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 27 | |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 28 | @app.route("/devices") |
| 29 | def get_devices(): |
| 30 | global devices, lock |
| 31 | with lock: |
| 32 | all = {} |
| 33 | for _, device in devices.items(): |
| 34 | all[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
| 35 | return all |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +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: |
| 44 | reachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
| 45 | return reachable |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +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: |
| 54 | unreachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
| 55 | return unreachable |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 56 | |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame^] | 57 | @app.route("/probe") |
| 58 | def probe(): |
| 59 | update_and_probe() |
| 60 | return get_devices_reachable() |
| 61 | |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 62 | @app.route("/config") |
| 63 | def config(): |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame^] | 64 | global args, probe_stop |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 65 | period = request.args.get('period') |
| 66 | if period is not None: |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame^] | 67 | period = int(period) |
| 68 | if period == 0: |
| 69 | log.info("Stopping probes...") |
| 70 | args.period = period |
| 71 | probe_stop.set() |
| 72 | else: |
| 73 | log.info("Starting probes...") |
| 74 | args.period = period |
| 75 | probe_start.set() |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 76 | config = vars(args) |
| 77 | config.pop('token', None) |
| 78 | config.pop('user', None) |
| 79 | config.pop('password', None) |
| 80 | return config |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 81 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 82 | |
| 83 | def build_argparser(): |
| 84 | parser = ArgumentParser(add_help=False) |
| 85 | args = parser.add_argument_group('Options') |
| 86 | args.add_argument('-h', '--help', |
| 87 | action='help', |
| 88 | default=SUPPRESS, |
| 89 | help='Show this help message and exit.') |
| 90 | args.add_argument("--user", |
| 91 | help="ROC username", |
| 92 | type=str) |
| 93 | args.add_argument("--password", |
| 94 | help="ROC password", |
| 95 | type=str) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 96 | args.add_argument("--token", |
| 97 | help="Rancher bearer token", |
| 98 | type=str) |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 99 | args.add_argument("--port", |
| 100 | help="Service port", |
| 101 | type=str, |
| 102 | default="3333") |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 103 | args.add_argument("--period", |
| 104 | help="Probing period in sec", |
| 105 | type=int, |
| 106 | default=180) |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 107 | return parser |
| 108 | |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 109 | def update(roc, prom, old): |
| 110 | new = roc.update_devices(old) |
Shad Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 111 | if new is not None: |
| 112 | new = prom.update_devices(new) |
| 113 | else: |
| 114 | new = old |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 115 | return new |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 116 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 117 | def probe(devices): |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 118 | for imsi_id, device in devices.items(): |
| 119 | if device.ip is None: |
| 120 | continue |
| 121 | if ping(device.ip): |
| 122 | device.reachable = True |
| 123 | device.last_reachable = datetime.now() |
| 124 | log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip)) |
| 125 | else: |
| 126 | device.reachable = False |
| 127 | log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip)) |
| 128 | |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame^] | 129 | def update_and_probe(): |
| 130 | global devices, lock |
| 131 | new = update(roc, prom, devices) |
| 132 | probe(new) |
| 133 | with lock: |
| 134 | devices = new |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 135 | |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame^] | 136 | def work_thread(roc, prom): |
| 137 | global args |
| 138 | while True: |
| 139 | probe_start.wait() |
| 140 | probe_start.clear() |
| 141 | log.info("Probing started") |
| 142 | while True: |
| 143 | update_and_probe() |
| 144 | if probe_stop.wait(timeout=args.period): |
| 145 | log.info("Probing stopped") |
| 146 | probe_stop.clear() |
| 147 | break |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 148 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 149 | if __name__ == '__main__': |
| 150 | |
| 151 | log.basicConfig( |
| 152 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 153 | level=log.DEBUG, |
| 154 | datefmt='%Y-%m-%d %H:%M:%S', |
| 155 | stream=sys.stdout) |
| 156 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 157 | log.info("Starting network-diag-app...") |
| 158 | |
| 159 | args = build_argparser().parse_args() |
| 160 | |
| 161 | roc = Roc(args.user, args.password) |
| 162 | prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1]) |
| 163 | |
| 164 | t = threading.Thread(target=work_thread, args=(roc, prom,)) |
| 165 | t.start() |
Shad Ansari | ce3c67b | 2022-02-10 21:00:25 +0000 | [diff] [blame^] | 166 | probe_start.set() |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 167 | |
| 168 | app.run('0.0.0.0', args.port) |