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 |
| 11 | from flask_restful import Resource, Api |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 12 | import logging as log |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 13 | from argparse import ArgumentParser, SUPPRESS |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 14 | import threading |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 15 | |
| 16 | from roc import Roc |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 17 | from prom import Prometheus |
| 18 | from ping import ping |
| 19 | import device |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 20 | |
| 21 | app = Flask(__name__) |
| 22 | api = Api(app) |
| 23 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 24 | devices = {} # dict imsi:device |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 25 | lock = threading.Lock() |
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 | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 28 | class Devices(Resource): |
| 29 | def get(self): |
| 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 | |
| 37 | class ReachableDevices(Resource): |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 38 | def get(self): |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 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 | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 47 | class UnreachableDevices(Resource): |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 48 | def get(self): |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 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 | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 57 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 58 | |
| 59 | def build_argparser(): |
| 60 | parser = ArgumentParser(add_help=False) |
| 61 | args = parser.add_argument_group('Options') |
| 62 | args.add_argument('-h', '--help', |
| 63 | action='help', |
| 64 | default=SUPPRESS, |
| 65 | help='Show this help message and exit.') |
| 66 | args.add_argument("--user", |
| 67 | help="ROC username", |
| 68 | type=str) |
| 69 | args.add_argument("--password", |
| 70 | help="ROC password", |
| 71 | type=str) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 72 | args.add_argument("--token", |
| 73 | help="Rancher bearer token", |
| 74 | type=str) |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 75 | args.add_argument("--port", |
| 76 | help="Service port", |
| 77 | type=str, |
| 78 | default="3333") |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 79 | return parser |
| 80 | |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 81 | def update(roc, prom, old): |
| 82 | new = roc.update_devices(old) |
| 83 | prom.update_devices(new) |
| 84 | return new |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 85 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 86 | def probe(devices): |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 87 | for imsi_id, device in devices.items(): |
| 88 | if device.ip is None: |
| 89 | continue |
| 90 | if ping(device.ip): |
| 91 | device.reachable = True |
| 92 | device.last_reachable = datetime.now() |
| 93 | log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip)) |
| 94 | else: |
| 95 | device.reachable = False |
| 96 | log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip)) |
| 97 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 98 | def work_thread(roc, prom): |
| 99 | global devices, lock |
| 100 | while True: |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 101 | new = update(roc, prom, devices) |
| 102 | probe(new) |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 103 | with lock: |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 104 | devices = new |
| 105 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 106 | time.sleep(5) |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 107 | |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 108 | if __name__ == '__main__': |
| 109 | |
| 110 | log.basicConfig( |
| 111 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 112 | level=log.DEBUG, |
| 113 | datefmt='%Y-%m-%d %H:%M:%S', |
| 114 | stream=sys.stdout) |
| 115 | |
| 116 | api.add_resource(Devices, '/devices') |
| 117 | api.add_resource(ReachableDevices, '/devices/reachable') |
| 118 | api.add_resource(UnreachableDevices, '/devices/unreachable') |
| 119 | |
| 120 | log.info("Starting network-diag-app...") |
| 121 | |
| 122 | args = build_argparser().parse_args() |
| 123 | |
| 124 | roc = Roc(args.user, args.password) |
| 125 | prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1]) |
| 126 | |
| 127 | t = threading.Thread(target=work_thread, args=(roc, prom,)) |
| 128 | t.start() |
| 129 | |
| 130 | app.run('0.0.0.0', args.port) |