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 | 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(): |
| 32 | all[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
| 33 | return all |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 34 | |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame^] | 35 | @app.route("/devices/reachable") |
| 36 | def get_devices_reachable(): |
| 37 | global devices, lock |
| 38 | with lock: |
| 39 | reachable = {} |
| 40 | for _, device in devices.items(): |
| 41 | if device.reachable is True: |
| 42 | reachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
| 43 | return reachable |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 44 | |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame^] | 45 | @app.route("/devices/unreachable") |
| 46 | def get_devices_unreachable(): |
| 47 | global devices, lock |
| 48 | with lock: |
| 49 | unreachable = {} |
| 50 | for _, device in devices.items(): |
| 51 | if device.reachable is False: |
| 52 | unreachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)} |
| 53 | return unreachable |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 54 | |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 55 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 56 | |
| 57 | def build_argparser(): |
| 58 | parser = ArgumentParser(add_help=False) |
| 59 | args = parser.add_argument_group('Options') |
| 60 | args.add_argument('-h', '--help', |
| 61 | action='help', |
| 62 | default=SUPPRESS, |
| 63 | help='Show this help message and exit.') |
| 64 | args.add_argument("--user", |
| 65 | help="ROC username", |
| 66 | type=str) |
| 67 | args.add_argument("--password", |
| 68 | help="ROC password", |
| 69 | type=str) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 70 | args.add_argument("--token", |
| 71 | help="Rancher bearer token", |
| 72 | type=str) |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 73 | args.add_argument("--port", |
| 74 | help="Service port", |
| 75 | type=str, |
| 76 | default="3333") |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 77 | return parser |
| 78 | |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 79 | def update(roc, prom, old): |
| 80 | new = roc.update_devices(old) |
Shad Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 81 | if new is not None: |
| 82 | new = prom.update_devices(new) |
| 83 | else: |
| 84 | new = old |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 85 | return new |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 86 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 87 | def probe(devices): |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 88 | for imsi_id, device in devices.items(): |
| 89 | if device.ip is None: |
| 90 | continue |
| 91 | if ping(device.ip): |
| 92 | device.reachable = True |
| 93 | device.last_reachable = datetime.now() |
| 94 | log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip)) |
| 95 | else: |
| 96 | device.reachable = False |
| 97 | log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip)) |
| 98 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 99 | def work_thread(roc, prom): |
| 100 | global devices, lock |
| 101 | while True: |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 102 | new = update(roc, prom, devices) |
| 103 | probe(new) |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 104 | with lock: |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 105 | devices = new |
| 106 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 107 | time.sleep(5) |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 108 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 109 | if __name__ == '__main__': |
| 110 | |
| 111 | log.basicConfig( |
| 112 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 113 | level=log.DEBUG, |
| 114 | datefmt='%Y-%m-%d %H:%M:%S', |
| 115 | stream=sys.stdout) |
| 116 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 117 | log.info("Starting network-diag-app...") |
| 118 | |
| 119 | args = build_argparser().parse_args() |
| 120 | |
| 121 | roc = Roc(args.user, args.password) |
| 122 | prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1]) |
| 123 | |
| 124 | t = threading.Thread(target=work_thread, args=(roc, prom,)) |
| 125 | t.start() |
| 126 | |
| 127 | app.run('0.0.0.0', args.port) |