| """ |
| SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
| SPDX-License-Identifier: LicenseRef-ONF-Member-1.01 |
| """ |
| import sys |
| import re |
| from datetime import datetime |
| |
| from flask import Flask, request |
| from flask_restful import Resource, Api |
| import logging as log |
| from argparse import ArgumentParser, SUPPRESS |
| |
| from roc import Roc |
| from prom import Prometheus |
| from ping import ping |
| import device |
| |
| app = Flask(__name__) |
| api = Api(app) |
| |
| devices = {} # dict imsi:device |
| |
| |
| |
| class ReachableDevices(Resource): |
| def get(self): |
| global devices |
| return device.get_reachable(devices) |
| |
| class UnreachableDevices(Resource): |
| def get(self): |
| global devices |
| return device.get_unreachable(devices) |
| |
| |
| api.add_resource(ReachableDevices, '/reachable-devices') |
| api.add_resource(UnreachableDevices, '/unreachable-devices') |
| |
| |
| def build_argparser(): |
| parser = ArgumentParser(add_help=False) |
| args = parser.add_argument_group('Options') |
| args.add_argument('-h', '--help', |
| action='help', |
| default=SUPPRESS, |
| help='Show this help message and exit.') |
| args.add_argument("--user", |
| help="ROC username", |
| type=str) |
| args.add_argument("--password", |
| help="ROC password", |
| type=str) |
| args.add_argument("--token", |
| help="Rancher bearer token", |
| type=str) |
| return parser |
| |
| |
| if __name__ == '__main__': |
| log.basicConfig( |
| format='%(asctime)s %(levelname)-8s %(message)s', |
| level=log.DEBUG, |
| datefmt='%Y-%m-%d %H:%M:%S', |
| stream=sys.stdout) |
| |
| log.info("Starting network-diag-app...") |
| |
| args = build_argparser().parse_args() |
| |
| roc = Roc(args.user, args.password) |
| prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1]) |
| |
| roc.update_devices(devices) |
| |
| prom.update_devices(devices) |
| |
| for imsi_id, device in devices.items(): |
| if device.ip is None: |
| continue |
| if ping(device.ip): |
| device.reachable = True |
| device.last_reachable = datetime.now() |
| log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip)) |
| else: |
| device.reachable = False |
| log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip)) |
| |
| |
| app.run('0.0.0.0', '3333') |