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 | 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 |
| 10 | from flask_restful import Resource, Api |
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 |
| 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 |
| 17 | import device |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 18 | |
| 19 | app = Flask(__name__) |
| 20 | api = Api(app) |
| 21 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame^] | 22 | devices = {} # dict imsi:device |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 23 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 24 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame^] | 25 | |
| 26 | class ReachableDevices(Resource): |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 27 | def get(self): |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame^] | 28 | global devices |
| 29 | return device.get_reachable(devices) |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 30 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame^] | 31 | class UnreachableDevices(Resource): |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 32 | def get(self): |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame^] | 33 | global devices |
| 34 | return device.get_unreachable(devices) |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 35 | |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 36 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame^] | 37 | api.add_resource(ReachableDevices, '/reachable-devices') |
| 38 | api.add_resource(UnreachableDevices, '/unreachable-devices') |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | def build_argparser(): |
| 42 | parser = ArgumentParser(add_help=False) |
| 43 | args = parser.add_argument_group('Options') |
| 44 | args.add_argument('-h', '--help', |
| 45 | action='help', |
| 46 | default=SUPPRESS, |
| 47 | help='Show this help message and exit.') |
| 48 | args.add_argument("--user", |
| 49 | help="ROC username", |
| 50 | type=str) |
| 51 | args.add_argument("--password", |
| 52 | help="ROC password", |
| 53 | type=str) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame^] | 54 | args.add_argument("--token", |
| 55 | help="Rancher bearer token", |
| 56 | type=str) |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 57 | return parser |
| 58 | |
Shad Ansari | 1dcfdb3 | 2022-01-24 23:13:06 +0000 | [diff] [blame] | 59 | |
| 60 | if __name__ == '__main__': |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 61 | log.basicConfig( |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 62 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 63 | level=log.DEBUG, |
| 64 | datefmt='%Y-%m-%d %H:%M:%S', |
| 65 | stream=sys.stdout) |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 66 | |
| 67 | log.info("Starting network-diag-app...") |
| 68 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 69 | args = build_argparser().parse_args() |
| 70 | |
| 71 | roc = Roc(args.user, args.password) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame^] | 72 | prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1]) |
| 73 | |
| 74 | roc.update_devices(devices) |
| 75 | |
| 76 | prom.update_devices(devices) |
| 77 | |
| 78 | for imsi_id, device in devices.items(): |
| 79 | if device.ip is None: |
| 80 | continue |
| 81 | if ping(device.ip): |
| 82 | device.reachable = True |
| 83 | device.last_reachable = datetime.now() |
| 84 | log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip)) |
| 85 | else: |
| 86 | device.reachable = False |
| 87 | log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip)) |
| 88 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 89 | |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 90 | app.run('0.0.0.0', '3333') |